Class: Usps::Imis::Query

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Requests
Defined in:
lib/usps/imis/query.rb

Overview

API wrapper for IQA Queries

Constant Summary collapse

QUERY_PATH =

Endpoint for IQA query requests

'api/Query'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, query_name, page_size: 100, offset: nil, **query_params) ⇒ Query

A new instance of Query

Parameters:

  • api (Api)

    Parent to use for making requests

  • query_name (String)

    Full path of the query in IQA, e.g. $/_ABC/Fiander/iMIS_ID

  • page_size (Integer) (defaults to: 100)

    Number of records to return on each request page

  • offset (Integer) (defaults to: nil)

    Offset index of records to return on next request page

  • query_params (Hash)

    Conforms to pattern { param_name => param_value }



47
48
49
50
51
52
53
# File 'lib/usps/imis/query.rb', line 47

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
end

Instance Attribute Details

#apiObject (readonly)

The parent Api object



17
18
19
# File 'lib/usps/imis/query.rb', line 17

def api
  @api
end

#countObject (readonly)

Count of records processed



37
38
39
# File 'lib/usps/imis/query.rb', line 37

def count
  @count
end

#offsetObject

Current offset for paging through the Query



33
34
35
# File 'lib/usps/imis/query.rb', line 33

def offset
  @offset
end

#page_sizeObject

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_nameObject (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_paramsObject (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

#eachObject

Iterate through all results from the query



57
58
59
60
61
62
63
# File 'lib/usps/imis/query.rb', line 57

def each(&)
  logger.info 'Running IQA Query on iMIS'

  items = []
  find_each { items << it }
  items.each(&)
end

#fetchObject

Fetch a raw query page



81
# File 'lib/usps/imis/query.rb', line 81

def fetch = JSON.parse(submit(uri, authorize(http_get)).body)

#fetch_nextObject

Fetch the next raw query page, and update the current offset



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/usps/imis/query.rb', line 85

def fetch_next
  logger.info 'Fetching IQA Query page'

  result = fetch

  @count += result['Count'] || 0
  total = result['TotalCount']
  logger.info "  -> #{@count} / #{total} #{'item'.pluralize(total)}"
  logger.debug '  -> Query page data:'
  JSON.pretty_generate(result).split("\n").each { logger.debug "    -> #{it}" }

  @offset = result['NextOffset']

  result
end

#find_eachObject

Iterate through all results from the query, fetching one page at a time



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/usps/imis/query.rb', line 67

def find_each(&)
  result = reset!

  while result['HasNext']
    result = fetch_next.tap do |result_page|
      result_page['Items']['$values'].map { it.except('$type') }.each(&)
    end
  end

  nil
end

#instance_variables_to_inspectObject

Ruby 3.5 instance variable filter



103
# File 'lib/usps/imis/query.rb', line 103

def instance_variables_to_inspect = instance_variables - %i[@api]