Skip to content

Commit d84af64

Browse files
committed
Fixes:
* Api `put` method * raising error on 5xx response * gemspec points to quickpay * README improvments
1 parent 355e028 commit d84af64

11 files changed

+77
-59
lines changed

LICENSE.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Dinesh Yadav
3+
Copyright (c) 2015 QuickPay
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ or install from Rubygems:
1313

1414
$ gem install quickpay-ruby-client
1515

16-
It is currently tested with Ruby 1.9
16+
It is currently tested with Ruby ( >= 2.1.x)
1717

1818
* MPI
19-
* Rubinius
19+
* Rubinius (2.0)
2020

2121
## Usage
2222

@@ -43,7 +43,7 @@ client = Quickpay::Client.new("#{ENV['QUICKPAY_LOGIN']}:#{ENV['QUICKPAY_PASSWORD
4343

4444
### API Calls
4545

46-
You can afterwards call any method described in QuickPay api with corresponding http method and endpoint. These methods are supported currently: `get`, `post`, `patch` and `delete`.
46+
You can afterwards call any method described in QuickPay api with corresponding http method and endpoint. These methods are supported currently: `get`, `post`, `put`, `patch` and `delete`.
4747

4848
```
4949
client.get("/activities").each do |activity|
@@ -69,7 +69,7 @@ end
6969

7070
### Handling API exceptions
7171

72-
By default `(get|post|delete|patch)` will return JSON parsed body on success (i.e. non `4xx` response code) otherwise it will raise appropriate error. Your code should handle the errors appropriately. Following error codes are supported currently:
72+
By default `(get|post|patch|put|delete)` will return JSON parsed body on success (i.e. `2xx` response code) otherwise it will raise appropriate error. Your code should handle the errors appropriately. Following error codes are supported currently:
7373

7474

7575
Response status | Error |
@@ -95,4 +95,4 @@ rescue Quickpay::Error => e
9595
end
9696
```
9797

98-
You can more about api responses at: [Link](http://tech.quickpay.net/api/)
98+
You can read more about api responses at: [Link](http://tech.quickpay.net/api/)

Rakefile

-2
This file was deleted.

bin/console

-14
This file was deleted.

bin/setup

-7
This file was deleted.

lib/quickpay/client.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def initialize credential
77
@credential = credential
88
end
99

10-
[:get, :post, :patch, :delete].each do |method|
10+
[:get, :post, :patch, :put, :delete].each do |method|
1111
define_method(method) do |*args|
1212
Request.new(@credential).request(method, *args)
1313
end

lib/quickpay/request.rb

+13-10
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ def initialize secret
1010

1111
def request method, path, data = {}
1212
raw = data.delete(:raw)
13+
1314
options = case method
14-
when :get
15+
when :get, :delete
1516
{ query: data }
16-
when :post, :patch
17+
when :post, :patch, :put
1718
{ body: data }
1819
end || {}
1920

@@ -23,15 +24,12 @@ def request method, path, data = {}
2324
end
2425

2526
def create_response raw, res
26-
response = res.parsed_response
2727
if raw
28-
[res.code, response, res.headers]
28+
[res.code, res.body, res.headers]
2929
else
30-
raise_error(response, res.code) if res.code.to_s =~ /4\d\d/
31-
32-
response.kind_of?(String) ?
33-
JSON.parse(response) :
34-
response
30+
response = res.parsed_response
31+
raise_error(response, res.code) if res.code >= 400
32+
response
3533
end
3634
end
3735

@@ -52,7 +50,12 @@ def raise_error body, status
5250
end
5351

5452
private
55-
53+
54+
def json_response? res
55+
res.headers.key?('content-type') &&
56+
res.headers['content-type'].include?('application/json')
57+
end
58+
5659
def error_description msg
5760
msg
5861
end

quickpay-ruby-client.gemspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ require 'quickpay/version'
66
Gem::Specification.new do |spec|
77
spec.name = "quickpay-ruby-client"
88
spec.version = Quickpay::VERSION
9-
spec.authors = ["Dinesh Yadav"]
10-
spec.email = ["[email protected]"]
9+
spec.authors = ["QuickPay Developers"]
10+
spec.email = ["[email protected]"]
1111

1212
spec.summary = "Ruby client for QuickPay API"
1313
spec.description = "Embed QuickPay's secure payments directly into your Ruby applications. more at https://tech.quickpay.net"

spec/quickpay/client_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper'
22

3-
describe Quickpay::Client, wip: true do
3+
describe Quickpay::Client do
44
let(:secret){ 'test:test' }
55
let(:client) { Quickpay::Client.new(secret) }
66

spec/quickpay/request_spec.rb

+40-10
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,56 @@
1212
describe '.request' do
1313
context 'when method is get' do
1414
it {
15-
stub_qp_request(:get, '/dummy', 200, { :id => 100 })
15+
stub_json_request(:get, '/dummy', 200, { :id => 100 })
1616
response = handler.request(:get, '/dummy')
1717
expect(response['id']).to eq(100)
1818
expect_qp_request(:get, "/dummy", "")
1919
}
2020
end
2121

22-
context 'when method is post/patch' do
22+
context 'when method is post/patch/put' do
2323
it {
24-
stub_qp_request(:post, '/dummy', 200, { :id => 100 })
24+
stub_json_request(:post, '/dummy', 200, { :id => 100 })
2525
response = handler.request(:post, '/dummy', { currency: 'DKK' })
2626
expect(response['id']).to eq(100)
2727
expect_qp_request(:post, "/dummy", { currency: 'DKK' })
2828
}
29+
30+
it {
31+
stub_json_request(:put, '/dummy', 200, { :id => 100 })
32+
response = handler.request(:put, '/dummy', { currency: 'DKK' })
33+
expect(response['id']).to eq(100)
34+
expect_qp_request(:put, "/dummy", { currency: 'DKK' })
35+
}
36+
37+
it {
38+
stub_json_request(:patch, '/dummy', 200, { :id => 100 })
39+
response = handler.request(:patch, '/dummy', { currency: 'DKK' })
40+
expect(response['id']).to eq(100)
41+
expect_qp_request(:patch, "/dummy", { currency: 'DKK' })
42+
}
43+
2944
end
3045

3146
context 'when method is delete' do
3247
it {
33-
stub_qp_request(:delete, '/dummy', 200, { :id => 100 })
48+
stub_json_request(:delete, '/dummy?currency=DKK', 200, { :id => 100 })
3449
response = handler.request(:delete, '/dummy', { currency: 'DKK' })
3550
expect(response['id']).to eq(100)
36-
expect_qp_request(:delete, "/dummy", "")
51+
expect_qp_request(:delete, "/dummy?currency=DKK", "")
3752
}
3853
end
3954

4055
end
4156

4257
describe '.create_response' do
58+
4359
context 'when raw is true' do
44-
it 'should return raw response' do
60+
it 'should return raw response', wip: true do
4561
body = { "id" => 100 }
4662
stub_request(:get, "https://api.quickpay.net/dummy").
47-
to_return(:status => 200, :body => body.to_json, :headers => {})
63+
to_return(:status => 200, :body => body.to_json,
64+
:headers => { "Content-Type" => 'application/json'})
4865

4966
response = handler.create_response(true, handler.class.get('/dummy'))
5067

@@ -54,16 +71,29 @@
5471
end
5572
end
5673

57-
context 'when raw is false' do
58-
it 'should return hash' do
74+
context 'when raw is not present' do
75+
it 'should return http body' do
5976
body = { "id" => 100 }
6077
stub_request(:get, "https://api.quickpay.net/dummy").
61-
to_return(:status => 200, :body => body.to_json, :headers => {})
78+
to_return(:status => 200, :body => body.to_json,
79+
:headers => {"Content-Type" => 'application/json' })
6280

6381
response = handler.create_response(false, handler.class.get('/dummy'))
6482

6583
expect(response).to include("id" => 100)
6684
end
85+
end
86+
87+
context 'when content-type is not json' do
88+
it 'should return raw response' do
89+
body = 'foobar'
90+
stub_request(:get, "https://api.quickpay.net/dummy").
91+
to_return(:status => 200, :body => 'foobar')
92+
93+
response = handler.create_response(false, handler.class.get('/dummy'))
94+
95+
expect(response).to eq("foobar")
96+
end
6797
end
6898

6999
end

spec/support/common_helpers.rb

+14-6
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@ def url_with_secret
99
"https://#{secret}@#{Quickpay::BASE_URI.split('://').last}"
1010
end
1111

12-
def stub_qp_request(method, path, status, response_body)
12+
def stub_qp_request(method, path, status, response_body, response_headers = {})
1313
url = URI.join(url_with_secret, path)
1414

1515
stub_request(method, url)
1616
.with(:headers => headers)
17-
.to_return(:status => status, :body => response_body.to_json)
17+
.to_return(:status => status,
18+
:headers => response_headers,
19+
:body => response_body.to_json)
1820

1921
end
2022

23+
def stub_json_request *args
24+
stub_qp_request(*(args << { 'Content-Type' => 'application/json' }))
25+
end
26+
2127
def expect_qp_request(method, path, body)
2228
url = URI.join(url_with_secret, path)
2329

@@ -26,16 +32,18 @@ def expect_qp_request(method, path, body)
2632
:headers => headers)
2733
end
2834

29-
def headers
35+
def headers options = {}
3036
user_agent = "quickpay-ruby-client, v#{Quickpay::VERSION}"
3137
user_agent += ", #{RUBY_VERSION}, #{RUBY_PLATFORM}, #{RUBY_PATCHLEVEL}"
3238
if defined?(RUBY_ENGINE)
3339
user_agent += ", #{RUBY_ENGINE}"
3440
end
3541

36-
{ "Accept-Version" => "v10",
37-
"User-Agent" => user_agent
38-
}
42+
{
43+
"Accept-Version" => "v10",
44+
"User-Agent" => user_agent
45+
}
3946
end
47+
4048
end
4149
end

0 commit comments

Comments
 (0)