Class: Chain::Query
Direct Known Subclasses
AccessToken::ClientModule::Query, Account::Query, Asset::Query, Balance::Query, MockHSM::Key::Query, Transaction::Query, TransactionFeed::Query, UnspentOutput::Query
Instance Attribute Summary collapse
- #client ⇒ Client readonly
Instance Method Summary collapse
-
#each ⇒ void
Iterate through objects in response, fetching the next page of results from the API as needed.
- #fetch(query) ⇒ Object abstract
-
#initialize(client, first_query = {}) ⇒ Query
constructor
A new instance of Query.
-
#translate(response_object) ⇒ Object
abstract
Overwrite to translate API response data to a different Ruby object.
Constructor Details
#initialize(client, first_query = {}) ⇒ Query
Returns a new instance of Query.
8 9 10 11 |
# File 'lib/chain/query.rb', line 8 def initialize(client, first_query = {}) @client = client @first_query = first_query end |
Instance Attribute Details
Instance Method Details
#each ⇒ void
This method returns an undefined value.
Iterate through objects in response, fetching the next page of results from the API as needed.
Implements required method Enumerable#each.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/chain/query.rb', line 19 def each page = fetch(@first_query) loop do if page['items'].empty? # we consume this array as we iterate break if page['last_page'] page = fetch(page['next']) # The second predicate (empty?) *should* be redundant, but we check it # anyway as a defensive measure. break if page['items'].empty? end item = page['items'].shift yield translate(item) end end |
#fetch(query) ⇒ Object
This method is abstract.
38 39 40 |
# File 'lib/chain/query.rb', line 38 def fetch(query) raise NotImplementedError end |
#translate(response_object) ⇒ Object
This method is abstract.
Overwrite to translate API response data to a different Ruby object.
44 45 46 |
# File 'lib/chain/query.rb', line 44 def translate(response_object) raise NotImplementedError end |