atrium-ruby 0.5.0 → 1.0.1

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: 245392ec9efa63cc44d3d292397c47a32a03a16b
4
- data.tar.gz: 4a07efc49acf876f734942c70b9f2e0fb6cd62d5
3
+ metadata.gz: 938771555b9c5477b1eb655e1296636cf3df04c4
4
+ data.tar.gz: 19370f5a2dbb896cf12a026c65033169a2d5d767
5
5
  SHA512:
6
- metadata.gz: a3a18069855ba1a1b7b5076d5428fc856729b1a90a47ac9eab4478ef248922683193317ddce8956602895adb466e4912500dcbd8e02a4fce25ab639fab73fea1
7
- data.tar.gz: 3073ba5011c23cf91cd44041d660cf258a29ccb2857f677e57841cd880d7cc01e0fee1896db94b689807fc2a18b86640d263626e58cb438c724d0c7bfbd8d1a4
6
+ metadata.gz: 06ff6b39928d2c63450e4e5d3f0dcf14490140a018b4452561bdf4e5f3b2ae9972a5bc1813d05d6ea26f7f9244a317e1316b5b48f40c1b35b74488efa0f31f30
7
+ data.tar.gz: e0444f11d6993852243ee8fc6f0b975d75d03673b94cdb765133543d63d6d5052af53e986a3998292c18416da634402ff12ef39c47de4ae15537db9df52ffc10
@@ -6,5 +6,7 @@ AllCops:
6
6
  Include:
7
7
  - 'Gemfile'
8
8
  - 'Rakefile'
9
+ Style/FileName:
10
+ Enabled: false
9
11
  Style/FrozenStringLiteralComment:
10
- Enabled: false
12
+ Enabled: false
data/README.md CHANGED
@@ -11,6 +11,58 @@ end
11
11
 
12
12
  From there, you can start using some basic class methods to make calls for data. See our [full documentation](https://atrium.mx.com/documentation) for more details.
13
13
 
14
+ ## Examples
15
+
16
+ ### Pagination
17
+
18
+ The following demonstrates how you can read data back from the API in a memory efficient way using built-in pagination
19
+ helpers. You can also specify query parameters such as `from_date` and `to_date`.
20
+
21
+ ```ruby
22
+ ::Atrium::User.list_each do |user|
23
+ user.each_member do |member|
24
+ puts member.name
25
+ puts member.accounts.total_entries
26
+
27
+ member.each_account do |account|
28
+ puts account.name
29
+ puts account.transactions.total_entries
30
+
31
+ account.each_transaction do |transaction|
32
+ puts transaction.description
33
+ end
34
+ end
35
+ end
36
+
37
+ user.each_account do |account|
38
+ puts account.name
39
+ puts account.transactions.total_entries
40
+
41
+ account.each_transaction do |transaction|
42
+ puts transaction.description
43
+ end
44
+ end
45
+
46
+ user.each_transaction do |transaction|
47
+ puts transaction.description
48
+ end
49
+ end
50
+ ```
51
+
52
+ ### Date Range
53
+
54
+ You can specify `from_date` and `to_date` to limit or widen your search. For example:
55
+
56
+ ```ruby
57
+ from_date = ::Date.new(2017, 02, 18)
58
+ to_date = ::Date.new(2017, 03, 18)
59
+ params = {:from_date => from_date, :to_date => to_date}
60
+
61
+ ::Atrium::Transaction.list_each(:user_guid => "USR-123", :query_params => params) do |transaction|
62
+ puts transaction.description
63
+ end
64
+ ```
65
+
14
66
  ## Installation
15
67
 
16
68
  Add this line to your application's Gemfile:
@@ -1,6 +1,7 @@
1
1
  require "active_attr"
2
2
  require "httpclient"
3
3
  require "json"
4
+ require "atrium/pageable"
4
5
  require "atrium/paginate"
5
6
 
6
7
  require "atrium/account"
@@ -1,5 +1,6 @@
1
1
  module Atrium
2
2
  class Account
3
+ extend ::Atrium::Pageable
3
4
  include ::ActiveAttr::Model
4
5
 
5
6
  # ATTRIBUTES
@@ -32,13 +33,19 @@ module Atrium
32
33
  attribute :updated_at
33
34
  attribute :user_guid
34
35
 
35
- def self.list(user_guid:)
36
- endpoint = "/users/#{user_guid}/accounts"
37
- accounts_response = ::Atrium.client.make_request(:get, endpoint)
36
+ def self.list(options = {})
37
+ options = _account_pagination_options(options)
38
+ paginate(options)
39
+ end
40
+
41
+ def self.list_each(options = {})
42
+ options = _account_pagination_options(options)
43
+ paginate_each(options) { |account| yield account }
44
+ end
38
45
 
39
- accounts_response["accounts"].map do |account|
40
- ::Atrium::Account.new(account)
41
- end
46
+ def self.list_in_batches(options = {})
47
+ options = _account_pagination_options(options)
48
+ paginate_in_batches(options) { |batch| yield batch }
42
49
  end
43
50
 
44
51
  def self.read(user_guid:, account_guid:)
@@ -49,13 +56,32 @@ module Atrium
49
56
  ::Atrium::Account.new(account_params)
50
57
  end
51
58
 
52
- def transactions
53
- endpoint = "/users/#{user_guid}/accounts/#{guid}/transactions"
54
- account_transactions_response = ::Atrium.client.make_request(:get, endpoint)
59
+ def transactions(options = {})
60
+ options = _transaction_pagination_options(options)
61
+ self.class.paginate(options)
62
+ end
63
+
64
+ def each_transaction(options = {})
65
+ options = _transaction_pagination_options(options)
66
+ self.class.paginate_each(options) { |account| yield account }
67
+ end
55
68
 
56
- account_transactions_response["transactions"].map do |transaction|
57
- ::Atrium::Transaction.new(transaction)
58
- end
69
+ def transactions_in_batches(options = {})
70
+ options = _transaction_pagination_options(options)
71
+ self.class.paginate_in_batches(options) { |batch| yield batch }
72
+ end
73
+
74
+ def self._account_pagination_options(options)
75
+ user_guid = options.fetch(:user_guid)
76
+ endpoint = "/users/#{user_guid}/accounts"
77
+ options.merge(:endpoint => endpoint, :resource => "accounts")
78
+ end
79
+
80
+ private
81
+
82
+ def _transaction_pagination_options(options)
83
+ endpoint = "/users/#{user_guid}/accounts/#{guid}/transactions"
84
+ options.merge(:endpoint => endpoint, :resource => "transactions", :klass => ::Atrium::Transaction)
59
85
  end
60
86
  end
61
87
  end
@@ -1,7 +1,7 @@
1
1
  module Atrium
2
2
  class Member
3
+ extend ::Atrium::Pageable
3
4
  include ::ActiveAttr::Model
4
- include ::ActiveAttr::Attributes
5
5
 
6
6
  attribute :aggregated_at
7
7
  attribute :guid
@@ -25,13 +25,19 @@ module Atrium
25
25
  ::Atrium::Member.new(member_params)
26
26
  end
27
27
 
28
- def self.list(user_guid:)
29
- endpoint = "/users/#{user_guid}/members"
30
- members_response = ::Atrium.client.make_request(:get, endpoint)
28
+ def self.list(options = {})
29
+ options = _member_pagination_options(options)
30
+ paginate(options)
31
+ end
31
32
 
32
- members_response["members"].map do |member|
33
- ::Atrium::Member.new(member)
34
- end
33
+ def self.list_each(options = {})
34
+ options = _member_pagination_options(options)
35
+ paginate_each(options) { |member| yield member }
36
+ end
37
+
38
+ def self.list_in_batches(options = {})
39
+ options = _member_pagination_options(options)
40
+ paginate_in_batches(options) { |batch| yield batch }
35
41
  end
36
42
 
37
43
  def self.read(user_guid:, member_guid:)
@@ -45,13 +51,19 @@ module Atrium
45
51
  ##
46
52
  # INSTANCE METHODS
47
53
  #
48
- def accounts
49
- endpoint = "/users/#{user_guid}/members/#{guid}/accounts"
50
- accounts_response = ::Atrium.client.make_request(:get, endpoint)
54
+ def accounts(options = {})
55
+ options = _account_pagination_options(options)
56
+ self.class.paginate(options)
57
+ end
51
58
 
52
- accounts_response["accounts"].map do |account|
53
- ::Atrium::Account.new(account)
54
- end
59
+ def each_account(options = {})
60
+ options = _account_pagination_options(options)
61
+ self.class.paginate_each(options) { |account| yield account }
62
+ end
63
+
64
+ def accounts_in_batches(options = {})
65
+ options = _account_pagination_options(options)
66
+ self.class.paginate_in_batches(options) { |batch| yield batch }
55
67
  end
56
68
 
57
69
  def aggregate
@@ -120,13 +132,25 @@ module Atrium
120
132
  self
121
133
  end
122
134
 
123
- def transactions
124
- endpoint = "/users/#{user_guid}/members/#{guid}/transactions"
125
- transactions_response = ::Atrium.client.make_request(:get, endpoint)
135
+ def transactions(options = {})
136
+ options = _transaction_pagination_options(options)
137
+ self.class.paginate(options)
138
+ end
126
139
 
127
- transactions_response["transactions"].map do |transaction|
128
- ::Atrium::Transaction.new(transaction)
129
- end
140
+ def each_transaction(options = {})
141
+ options = _transaction_pagination_options(options)
142
+ self.class.paginate_each(options) { |transaction| yield transaction }
143
+ end
144
+
145
+ def transactions_in_batches(options = {})
146
+ options = _transaction_pagination_options(options)
147
+ self.class.paginate_in_batches(options) { |batch| yield batch }
148
+ end
149
+
150
+ def self._member_pagination_options(options)
151
+ user_guid = options.fetch(:user_guid)
152
+ endpoint = "/users/#{user_guid}/members"
153
+ options.merge(:endpoint => endpoint, :resource => "members")
130
154
  end
131
155
 
132
156
  ##
@@ -147,6 +171,11 @@ module Atrium
147
171
  ##
148
172
  # PRIVATE INSTANCE METHODS
149
173
  #
174
+ def _account_pagination_options(options)
175
+ endpoint = "/users/#{user_guid}/members/#{guid}/accounts"
176
+ options.merge(:endpoint => endpoint, :resource => "accounts", :klass => ::Atrium::Account)
177
+ end
178
+
150
179
  def member_body(params)
151
180
  {
152
181
  :member => {
@@ -165,5 +194,10 @@ module Atrium
165
194
  }
166
195
  }
167
196
  end
197
+
198
+ def _transaction_pagination_options(options)
199
+ endpoint = "/users/#{user_guid}/members/#{guid}/transactions"
200
+ options.merge(:endpoint => endpoint, :resource => "transactions", :klass => ::Atrium::Transaction)
201
+ end
168
202
  end
169
203
  end
@@ -0,0 +1,74 @@
1
+ module Atrium
2
+ class PaginationBatch < ::Array
3
+ attr_accessor :total_pages, :total_entries, :current_page, :records_per_page
4
+ end
5
+
6
+ module Pageable
7
+ DEFAULT_RECORDS_PER_PAGE = 25
8
+
9
+ def make_get_request(endpoint, query_params)
10
+ uri = endpoint + "?" + ::URI.encode_www_form(query_params.sort)
11
+ ::Atrium.client.make_request(:get, uri)
12
+ end
13
+
14
+ def paginate(options = {})
15
+ records_per_page = options.fetch(:records_per_page, DEFAULT_RECORDS_PER_PAGE)
16
+ results = PaginationBatch.new
17
+ paginate_in_batches(options.merge(:limit => records_per_page)) do |batch|
18
+ results = batch
19
+ end
20
+ results
21
+ end
22
+
23
+ def paginate_each(options = {})
24
+ paginate_in_batches(options) do |batch|
25
+ batch.each do |record|
26
+ yield record
27
+ end
28
+ end
29
+ end
30
+
31
+ def paginate_in_batches(options = {})
32
+ fail ::ArgumentError, "A block is required" unless block_given?
33
+
34
+ endpoint = options.fetch(:endpoint)
35
+ resource = options.fetch(:resource)
36
+ klass = options.fetch(:klass, self)
37
+
38
+ query_params = options.fetch(:query_params, {})
39
+ limit = options.fetch(:limit, nil)
40
+ current_page = options.fetch(:initial_page, 1)
41
+ records_per_page = options.fetch(:records_per_page, DEFAULT_RECORDS_PER_PAGE)
42
+
43
+ pagination_info_query_params = query_params.merge(:records_per_page => records_per_page)
44
+ pagination = pagination_info(endpoint, pagination_info_query_params)
45
+ total_pages = pagination["total_pages"]
46
+ total_entries = pagination["total_entries"]
47
+ # "total_pages > 1" check exists since some query_params only return 1 page
48
+ total_pages = limit / records_per_page if limit.present? && total_pages > 1
49
+
50
+ until current_page > total_pages
51
+ params = query_params.merge(:page => current_page, :records_per_page => records_per_page)
52
+ response = make_get_request(endpoint, params)
53
+
54
+ records = response[resource].map { |attributes| klass.new(attributes) }
55
+
56
+ batch = ::Atrium::PaginationBatch.new(records)
57
+ batch.total_pages = total_pages
58
+ batch.total_entries = total_entries
59
+ batch.current_page = current_page
60
+ batch.records_per_page = records_per_page
61
+ yield batch
62
+
63
+ current_page += 1
64
+ end
65
+
66
+ nil
67
+ end
68
+
69
+ def pagination_info(endpoint, query_params)
70
+ response = make_get_request(endpoint, query_params.merge(:page => 1))
71
+ response["pagination"]
72
+ end
73
+ end
74
+ end
@@ -1,5 +1,6 @@
1
1
  module Atrium
2
2
  class Transaction
3
+ extend ::Atrium::Pageable
3
4
  include ::ActiveAttr::Model
4
5
 
5
6
  attribute :account_guid
@@ -31,20 +32,31 @@ module Atrium
31
32
  attribute :updated_at
32
33
  attribute :user_guid
33
34
 
34
- def self.list(user_guid:)
35
- endpoint = "/users/#{user_guid}/transactions"
36
- raw_transactions = ::Atrium.client.make_request(:get, endpoint)
35
+ def self.list(options = {})
36
+ options = _transaction_pagination_options(options)
37
+ paginate(options)
38
+ end
39
+
40
+ def self.list_each(options = {})
41
+ options = _transaction_pagination_options(options)
42
+ paginate_each(options) { |transaction| yield transaction }
43
+ end
37
44
 
38
- raw_transactions["transactions"].map do |raw_transaction|
39
- ::Atrium::Transaction.new(raw_transaction)
40
- end
45
+ def self.list_in_batches(options = {})
46
+ options = _transaction_pagination_options(options)
47
+ paginate_in_batches(options) { |batch| yield batch }
41
48
  end
42
49
 
43
50
  def self.read(user_guid:, transaction_guid:)
44
51
  endpoint = "/users/#{user_guid}/transactions/#{transaction_guid}"
45
52
  raw_transaction = ::Atrium.client.make_request(:get, endpoint)
46
-
47
53
  ::Atrium::Transaction.new(raw_transaction["transaction"])
48
54
  end
55
+
56
+ def self._transaction_pagination_options(options)
57
+ user_guid = options.fetch(:user_guid)
58
+ endpoint = "/users/#{user_guid}/transactions"
59
+ options.merge(:endpoint => endpoint, :resource => "transactions")
60
+ end
49
61
  end
50
62
  end
@@ -1,5 +1,6 @@
1
1
  module Atrium
2
2
  class User
3
+ extend ::Atrium::Pageable
3
4
  include ::ActiveAttr::Model
4
5
 
5
6
  # ATTRIBUTES
@@ -20,13 +21,19 @@ module Atrium
20
21
  ::Atrium::User.new(user_params)
21
22
  end
22
23
 
23
- def self.list
24
- endpoint = "/users"
25
- users_response = ::Atrium.client.make_request(:get, endpoint)
24
+ def self.list(options = {})
25
+ options = _user_pagination_options(options)
26
+ paginate(options)
27
+ end
28
+
29
+ def self.list_each(options = {})
30
+ options = _user_pagination_options(options)
31
+ paginate_each(options) { |user| yield user }
32
+ end
26
33
 
27
- users_response["users"].map do |user|
28
- ::Atrium::User.new(user)
29
- end
34
+ def self.list_in_batches(options = {})
35
+ options = _user_pagination_options(options)
36
+ paginate_in_batches(options) { |batch| yield batch }
30
37
  end
31
38
 
32
39
  def self.read(guid:)
@@ -40,13 +47,19 @@ module Atrium
40
47
  ##
41
48
  # INSTANCE METHODS
42
49
  #
43
- def accounts
44
- endpoint = "/users/#{guid}/accounts"
45
- response = ::Atrium.client.make_request(:get, endpoint)
50
+ def accounts(options = {})
51
+ options = _account_pagination_options(options)
52
+ self.class.paginate(options)
53
+ end
46
54
 
47
- response["accounts"].map do |account|
48
- ::Atrium::Account.new(account)
49
- end
55
+ def each_account(options = {})
56
+ options = _account_pagination_options(options)
57
+ self.class.paginate_each(options) { |account| yield account }
58
+ end
59
+
60
+ def accounts_in_batches(options = {})
61
+ options = _account_pagination_options(options)
62
+ self.class.paginate_in_batches(options) { |batch| yield batch }
50
63
  end
51
64
 
52
65
  def delete
@@ -56,22 +69,34 @@ module Atrium
56
69
  self
57
70
  end
58
71
 
59
- def members
60
- endpoint = "/users/#{guid}/members"
61
- response = ::Atrium.client.make_request(:get, endpoint)
72
+ def members(options = {})
73
+ options = _member_pagination_options(options)
74
+ self.class.paginate(options)
75
+ end
62
76
 
63
- response["members"].map do |member|
64
- ::Atrium::Member.new(member)
65
- end
77
+ def each_member(options = {})
78
+ options = _member_pagination_options(options)
79
+ self.class.paginate_each(options) { |member| yield member }
66
80
  end
67
81
 
68
- def transactions
69
- endpoint = "/users/#{guid}/transactions"
70
- response = ::Atrium.client.make_request(:get, endpoint)
82
+ def members_in_batches(options = {})
83
+ options = _member_pagination_options(options)
84
+ self.class.paginate_in_batches(options) { |batch| yield batch }
85
+ end
71
86
 
72
- response["transactions"].map do |transaction|
73
- ::Atrium::Transaction.new(transaction)
74
- end
87
+ def transactions(options = {})
88
+ options = _transaction_pagination_options(options)
89
+ self.class.paginate(options)
90
+ end
91
+
92
+ def each_transaction(options = {})
93
+ options = _transaction_pagination_options(options)
94
+ self.class.paginate_each(options) { |transaction| yield transaction }
95
+ end
96
+
97
+ def transactions_in_batches(options = {})
98
+ options = _transaction_pagination_options(options)
99
+ self.class.paginate_in_batches(options) { |batch| yield batch }
75
100
  end
76
101
 
77
102
  def update(params)
@@ -84,8 +109,27 @@ module Atrium
84
109
  self
85
110
  end
86
111
 
112
+ def self._user_pagination_options(options)
113
+ options.merge(:endpoint => "/users", :resource => "users")
114
+ end
115
+
87
116
  private
88
117
 
118
+ def _account_pagination_options(options)
119
+ endpoint = "/users/#{guid}/accounts"
120
+ options.merge(:endpoint => endpoint, :resource => "accounts", :klass => ::Atrium::Account)
121
+ end
122
+
123
+ def _member_pagination_options(options)
124
+ endpoint = "/users/#{guid}/members"
125
+ options.merge(:endpoint => endpoint, :resource => "members", :klass => ::Atrium::Member)
126
+ end
127
+
128
+ def _transaction_pagination_options(options)
129
+ endpoint = "/users/#{guid}/transactions"
130
+ options.merge(:endpoint => endpoint, :resource => "transactions", :klass => ::Atrium::Transaction)
131
+ end
132
+
89
133
  def update_params(params)
90
134
  {
91
135
  :user => params
@@ -1,3 +1,3 @@
1
1
  module Atrium
2
- VERSION = "0.5.0".freeze
2
+ VERSION = "1.0.1".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atrium-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Carstens, Dan Jones, Zach Toolson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-07 00:00:00.000000000 Z
11
+ date: 2017-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_attr
@@ -176,6 +176,7 @@ files:
176
176
  - lib/atrium/error.rb
177
177
  - lib/atrium/institution.rb
178
178
  - lib/atrium/member.rb
179
+ - lib/atrium/pageable.rb
179
180
  - lib/atrium/paginate.rb
180
181
  - lib/atrium/ruby.rb
181
182
  - lib/atrium/transaction.rb
@@ -201,7 +202,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
201
202
  version: '0'
202
203
  requirements: []
203
204
  rubyforge_project:
204
- rubygems_version: 2.5.2
205
+ rubygems_version: 2.6.13
205
206
  signing_key:
206
207
  specification_version: 4
207
208
  summary: Ruby wrapper for the Atrium API by MX