Class: Usps::Imis::Query
- Inherits:
-
Object
- Object
- Usps::Imis::Query
- Includes:
- Enumerable, Requests
- Defined in:
- lib/usps/imis/query.rb
Overview
API wrapper for IQA and Business Object Queries
Constant Summary collapse
- IQA_PATH =
Endpoint for IQA query requests
'api/Query'
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
The parent
Apiobject. -
#count ⇒ Object
readonly
Count of records processed.
-
#logger ⇒ Object
readonly
Tagged logger.
-
#next_page ⇒ Object
readonly
Whether the current query has a next page.
-
#offset ⇒ Object
Current offset for paging through the Query.
-
#page_size ⇒ Object
Current page size for paging through the Query.
-
#query_name ⇒ Object
readonly
Name of the Query to run.
-
#query_params ⇒ Object
readonly
Parameters for the Query.
Instance Method Summary collapse
-
#each ⇒ Object
Iterate through all results from the query.
-
#fetch ⇒ Object
Fetch a raw query page.
-
#fetch_next ⇒ Object
Fetch the next raw query page, and update the current offset.
-
#find_each ⇒ Object
Iterate through all results from the query, fetching one page at a time.
-
#initialize(api, query_name, page_size: 100, offset: nil, **query_params) ⇒ Query
constructor
A new instance of
Query. -
#instance_variables_to_inspect ⇒ Object
Ruby 3.5 instance variable filter.
-
#page ⇒ Object
Fetch a filtered query page, and update the current offset.
-
#reset! ⇒ Object
Reset query paging progress.
Constructor Details
#initialize(api, query_name, page_size: 100, offset: nil, **query_params) ⇒ Query
A new instance of Query
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/usps/imis/query.rb', line 55 def initialize(api, query_name, page_size: 100, offset: nil, **query_params) @api = api @query_name = query_name @query_params = query_params @page_size = page_size @offset = offset @count = 0 @logger ||= Imis.logger('Query', query_type) logger.tagged('Name').debug query_name logger.tagged('Params').json query_params logger.tagged('URI').debug uri logger.tagged('Page Size').debug page_size logger.tagged('Offset').debug offset.to_i end |
Instance Attribute Details
#api ⇒ Object (readonly)
The parent Api object
17 18 19 |
# File 'lib/usps/imis/query.rb', line 17 def api @api end |
#count ⇒ Object (readonly)
Count of records processed
37 38 39 |
# File 'lib/usps/imis/query.rb', line 37 def count @count end |
#logger ⇒ Object (readonly)
Tagged logger
45 46 47 |
# File 'lib/usps/imis/query.rb', line 45 def logger @logger end |
#next_page ⇒ Object (readonly)
Whether the current query has a next page
41 42 43 |
# File 'lib/usps/imis/query.rb', line 41 def next_page @next_page end |
#offset ⇒ Object
Current offset for paging through the Query
33 34 35 |
# File 'lib/usps/imis/query.rb', line 33 def offset @offset end |
#page_size ⇒ Object
Current page size for paging through the Query
29 30 31 |
# File 'lib/usps/imis/query.rb', line 29 def page_size @page_size end |
#query_name ⇒ Object (readonly)
Name of the Query to run
21 22 23 |
# File 'lib/usps/imis/query.rb', line 21 def query_name @query_name end |
#query_params ⇒ Object (readonly)
Parameters for the Query
25 26 27 |
# File 'lib/usps/imis/query.rb', line 25 def query_params @query_params end |
Instance Method Details
#each ⇒ Object
Iterate through all results from the query
73 74 75 76 77 78 79 |
# File 'lib/usps/imis/query.rb', line 73 def each(&) logger.info 'Running' items = [] find_each { items << it } items.each(&) end |
#fetch ⇒ Object
Fetch a raw query page
116 |
# File 'lib/usps/imis/query.rb', line 116 def fetch = JSON.parse(submit(uri, (http_get)).body) |
#fetch_next ⇒ Object
Fetch the next raw query page, and update the current offset
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/usps/imis/query.rb', line 95 def fetch_next return unless page? logger.info "Fetching #{query_type} Query page" result = fetch @count += result['Count'] || 0 total = result['TotalCount'] logger.info "#{@count} / #{total} #{'item'.pluralize(total)}" logger.debug 'Query page data:' logger.json result @offset = result['NextOffset'] @next_page = result['HasNext'] result end |
#find_each ⇒ Object
Iterate through all results from the query, fetching one page at a time
83 84 85 86 87 |
# File 'lib/usps/imis/query.rb', line 83 def find_each(&) reset! page.each(&) while page? nil end |
#instance_variables_to_inspect ⇒ Object
Ruby 3.5 instance variable filter
132 |
# File 'lib/usps/imis/query.rb', line 132 def instance_variables_to_inspect = instance_variables - %i[@api @logger] |
#page ⇒ Object
Fetch a filtered query page, and update the current offset
91 |
# File 'lib/usps/imis/query.rb', line 91 def page = fetch_next['Items']['$values'].map { iqa? ? it.except('$type') : Imis::Data[it] } |
#reset! ⇒ Object
Reset query paging progress
120 121 122 123 124 125 126 127 128 |
# File 'lib/usps/imis/query.rb', line 120 def reset! return if next_page.nil? logger.debug 'Resetting progress' @count = 0 @offset = 0 @next_page = nil end |