Class: Usps::Imis::Api
- Inherits:
-
Object
- Object
- Usps::Imis::Api
- Includes:
- Requests
- Defined in:
- lib/usps/imis/api.rb
Overview
The core API wrapper
Constant Summary collapse
- AUTHENTICATION_PATH =
Endpoint for (re-)authentication requests
'Token'- QUERY_PATH =
Endpoint for IQA query requests
'api/Query'
Instance Attribute Summary collapse
-
#imis_id ⇒ Object
Currently selected iMIS ID for API requests.
-
#lock_imis_id ⇒ Object
readonly
Whether to lock changes to the selected iMIS ID.
-
#token ⇒ Object
readonly
API bearer token.
-
#token_expiration ⇒ Object
readonly
Expiration time for the API bearer token.
Instance Method Summary collapse
-
#business_object(business_object_name, url_id: nil) ⇒ Object
An instance of
BusinessObject, using this instance as its parentApi. -
#imis_id_for(certificate) ⇒ String
Convert a member’s certificate number into an iMIS ID number.
-
#initialize(skip_authentication: false, imis_id: nil) ⇒ Api
constructor
A new instance of
Api. -
#instance_variables_to_inspect ⇒ Object
Ruby 3.5 instance variable filter.
-
#mapper ⇒ Object
An instance of
Mapper, using this instance as its parentApi. -
#on(business_object_name, url_id: nil) ⇒ Object
Run requests as DSL, with specific
BusinessObjectonly maintained for this scope. -
#panels ⇒ Object
Convenience accessor for available Panel objects, each using this instance as its parent
Api. -
#query(query_name, query_params = {}) ⇒ Hash
Run an IQA Query.
-
#query_all(query_name, query_params = {}) ⇒ Array<Hash>
Run an IQA Query, paging through all responses.
-
#update(data) ⇒ Object
Convenience alias for updating mapped fields.
-
#with(id) ⇒ Object
Run requests as DSL, with specific iMIS ID only maintained for this scope.
Constructor Details
#initialize(skip_authentication: false, imis_id: nil) ⇒ Api
A new instance of Api
41 42 43 44 |
# File 'lib/usps/imis/api.rb', line 41 def initialize(skip_authentication: false, imis_id: nil) authenticate unless skip_authentication self.imis_id = imis_id if imis_id end |
Instance Attribute Details
#imis_id ⇒ Object
Currently selected iMIS ID for API requests
30 31 32 |
# File 'lib/usps/imis/api.rb', line 30 def imis_id @imis_id end |
#lock_imis_id ⇒ Object (readonly)
Whether to lock changes to the selected iMIS ID
34 35 36 |
# File 'lib/usps/imis/api.rb', line 34 def lock_imis_id @lock_imis_id end |
#token ⇒ Object (readonly)
API bearer token
20 21 22 |
# File 'lib/usps/imis/api.rb', line 20 def token @token end |
#token_expiration ⇒ Object (readonly)
Expiration time for the API bearer token
Used to automatically re-authenticate as needed
26 27 28 |
# File 'lib/usps/imis/api.rb', line 26 def token_expiration @token_expiration end |
Instance Method Details
#business_object(business_object_name, url_id: nil) ⇒ Object
An instance of BusinessObject, using this instance as its parent Api
140 141 142 |
# File 'lib/usps/imis/api.rb', line 140 def business_object(business_object_name, url_id: nil) BusinessObject.new(self, business_object_name, url_id:) end |
#imis_id_for(certificate) ⇒ String
Convert a member’s certificate number into an iMIS ID number
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/usps/imis/api.rb', line 62 def imis_id_for(certificate) raise Error::ApiError, 'Cannot change iMIS ID while locked' if lock_imis_id begin result = query(Imis.configuration.imis_id_query_name, { certificate: }) @imis_id = result['Items']['$values'][0]['ID'] rescue StandardError raise Error::ApiError, 'Member not found' end end |
#instance_variables_to_inspect ⇒ Object
Ruby 3.5 instance variable filter
184 |
# File 'lib/usps/imis/api.rb', line 184 def instance_variables_to_inspect = %i[@token_expiration @imis_id] |
#mapper ⇒ Object
An instance of Mapper, using this instance as its parent Api
162 163 164 |
# File 'lib/usps/imis/api.rb', line 162 def mapper @mapper ||= Mapper.new(self) end |
#on(business_object_name, url_id: nil) ⇒ Object
Run requests as DSL, with specific BusinessObject only maintained for this scope
If no block is given, this returns the specified BusinessObject.
151 152 153 154 155 156 157 158 |
# File 'lib/usps/imis/api.rb', line 151 def on(business_object_name, url_id: nil, &) object = business_object(business_object_name, url_id:) return object unless block_given? result = nil object.tap { |obj| result = obj.instance_eval(&) } result end |
#panels ⇒ Object
Convenience accessor for available Panel objects, each using this instance as its parent Api
175 176 177 178 179 180 |
# File 'lib/usps/imis/api.rb', line 175 def panels @panels ||= Struct.new(:vsc, :education).new( Panel::Vsc.new(self), Panel::Education.new(self) ) end |
#query(query_name, query_params = {}) ⇒ Hash
Run an IQA Query
107 108 109 110 111 112 113 114 |
# File 'lib/usps/imis/api.rb', line 107 def query(query_name, query_params = {}) query_params[:QueryName] = query_name path = "#{QUERY_PATH}?#{query_params.to_query}" uri = URI(File.join(Imis.configuration.hostname, path)) request = Net::HTTP::Get.new(uri) result = submit(uri, (request)) JSON.parse(result.body) end |
#query_all(query_name, query_params = {}) ⇒ Array<Hash>
Run an IQA Query, paging through all responses
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/usps/imis/api.rb', line 123 def query_all(query_name, query_params = {}) response = query(query_name, **query_params) results = response['Items']['$values'] while response['HasNext'] response = query(query_name, **query_params, Offset: response['NextOffset']) results += response['Items']['$values'] end results end |
#update(data) ⇒ Object
Convenience alias for updating mapped fields
168 169 170 |
# File 'lib/usps/imis/api.rb', line 168 def update(data) mapper.update(data) end |
#with(id) ⇒ Object
Run requests as DSL, with specific iMIS ID only maintained for this scope
While in this block, changes to the value of imis_id are not allowed
If no block is given, this sets the iMIS ID and returns self.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/usps/imis/api.rb', line 86 def with(id, &) old_id = imis_id self.imis_id = id return self unless block_given? @lock_imis_id = true instance_eval(&) ensure if block_given? @lock_imis_id = false self.imis_id = old_id end end |