besepa 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c069ce337f4d79698fad699f1b84963b81b083b2
4
- data.tar.gz: 88a9d145c8bffd3987a5426efb3ac3161339ce1b
3
+ metadata.gz: 320cf673fdede800b3b27d112273da9d5bfa8702
4
+ data.tar.gz: 0872852f873885be68c8c60d3fb59faa39443f9f
5
5
  SHA512:
6
- metadata.gz: b68ac3392ce09105c3109845cffdd641edc88385e468f2ff4c7c6fb13a534597292fbcad5239321bb38a430a194b3cf39ef42241717d0a06f647ae64e5d21160
7
- data.tar.gz: a40e01d429ac07a1cf07c40d8ffd6574dc56f69c14b7ab6a347aca4aad946f6e3585f794ccdda700bc46d9aebe91f617182bc87fbcc7cd1a76e7fbd721533fc0
6
+ metadata.gz: 0290b23a8336c5b63cfbf362eef511b2b1a6ac5c2ce4c488dfb7f480d09f1858fec5ceed547fe5a751679fe0947ac0f1d006fbf42bc9b0a40d8615c4cbae4345
7
+ data.tar.gz: 62ed15a4ce4b7852cf876d4df24ccfa06fb3fab248f522c2f8d246a6826663505e1c38892af132a6c5e75a9fd5f8017530c4465a11acad605518135e87476bb8
@@ -1,36 +1,26 @@
1
1
  module Besepa
2
-
3
2
  module ApiCalls
4
-
5
3
  module List
6
-
7
4
  module ClassMethods
8
-
9
5
  def all(filters={})
10
- response = get "/#{api_path(filters)}"
11
- objects = Array.new
12
- if response['count'] > 0
13
- response['response'].each do |c|
14
- objects << self.new(c)
15
- end
16
- end
17
- objects
6
+ path = "/#{api_path(filters)}"
7
+ params = filters.select{|x| [:page, :field, :value].include?(x)}
8
+ response = get(path, params)
9
+
10
+ Besepa::Collection.new(response, self)
18
11
  end
19
-
12
+
20
13
  def find(id, filters={})
21
14
  response = get "/#{api_path(filters)}/#{id}"
22
15
  c = self.new(response['response'])
23
16
  c
24
17
  end
25
-
26
18
  end
27
19
 
28
20
  def self.included(base)
29
21
  base.extend(ClassMethods)
30
22
  end
31
-
23
+
32
24
  end
33
-
34
25
  end
35
-
36
26
  end
@@ -2,24 +2,17 @@ module Besepa
2
2
  module ApiCalls
3
3
  module Search
4
4
  module ClassMethods
5
-
6
5
  def search(filters = {})
7
- response = get "/#{api_path}/search?field=#{filters[:field]}&value=#{filters[:value]}"
8
- objects = Array.new
9
- if response['count'] > 0
10
- response['response'].each do |c|
11
- objects << self.new(c)
12
- end
13
- end
14
- objects
6
+ path = "/#{api_path}/search"
7
+ params = filters.select{|x| [:page, :field, :value].include?(x)}
8
+ response = get(path, params)
9
+ Besepa::Collection.new(response, self)
15
10
  end
16
-
17
11
  end
18
12
 
19
13
  def self.included(base)
20
14
  base.extend(ClassMethods)
21
15
  end
22
-
23
16
  end
24
17
  end
25
18
  end
@@ -0,0 +1,36 @@
1
+ module Besepa
2
+ class Collection
3
+ include Enumerable
4
+ attr_accessor :items, :pages, :per_page, :total, :current_page
5
+ attr_reader :pagination
6
+
7
+ def initialize(response, klass)
8
+ @pagination = false
9
+ @items = []
10
+ process_response(response, klass)
11
+ end
12
+
13
+ def each &block
14
+ @items.each{|item| block.call(item)}
15
+ end
16
+
17
+ alias_method :size, :count
18
+
19
+ private
20
+
21
+ def process_response(response, klass)
22
+ if response['pagination']
23
+ @pagination = true
24
+ self.per_page = response['pagination']['per_page']
25
+ self.current_page = response['pagination']['current']
26
+ self.total = response['pagination']['count']
27
+ self.pages = response['pagination']['pages']
28
+ end
29
+
30
+ response['response'].each do |c|
31
+ @items << klass.new(c)
32
+ end
33
+ end
34
+
35
+ end
36
+ end
@@ -1,7 +1,7 @@
1
1
  module Besepa
2
-
2
+
3
3
  module Utils
4
-
4
+
5
5
  # Defines HTTP request methods
6
6
  module Request
7
7
 
@@ -52,9 +52,6 @@ module Besepa
52
52
  body
53
53
  end
54
54
  end
55
-
56
55
  end
57
-
58
56
  end
59
-
60
- end
57
+ end
@@ -1,6 +1,6 @@
1
1
  module Besepa
2
2
 
3
3
  module Utils
4
- VERSION = '0.5.0'.freeze
4
+ VERSION = '0.6.0'.freeze
5
5
  end
6
6
  end
@@ -4,14 +4,18 @@ describe Besepa::Customer do
4
4
 
5
5
  describe '#all' do
6
6
  before do
7
- stub_get('/customers').to_return(body: fixture('customers.json'), headers: {content_type: 'application/json; charset=utf-8'})
7
+ stub_get('/customers?page=1').to_return(body: fixture('customers.json'), headers: {content_type: 'application/json; charset=utf-8'})
8
8
  end
9
9
 
10
10
  it 'returns a list of customers' do
11
- customers = Besepa::Customer.all
12
- expect(customers).to be_an Array
11
+ customers = Besepa::Customer.all(page: 1)
12
+ expect(customers).to respond_to(:each)
13
13
  expect(customers.first).to be_an Besepa::Customer
14
14
  expect(customers.size).to eq(1)
15
+ expect(customers.per_page).to eq(50)
16
+ expect(customers.current_page).to eq(1)
17
+ expect(customers.total).to eq(1)
18
+ expect(customers.pages).to eq(1)
15
19
  end
16
20
  end
17
21
 
@@ -29,14 +33,18 @@ describe Besepa::Customer do
29
33
 
30
34
  describe '#search' do
31
35
  before do
32
- stub_get('/customers/search?field=group_id&value=foo').to_return(body: fixture('customers.json'), headers: {content_type: 'application/json; charset=utf-8'})
36
+ stub_get('/customers/search?field=group_id&value=foo&page=1').to_return(body: fixture('customers.json'), headers: {content_type: 'application/json; charset=utf-8'})
33
37
  end
34
38
 
35
39
  it 'returs a list of customers' do
36
- customers = Besepa::Customer.search(field: 'group_id', value: 'foo')
37
- expect(customers).to be_an Array
40
+ customers = Besepa::Customer.search(field: 'group_id', value: 'foo', page: 1)
41
+ expect(customers).to respond_to(:each)
38
42
  expect(customers.first).to be_an Besepa::Customer
39
43
  expect(customers.size).to eq(1)
44
+ expect(customers.per_page).to eq(50)
45
+ expect(customers.current_page).to eq(1)
46
+ expect(customers.total).to eq(1)
47
+ expect(customers.pages).to eq(1)
40
48
  end
41
49
  end
42
50
 
@@ -77,7 +85,7 @@ describe Besepa::Customer do
77
85
 
78
86
  it 'returns a list of debits' do
79
87
  debits = Besepa::Customer.new(id: 'cus12345').debits
80
- expect(debits).to be_an Array
88
+ expect(debits).to respond_to(:each)
81
89
  expect(debits.first).to be_an Besepa::Debit
82
90
  expect(debits.size).to eq(1)
83
91
  end
@@ -104,7 +112,7 @@ describe Besepa::Customer do
104
112
 
105
113
  it 'returns a list of bank accounts' do
106
114
  bank_accounts = Besepa::Customer.new(id: 'cus12345').bank_accounts
107
- expect(bank_accounts).to be_an Array
115
+ expect(bank_accounts).to respond_to(:each)
108
116
  expect(bank_accounts.first).to be_an Besepa::BankAccount
109
117
  expect(bank_accounts.size).to eq(1)
110
118
  end
@@ -10,9 +10,13 @@ describe Besepa::Group do
10
10
 
11
11
  it 'returns a list of customers' do
12
12
  customers = @group.customers
13
- expect(customers).to be_an Array
13
+ expect(customers).to respond_to(:each)
14
14
  expect(customers.first).to be_an Besepa::Customer
15
15
  expect(customers.size).to eq(1)
16
+ expect(customers.per_page).to eq(50)
17
+ expect(customers.current_page).to eq(1)
18
+ expect(customers.total).to eq(1)
19
+ expect(customers.pages).to eq(1)
16
20
  end
17
21
  end
18
22
  end
@@ -16,5 +16,12 @@
16
16
  "id": "cus9878c9231d1d65a80a053684d91fa076"
17
17
  }
18
18
  ],
19
+ "pagination":
20
+ {
21
+ "per_page": 50,
22
+ "current": 1,
23
+ "count": 1,
24
+ "pages": 1
25
+ },
19
26
  "count": 1
20
- }
27
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: besepa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - besepa.com
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-22 00:00:00.000000000 Z
11
+ date: 2016-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -103,7 +103,6 @@ extra_rdoc_files: []
103
103
  files:
104
104
  - ".gitignore"
105
105
  - Gemfile
106
- - Gemfile.lock
107
106
  - LICENSE.txt
108
107
  - README.md
109
108
  - Rakefile
@@ -117,6 +116,7 @@ files:
117
116
  - lib/besepa/api_calls/update.rb
118
117
  - lib/besepa/bank_account.rb
119
118
  - lib/besepa/business_account.rb
119
+ - lib/besepa/collection.rb
120
120
  - lib/besepa/customer.rb
121
121
  - lib/besepa/debit.rb
122
122
  - lib/besepa/errors/besepa_error.rb