cubits 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: d337170a2395378547f050001109b90f2387bb58
4
- data.tar.gz: 8b505f0edeb55b9a5c1a518f8dad387a9e167f42
3
+ metadata.gz: f5511ab4b3447612a3c1b70d5c56b6990ad321cc
4
+ data.tar.gz: 014d343a1b5cb390dc820fd6cb81e0fc5fd574fd
5
5
  SHA512:
6
- metadata.gz: b2c5625fe8ae836a0fb521e2b423eed6af3eca3c95d44988b8497bb2710201ea0a176ef81cabd2155c3ff04deda7f164d11fab2dc4d8f81b6a9b41de373dbc88
7
- data.tar.gz: 9327d05bb549d9478ea3d03112fc98d4dfa9cde73c3f1dc83e9fab0e054e15bea76bc098f21cc9c3291330645ab5cb3b45b99884586418b164f900d96088d31b
6
+ metadata.gz: 0b6f04d5f24a2a85ad94bfe4631cde2084e1d904b9afb7647e17183f21df82e474da51239d9d6dd572b9f50a7b740573340e916c8b3d6a0e992c25c1bf75d2e3
7
+ data.tar.gz: a6493083f990beab6a4908bdda55a80ae3460774518547a777bc8cd2e32c74560f50696588fd43968d6c11f4d5d60cbbc9ead7a0f061fb652e8976ff6b3585a2
data/CHANGES.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.6.0
2
+
3
+ * Added Cubits::ResourceCollection, which implements Ruby [Enumerable](http://ruby-doc.org/core-2.2.1/Enumerable.html)
4
+ plus some helper methods
5
+ * `Cubits::Channel#txs` now is a `Cubits::ResourceCollection` of `Cubits::Channel::Tx`
6
+
1
7
  # 0.5.0
2
8
 
3
9
  * Added Cubits::Channel::Tx collection implementation.
data/README.md CHANGED
@@ -147,24 +147,85 @@ channel.reference # => "CHAN_192357"
147
147
 
148
148
  #### #txs
149
149
 
150
- Returns channel transactions as a collection of `Cubits::Channel::Tx` objects.
150
+ Returns channel transactions as `Cubits::ResourceCollection` of `Cubits::Channel::Tx` objects.
151
151
 
152
- The collection exposes methods `.all` and `.find()`:
152
+ ```ruby
153
+ channel = Cubits::Channel.find("d17ad6c96f83162a2764ecd4739d7ab2")
154
+ channel.txs.count # => 42
155
+ ```
156
+
157
+ ### Cubits::ResourceCollection
158
+
159
+ The `Cubits::ResourceCollection` object represents a collection of resources of a particular type:
160
+
161
+ ```ruby
162
+ channel = Cubits::Channel.find("d17ad6c96f83162a2764ecd4739d7ab2")
163
+ channel.txs # => <Cubits::ResourceCollection of Cubits::Channel::Tx ...>
164
+ ```
165
+
166
+ `Cubits::ResourceCollection` implements Ruby [Enumerable](http://ruby-doc.org/core-2.2.1/Enumerable.html) interface, and therefore exposes all the basic iterators (`#each`, `#select`, `#map`) as well as [many other methods](http://ruby-doc.org/core-2.2.1/Enumerable.html).
167
+
168
+ In addition to that, some helper methods are implemented:
169
+
170
+ #### #count
171
+
172
+ Returns number of elements in the collection:
173
+ ```ruby
174
+ channel = Cubits::Channel.find("d17ad6c96f83162a2764ecd4739d7ab2")
175
+ channel.txs.count # => 42
176
+ ```
177
+
178
+ #### #all
179
+
180
+ Returns all the elements of the collection as an `Array`:
181
+ ```ruby
182
+ channel = Cubits::Channel.find("d17ad6c96f83162a2764ecd4739d7ab2")
183
+ channel.txs.all # => [{ "tx_ref_code"=>"YYWZN",
184
+ # "state"=>"pending",
185
+ # "channel_id"=>"d17ad6c96f83162a2764ecd4739d7ab2",
186
+ # ...
187
+ # },
188
+ # ...
189
+ # ]
190
+ ```
191
+
192
+ #### #first
193
+
194
+ Returns the first element of the collection:
195
+ ```ruby
196
+ channel = Cubits::Channel.find("d17ad6c96f83162a2764ecd4739d7ab2")
197
+ channel.txs.first # => { "tx_ref_code"=>"YYWZN", "state" => "pending", ... }
198
+ ```
199
+
200
+ #### #last
153
201
 
154
- Listing all channel transactions:
202
+ Returns the last element of the collection:
155
203
  ```ruby
156
204
  channel = Cubits::Channel.find("d17ad6c96f83162a2764ecd4739d7ab2")
205
+ channel.txs.last # => { "tx_ref_code"=>"WWZKX", "state" => "completed", ... }
206
+ ```
207
+
208
+ #### #find(*id*)
209
+
210
+ Finds and returns an element in the collection with a given *id* (or *tx_ref_code* in case of Cubits::Channel::Tx), or `nil` if the element cannot be found.
157
211
 
158
- channel.txs.all # => [{"tx_ref_code"=>"YYWZN", ...}, ...]
212
+ ```ruby
213
+ channel = Cubits::Channel.find("d17ad6c96f83162a2764ecd4739d7ab2")
214
+ channel.txs.find("PGDAV") # => { "tx_ref_code"=>"PGDAV", "state" => "completed", ... }
159
215
  ```
160
216
 
161
- Retrieving a channel transaction with given *tx_ref_code*:
217
+ #### #reload
218
+
219
+ Reloads collection. Resets preloaded elements and element count.
220
+
162
221
  ```ruby
163
222
  channel = Cubits::Channel.find("d17ad6c96f83162a2764ecd4739d7ab2")
164
- tx = channel.txs.find("YYWZN")
165
- tx.class # => Cubits::Channel::Tx
223
+ channel.txs.count # => 42
224
+
225
+ # ... new transaction is created
166
226
 
167
- tx # => {"tx_ref_code"=>"YYWZN", "state"=>"pending", ... }
227
+ channel.txs.count # => 42
228
+ channel.txs.reload.count # => 43
168
229
  ```
169
230
 
170
231
  ### Cubits::Channel::Tx
@@ -1,5 +1,7 @@
1
1
  module Cubits
2
2
  class ResourceCollection
3
+ include Enumerable
4
+
3
5
  attr_reader :path, :resource
4
6
 
5
7
  def initialize(params = {})
@@ -20,6 +22,12 @@ module Cubits
20
22
  (@exposed_methods || []).include?(method_name)
21
23
  end
22
24
 
25
+ # Returns number of elements in the collection
26
+ #
27
+ def count
28
+ pagination.total_count
29
+ end
30
+ alias_method :size, :count
23
31
 
24
32
  # Loads collection of resources
25
33
  #
@@ -27,9 +35,25 @@ module Cubits
27
35
  #
28
36
  def all
29
37
  fail NoMethodError, "Resource #{name} does not expose .all" unless exposed_method?(:all)
30
- Cubits.connection.get(path_to, per_page: 1000)[collection_name].map { |r| resource.new r }
38
+ list = []
39
+ page_count.times do |i|
40
+ list += page(i + 1)
41
+ end
42
+ list
31
43
  rescue NotFound
32
- nil
44
+ []
45
+ end
46
+
47
+ # Returns first element of the collection
48
+ #
49
+ def first
50
+ first_page.first
51
+ end
52
+
53
+ # Returns last element of the collection
54
+ #
55
+ def last
56
+ last_page.last
33
57
  end
34
58
 
35
59
  # Loads resource
@@ -45,6 +69,27 @@ module Cubits
45
69
  nil
46
70
  end
47
71
 
72
+ # Reloads collection
73
+ #
74
+ # @return [self]
75
+ #
76
+ def reload
77
+ @pagination = nil
78
+ @pages = nil
79
+ self
80
+ end
81
+
82
+ # Iterates through the collection, yielding the giving block for each resource.
83
+ #
84
+ def each(&_block)
85
+ return to_enum unless block_given?
86
+ page_count.times do |i|
87
+ page(i + 1).each do |r|
88
+ yield r
89
+ end
90
+ end
91
+ end
92
+
48
93
  # Returns API path to resource
49
94
  #
50
95
  def path_to(resource_or_id = nil)
@@ -63,7 +108,61 @@ module Cubits
63
108
  end
64
109
 
65
110
  def to_s
66
- "<#{self.class.name}:<#{resource.name}>:#{path}>"
111
+ "<#{self.class.name} of #{resource.name}, #{path}>"
112
+ end
113
+
114
+ private
115
+
116
+ # Returns current pagination object.
117
+ # If pagination was not requested yet, does a one page request
118
+ #
119
+ def pagination
120
+ return @pagination if @pagination
121
+ first_page
122
+ @pagination
123
+ end
124
+
125
+ # Returns number of pages
126
+ #
127
+ def page_count
128
+ pagination.page_count
129
+ end
130
+
131
+ # Returns collection of pages as a Hash:
132
+ # {
133
+ # <page_number> => [<resource>, ...],
134
+ # <page_number> => [<resource>, ...],
135
+ # ...
136
+ # }
137
+ #
138
+ def pages
139
+ @pages ||= {}
140
+ end
141
+
142
+ # Returns i-th page
143
+ #
144
+ def page(i)
145
+ pages[i] ||= load_page(i)
146
+ end
147
+
148
+ # Loads i-th page
149
+ #
150
+ def load_page(i)
151
+ response = Cubits.connection.get(path_to, page: i)
152
+ @pagination = Hashie::Mash.new(response['pagination'])
153
+ response[collection_name].map { |r| resource.new r }
154
+ end
155
+
156
+ # Returns first page of the collection
157
+ #
158
+ def first_page
159
+ page(1)
160
+ end
161
+
162
+ # Returns last page of the collection
163
+ #
164
+ def last_page
165
+ page(page_count)
67
166
  end
68
167
  end # class ResourceCollection
69
168
  end # module Cubits
@@ -1,3 +1,3 @@
1
1
  module Cubits
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cubits
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
  - Alex Kukushkin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-01 00:00:00.000000000 Z
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http