wizypay-api-client 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8573094a2d7f24094536131ae2fe1ecf653e264c
4
- data.tar.gz: 504cd6b646b781050fc88bea39ceed4dcd78e690
3
+ metadata.gz: 426e05d103a20afb933b420f11f2b2ca440d7c09
4
+ data.tar.gz: 76eaa542040e90e45323248168db754f41bb6260
5
5
  SHA512:
6
- metadata.gz: 7e5aae2e97ba7c749071bc262319213fa6b6fa0046d6fe92e3c02f1a8e3521de5d95632beaa3f4fe2055cc626b1c7ff3cfaa6fd65121772895505cc2bceb994d
7
- data.tar.gz: 5a49b1f53e0b97029f696446232dbb53952217cdaa39eeb1b289b79acc98771a2b8c3e3f5642ee7a7da5fe7ea3d728d4256dc18577d7a0f66d6601dd7c6f8d35
6
+ metadata.gz: c7d032b91a535f34c00d57ac2d65013f677ae75eadff83316259f83b24320cd8a27d20a94436916a0cb15ab7821cf67bae1e0b7be15f9cda8dbf9b7c9b2f1495
7
+ data.tar.gz: 04967cd33c55cb9ddaba58d5b0b62b47f320c27efe8673800dacd1040d0a5f84dcc64e6d38f7feb206f4dbcaa1d8c846195fc513f94fda333b1cf6308fd82335
data/lib/wizypay.rb CHANGED
@@ -6,7 +6,10 @@ require 'rest-client'
6
6
  require 'simple-hmac'
7
7
  require 'json'
8
8
  require_relative 'wizypay/client'
9
+ require_relative 'wizypay/resource'
10
+ require_relative 'wizypay/collection'
9
11
  require_relative 'wizypay/card'
12
+ require_relative 'wizypay/merchant_category'
10
13
  require_relative 'wizypay/merchant'
11
14
 
12
15
  module Wizypay
data/lib/wizypay/card.rb CHANGED
@@ -1,16 +1,25 @@
1
1
  module Wizypay
2
- class Card
2
+ class Card < Resource
3
3
  def self.create(reference, amount, currency, merchant_id, user)
4
- user = user.to_json if user.is_a?(Hash)
5
- payload = { amount: amount, currency: currency, merchant_id: merchant_id, reference: reference, user: user }
6
- Client.post('/cards', payload)
4
+ user = user.to_json if user.is_a?(Hash)
5
+ new(amount: amount, currency: currency, merchant_id: merchant_id, reference: reference, user: user).tap do |card|
6
+ card.save
7
+ end
7
8
  end
8
9
 
9
10
  def self.cancel(reference)
10
- Client.post("/cards/#{URI::encode_www_form_component reference}/cancel")
11
+ new(reference: reference).cancel
11
12
  end
12
13
 
13
14
  def self.url(reference)
15
+ new(reference: reference).url
16
+ end
17
+
18
+ def cancel
19
+ Client.post("/cards/#{URI::encode_www_form_component reference}/cancel")
20
+ end
21
+
22
+ def url
14
23
  timestamp = Time.now.utc.httpdate
15
24
  security = Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', Client.api_secret, "#{reference}\n#{timestamp}"))
16
25
  query_string = {
@@ -21,5 +30,9 @@ module Wizypay
21
30
  }.map { |k, v| "#{k}=#{URI::encode_www_form_component(v)}" }.join('&')
22
31
  "#{Client.api_endpoint}/widget?#{query_string}"
23
32
  end
33
+
34
+ def save
35
+ reinitialize Client.post('/cards', to_h)
36
+ end
24
37
  end
25
38
  end
@@ -0,0 +1,26 @@
1
+ class Wizypay::Collection
2
+ include Enumerable
3
+
4
+ def initialize(klass, data, meta = {})
5
+ @data = data.map {|h| klass.new(h)}
6
+ @meta = meta
7
+ end
8
+
9
+ def respond_to?(m)
10
+ data.respond_to?(m) || meta.has_key?(m.to_s) || super
11
+ end
12
+
13
+ def method_missing(m, *args, &block)
14
+ if data.respond_to?(m)
15
+ data.send(m, *args, &block)
16
+ elsif meta.has_key?(m.to_s)
17
+ meta[m.to_s]
18
+ else
19
+ super
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :data, :meta
26
+ end
@@ -1,11 +1,20 @@
1
1
  module Wizypay
2
- class Merchant
2
+ class Merchant < Resource
3
3
  def self.where(q)
4
- Client.get('/merchants', q)[:data]
4
+ raw = Client.get('/merchants', q)
5
+ Collection.new(self, raw[:data], raw[:meta])
5
6
  end
6
7
 
7
8
  def self.all(q = {})
8
9
  where(q)
9
10
  end
11
+
12
+ def primary_ad
13
+ Resource.new(super['data'])
14
+ end
15
+
16
+ def categories
17
+ Collection.new(MerchantCategory, super['data'], super['meta'])
18
+ end
10
19
  end
11
20
  end
@@ -0,0 +1,4 @@
1
+ module Wizypay
2
+ class MerchantCategory < Resource
3
+ end
4
+ end
@@ -0,0 +1,10 @@
1
+ class Wizypay::Resource < OpenStruct
2
+ def reinitialize(h)
3
+ each_pair do |k, _|
4
+ delete_field k
5
+ end
6
+ h.each do |k, v|
7
+ self[k] = v
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module Wizypay
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -1,46 +1,56 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe 'DistributionApi::cancel_card' do
3
+ module Wizypay
4
+ RSpec.describe 'Card#cancel' do
4
5
 
5
- let(:api_key) { 'key' }
6
- let(:api_secret) { 'secret' }
7
- let(:api_endpoint) { 'https://api.wizypay.com' }
8
- let(:reference) { '85fb584c-72cb-4f35-b307-ac5442ef7153' }
9
- let(:cancel_request) { stub_request(:post, "#{api_endpoint}/cards/#{reference}/cancel").
10
- with(headers: { 'Accept' => 'application/json',
11
- 'Authorization' => /WIZYPAY #{api_key}:.*/,
12
- 'Content-Type' => 'application/x-www-form-urlencoded' }) }
13
- before do
14
- Wizypay.setup(api_key, api_secret, api_endpoint)
15
- end
16
-
17
- context 'success' do
18
- it 'returns empty body' do
19
- request = cancel_request.to_return(status: 200, body: '')
20
- response = Wizypay::Card.cancel(reference)
21
- expect(request).to have_been_made
22
- expect(response).to be_empty
6
+ let(:api_key) { 'key' }
7
+ let(:api_secret) { 'secret' }
8
+ let(:api_endpoint) { 'https://api.wizypay.com' }
9
+ let(:reference) { '85fb584c-72cb-4f35-b307-ac5442ef7153' }
10
+ let(:cancel_request) { stub_request(:post, "#{api_endpoint}/cards/#{reference}/cancel").
11
+ with(headers: { 'Accept' => 'application/json',
12
+ 'Authorization' => /WIZYPAY #{api_key}:.*/,
13
+ 'Content-Type' => 'application/x-www-form-urlencoded' }) }
14
+ before do
15
+ Wizypay.setup(api_key, api_secret, api_endpoint)
23
16
  end
24
- end
25
17
 
26
- context 'failure' do
27
- context '4XX' do
28
- it "throws #{RestClient::Exception}" do
29
- request = cancel_request.to_return(status: 401, body: { errors: { base: ['authentication_failed'] } }.to_json)
30
- expect do
31
- Wizypay::Card.cancel(reference)
32
- end.to raise_error(RestClient::Exception)
18
+ context 'success' do
19
+ it 'returns empty body' do
20
+ request = cancel_request.to_return(status: 200, body: '')
21
+ response = Card.new(reference: reference).cancel
33
22
  expect(request).to have_been_made
23
+ expect(response).to be_empty
34
24
  end
35
25
  end
36
26
 
37
- context 'timeout' do
38
- it "throws #{RestClient::Exception}" do
39
- request = cancel_request.to_timeout
40
- expect do
41
- Wizypay::Card.cancel(reference)
42
- end.to raise_error(RestClient::Exception)
43
- expect(request).to have_been_made
27
+ describe '.cancel' do
28
+ it 'delegates to an instance' do
29
+ expect(Card).to receive(:new).with(reference: reference).and_return(card = double('card'))
30
+ expect(card).to receive(:cancel)
31
+ Card.cancel(reference)
32
+ end
33
+ end
34
+
35
+ context 'failure' do
36
+ context '4XX' do
37
+ it "throws #{RestClient::Exception}" do
38
+ request = cancel_request.to_return(status: 401, body: { errors: { base: ['authentication_failed'] } }.to_json)
39
+ expect do
40
+ Card.cancel(reference)
41
+ end.to raise_error(RestClient::Exception)
42
+ expect(request).to have_been_made
43
+ end
44
+ end
45
+
46
+ context 'timeout' do
47
+ it "throws #{RestClient::Exception}" do
48
+ request = cancel_request.to_timeout
49
+ expect do
50
+ Card.cancel(reference)
51
+ end.to raise_error(RestClient::Exception)
52
+ expect(request).to have_been_made
53
+ end
44
54
  end
45
55
  end
46
56
  end
@@ -1,25 +1,35 @@
1
1
  require 'spec_helper'
2
2
  require 'time'
3
3
 
4
- RSpec.describe 'DistributionApi::create_card' do
4
+ module Wizypay
5
+ RSpec.describe 'Card#url' do
5
6
 
6
- let(:api_key) { 'key' }
7
- let(:api_secret) { 'secret' }
8
- let(:api_endpoint) { 'https://api.wizypay.com' }
9
- let(:reference) { '85fb584c-72cb-4f35-b307-ac5442ef7153' }
7
+ let(:api_key) { 'key' }
8
+ let(:api_secret) { 'secret' }
9
+ let(:api_endpoint) { 'https://api.wizypay.com' }
10
+ let(:reference) { '85fb584c-72cb-4f35-b307-ac5442ef7153' }
10
11
 
11
- before do
12
- Wizypay.setup(api_key, api_secret, api_endpoint)
13
- end
12
+ before do
13
+ Wizypay.setup(api_key, api_secret, api_endpoint)
14
+ end
15
+
16
+ it 'returns' do
17
+ t = Time.new('2015-10-30T21:00:00+001')
18
+ Timecop.freeze(t) do
19
+ url = Card.new(reference: reference).url
20
+ expect(url).to eq("#{api_endpoint}/widget?key=#{URI::encode_www_form_component api_key}"+
21
+ "&timestamp=#{URI::encode_www_form_component t.httpdate}" +
22
+ "&reference=#{URI::encode_www_form_component reference}"+
23
+ "&security=0fKNCz6hux0IjNCz31JHgzh35gqLTAIM8z3iNhSkC7g%3D")
24
+ end
25
+ end
14
26
 
15
- it 'returns' do
16
- t = Time.new('2015-10-30T21:00:00+001')
17
- Timecop.freeze(t) do
18
- url = Wizypay::Card.url(reference)
19
- expect(url).to eq("#{api_endpoint}/widget?key=#{URI::encode_www_form_component api_key}"+
20
- "&timestamp=#{URI::encode_www_form_component t.httpdate}" +
21
- "&reference=#{URI::encode_www_form_component reference}"+
22
- "&security=0fKNCz6hux0IjNCz31JHgzh35gqLTAIM8z3iNhSkC7g%3D")
27
+ describe '.url' do
28
+ it 'delegates to an instance' do
29
+ expect(Card).to receive(:new).with(reference: reference).and_return(card = double('card'))
30
+ expect(card).to receive(:url)
31
+ Card.url(reference)
32
+ end
23
33
  end
24
34
  end
25
35
  end
@@ -1,60 +1,62 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe 'DistributionApi::create_card' do
4
-
5
- let(:api_key) { 'key' }
6
- let(:api_secret) { 'secret' }
7
- let(:api_endpoint) { 'https://api.wizypay.com' }
8
- let(:user) { { id: 33, first_name: 'Nicolas', last_name: 'Cage' } }
9
- let(:reference) { '85fb584c-72cb-4f35-b307-ac5442ef7153' }
10
- let(:merchant_id) { '91' }
11
- let(:amount) { '5000' }
12
- let(:currency) { 'EUR' }
13
- let(:create_request) { stub_request(:post, "#{api_endpoint}/cards").
14
- with(body: { amount: amount, currency: currency, merchant_id: merchant_id,
15
- reference: reference, user: user.to_json },
16
- headers: { 'Accept' => 'application/json',
17
- 'Authorization' => /WIZYPAY #{api_key}:.*/,
18
- 'Content-Type' => 'application/x-www-form-urlencoded' }) }
19
- before do
20
- Wizypay.setup(api_key, api_secret, api_endpoint)
21
- end
22
-
23
- context 'success' do
24
- it 'returns response as hash' do
25
- response_body = {
26
- reference: reference,
27
- type: 'Mastercard',
28
- amount: amount,
29
- currency: currency,
30
- merchant_id: merchant_id,
31
- user_id: user[:id]
32
- }
33
- request = create_request.to_return(status: 200, body: response_body.to_json)
34
- response = Wizypay::Card.create(reference, amount, currency, merchant_id, user)
35
- expect(request).to have_been_made
36
- expect(response).to match(response_body)
3
+ module Wizypay
4
+ RSpec.describe 'Card.create' do
5
+ let(:api_key) { 'key' }
6
+ let(:api_secret) { 'secret' }
7
+ let(:api_endpoint) { 'https://api.wizypay.com' }
8
+ let(:user) { { id: 33, first_name: 'Nicolas', last_name: 'Cage' } }
9
+ let(:reference) { '85fb584c-72cb-4f35-b307-ac5442ef7153' }
10
+ let(:merchant_id) { '91' }
11
+ let(:amount) { '5000' }
12
+ let(:currency) { 'EUR' }
13
+ let(:create_request) { stub_request(:post, "#{api_endpoint}/cards").
14
+ with(body: { amount: amount, currency: currency, merchant_id: merchant_id,
15
+ reference: reference, user: user.to_json },
16
+ headers: { 'Accept' => 'application/json',
17
+ 'Authorization' => /WIZYPAY #{api_key}:.*/,
18
+ 'Content-Type' => 'application/x-www-form-urlencoded' }) }
19
+ before do
20
+ Wizypay.setup(api_key, api_secret, api_endpoint)
37
21
  end
38
- end
39
22
 
40
- context 'failure' do
41
- context '4XX' do
42
- it "throws #{RestClient::Exception}" do
43
- request = create_request.to_return(status: 401, body: { errors: { base: ['authentication_failed'] } }.to_json)
44
- expect do
45
- Wizypay::Card.create(reference, amount, currency, merchant_id, user)
46
- end.to raise_error(RestClient::Exception)
23
+ context 'success' do
24
+ it 'returns a Card with correct data' do
25
+ response_body = {
26
+ reference: reference,
27
+ type: 'Mastercard',
28
+ amount: amount,
29
+ currency: currency,
30
+ merchant_id: merchant_id,
31
+ user_id: user[:id]
32
+ }
33
+ request = create_request.to_return(status: 200, body: response_body.to_json)
34
+ response = Wizypay::Card.create(reference, amount, currency, merchant_id, user)
47
35
  expect(request).to have_been_made
36
+ expect(response).to be_a(Wizypay::Card)
37
+ expect(response.to_h).to match(response_body)
48
38
  end
49
39
  end
50
40
 
51
- context 'timeout' do
52
- it "throws #{RestClient::Exception}" do
53
- request = create_request.to_timeout
54
- expect do
55
- Wizypay::Card.create(reference, amount, currency, merchant_id, user)
56
- end.to raise_error(RestClient::Exception)
57
- expect(request).to have_been_made
41
+ context 'failure' do
42
+ context '4XX' do
43
+ it "throws #{RestClient::Exception}" do
44
+ request = create_request.to_return(status: 401, body: { errors: { base: ['authentication_failed'] } }.to_json)
45
+ expect do
46
+ Card.create(reference, amount, currency, merchant_id, user)
47
+ end.to raise_error(RestClient::Exception)
48
+ expect(request).to have_been_made
49
+ end
50
+ end
51
+
52
+ context 'timeout' do
53
+ it "throws #{RestClient::Exception}" do
54
+ request = create_request.to_timeout
55
+ expect do
56
+ Card.create(reference, amount, currency, merchant_id, user)
57
+ end.to raise_error(RestClient::Exception)
58
+ expect(request).to have_been_made
59
+ end
58
60
  end
59
61
  end
60
62
  end
@@ -1,56 +1,85 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe 'AffiliationApi::merchants' do
4
-
5
- let(:api_key) { 'key' }
6
- let(:api_secret) { 'secret' }
7
- let(:api_endpoint) { 'https://api.wizypay.com' }
8
- let(:all_merchants_request) { stub_request(:get, "#{api_endpoint}/merchants").
9
- with(headers: { 'Accept' => 'application/json',
10
- 'Authorization' => /WIZYPAY #{api_key}:.*/ }) }
11
- let(:where_merchants_request) { stub_request(:get, "#{api_endpoint}/merchants?q=hp").
12
- with(headers: { 'Accept' => 'application/json',
13
- 'Authorization' => /WIZYPAY #{api_key}:.*/ }) }
14
- before do
15
- Wizypay.setup(api_key, api_secret, api_endpoint)
16
- end
17
-
18
- context 'all' do
19
- it 'returns empty body' do
20
- request = all_merchants_request.to_return(status: 200, body: '{"data": []}')
21
- response = Wizypay::Merchant.all
22
- expect(request).to have_been_made
23
- expect(response).to be_empty
3
+ module Wizypay
4
+ RSpec.describe Merchant do
5
+ let(:api_key) { 'key' }
6
+ let(:api_secret) { 'secret' }
7
+ let(:api_endpoint) { 'https://api.wizypay.com' }
8
+ let(:all_merchants_request) { stub_request(:get, "#{api_endpoint}/merchants").
9
+ with(headers: { 'Accept' => 'application/json',
10
+ 'Authorization' => /WIZYPAY #{api_key}:.*/ }) }
11
+ let(:where_merchants_request) { stub_request(:get, "#{api_endpoint}/merchants?q=hp").
12
+ with(headers: { 'Accept' => 'application/json',
13
+ 'Authorization' => /WIZYPAY #{api_key}:.*/ }) }
14
+ before do
15
+ Wizypay.setup(api_key, api_secret, api_endpoint)
24
16
  end
25
- end
26
17
 
27
- context 'where' do
28
- it 'returns empty body' do
29
- request = where_merchants_request.to_return(status: 200, body: '{"data": []}')
30
- response = Wizypay::Merchant.where(q: 'hp')
31
- expect(request).to have_been_made
32
- expect(response).to be_empty
18
+ context 'all' do
19
+ it 'returns empty body' do
20
+ request = all_merchants_request.to_return(status: 200, body: '{"data": []}')
21
+ response = Merchant.all
22
+ expect(request).to have_been_made
23
+ expect(response).to be_empty
24
+ end
25
+
26
+ it 'is a collection' do
27
+ all_merchants_request.to_return(status: 200, body: '{"data": [], "meta": {"total": 0}}')
28
+ response = Merchant.all
29
+ expect(response).to be_a(Collection)
30
+ expect(response.total).to eq 0
31
+ end
32
+
33
+ it 'is a collection of Merchants' do
34
+ all_merchants_request.to_return(status: 200, body: '{"data": [{"id": 1, "name": "merchant 1"}]}')
35
+ response = Merchant.all
36
+ expect(response.first).to be_a(Merchant)
37
+ expect(response.first.name).to eq 'merchant 1'
38
+ end
39
+
40
+ it 'has a primary ad' do
41
+ all_merchants_request.to_return(status: 200, body: '{"data": [{"primary_ad": {"data": {"id": 1}}}]}')
42
+ response = Merchant.all
43
+ expect(response.first.primary_ad.id).to eq 1
44
+ end
45
+
46
+ it 'has categories' do
47
+ all_merchants_request.to_return(status: 200, body: '{"data": [{"categories": {"data": [{"id": 1}]}}]}')
48
+ response = Merchant.all
49
+ expect(response.first.categories).to be_a(Collection)
50
+ expect(response.first.categories.first).to be_a(MerchantCategory)
51
+ expect(response.first.categories.first.id).to eq 1
52
+ end
33
53
  end
34
- end
35
54
 
36
- context 'failure' do
37
- context '4XX' do
38
- it "throws #{RestClient::Exception}" do
39
- request = all_merchants_request.to_return(status: 401, body: { errors: { base: ['authentication_failed'] } }.to_json)
40
- expect do
41
- Wizypay::Merchant.all
42
- end.to raise_error(RestClient::Exception)
55
+ context 'where' do
56
+ it 'returns empty body' do
57
+ request = where_merchants_request.to_return(status: 200, body: '{"data": []}')
58
+ response = Merchant.where(q: 'hp')
43
59
  expect(request).to have_been_made
60
+ expect(response).to be_empty
44
61
  end
45
62
  end
46
63
 
47
- context 'timeout' do
48
- it "throws #{RestClient::Exception}" do
49
- request = all_merchants_request.to_timeout
50
- expect do
51
- Wizypay::Merchant.all
52
- end.to raise_error(RestClient::Exception)
53
- expect(request).to have_been_made
64
+ context 'failure' do
65
+ context '4XX' do
66
+ it "throws #{RestClient::Exception}" do
67
+ request = all_merchants_request.to_return(status: 401, body: { errors: { base: ['authentication_failed'] } }.to_json)
68
+ expect do
69
+ Merchant.all
70
+ end.to raise_error(RestClient::Exception)
71
+ expect(request).to have_been_made
72
+ end
73
+ end
74
+
75
+ context 'timeout' do
76
+ it "throws #{RestClient::Exception}" do
77
+ request = all_merchants_request.to_timeout
78
+ expect do
79
+ Merchant.all
80
+ end.to raise_error(RestClient::Exception)
81
+ expect(request).to have_been_made
82
+ end
54
83
  end
55
84
  end
56
85
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wizypay-api-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chaker Nakhli
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-01-28 00:00:00.000000000 Z
13
+ date: 2016-01-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: simple-hmac
@@ -138,7 +138,10 @@ files:
138
138
  - lib/wizypay.rb
139
139
  - lib/wizypay/card.rb
140
140
  - lib/wizypay/client.rb
141
+ - lib/wizypay/collection.rb
141
142
  - lib/wizypay/merchant.rb
143
+ - lib/wizypay/merchant_category.rb
144
+ - lib/wizypay/resource.rb
142
145
  - lib/wizypay/version.rb
143
146
  - spec/card/cancel_card_spec.rb
144
147
  - spec/card/card_url_spec.rb