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, 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



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

def initialize(api, query_name, query_params)
  @api = api
  @query_name = query_name
  @query_params = query_params
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

#offsetObject (readonly)

Current offset for paging through the Query



29
30
31
# File 'lib/usps/imis/query.rb', line 29

def offset
  @offset
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



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

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

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

#find_eachObject

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/usps/imis/query.rb', line 55

def find_each(&)
  result = { 'HasNext' => true }
  count = 0

  while result['HasNext']
    Imis.logger.info 'Fetching IQA Query page'

    result = fetch

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

    items = result['Items']['$values'].map { it.except('$type') }
    @offset = result['NextOffset']

    items.each(&)
  end

  nil
end