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'
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.
-
#logger ⇒ Object
readonly
Tagged logger.
-
#token ⇒ Object
readonly
API bearer token.
-
#token_expiration ⇒ Object
readonly
Expiration time for the API bearer token.
Class Method Summary collapse
-
.with_token(token) ⇒ Object
Create a new instance of
Apiand provide an existing auth token.
Instance Method Summary collapse
-
#auth_token ⇒ Object
Used by the CLI wrappers to reduce authentication requests.
-
#business_objects ⇒ Hash<Symbol, Array<String>>
List of available Business Object names.
-
#fetch(field_key) ⇒ Object
(also: #[])
Convenience alias for reading mapped fields.
-
#fetch_all(*fields) ⇒ Array
Convenience alias for reading multiple mapped fields.
-
#imis_id_for(certificate) ⇒ Integer
Convert a member’s certificate number into an iMIS ID number.
-
#initialize(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, ordinal: nil) ⇒ Usps::Imis::BusinessObject
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. -
#put_field(field_key, value) ⇒ Array
(also: #[]=)
Convenience alias for updating mapped fields.
-
#query(query_name, query_params = nil) ⇒ Usps::Imis::Query
Build a Query interface.
-
#update(data) ⇒ Usps::Imis::Data
Convenience alias for updating mapped fields.
-
#with(id = nil, certificate: nil) ⇒ Usps::Imis::Api
Run requests as DSL, with specific iMIS ID only maintained for this scope.
Constructor Details
#initialize(imis_id: nil) ⇒ Api
A new instance of Api
56 57 58 59 60 |
# File 'lib/usps/imis/api.rb', line 56 def initialize(imis_id: nil) self.imis_id = imis_id if imis_id @logger ||= Imis.logger('Api') Imis.config.validate! end |
Instance Attribute Details
#imis_id ⇒ Object
Currently selected iMIS ID for API requests
31 32 33 |
# File 'lib/usps/imis/api.rb', line 31 def imis_id @imis_id end |
#lock_imis_id ⇒ Object (readonly)
Whether to lock changes to the selected iMIS ID
35 36 37 |
# File 'lib/usps/imis/api.rb', line 35 def lock_imis_id @lock_imis_id end |
#logger ⇒ Object (readonly)
Tagged logger
39 40 41 |
# File 'lib/usps/imis/api.rb', line 39 def logger @logger end |
#token ⇒ Object (readonly)
API bearer token
21 22 23 |
# File 'lib/usps/imis/api.rb', line 21 def token @token end |
#token_expiration ⇒ Object (readonly)
Expiration time for the API bearer token
Used to automatically re-authenticate as needed
27 28 29 |
# File 'lib/usps/imis/api.rb', line 27 def token_expiration @token_expiration end |
Class Method Details
.with_token(token) ⇒ Object
Create a new instance of Api and provide an existing auth token
45 46 47 48 49 50 |
# File 'lib/usps/imis/api.rb', line 45 def self.with_token(token) new.tap do |api| api.instance_variable_set(:@token, token) api.instance_variable_set(:@token_expiration, Time.now + 3600) # Greater than the actual lifetime of the token end end |
Instance Method Details
#auth_token ⇒ Object
Used by the CLI wrappers to reduce authentication requests
207 208 209 210 211 |
# File 'lib/usps/imis/api.rb', line 207 def auth_token authenticate { token: @token, token_expiration: @token_expiration } end |
#business_objects ⇒ Hash<Symbol, Array<String>>
List of available Business Object names
192 193 194 195 196 |
# File 'lib/usps/imis/api.rb', line 192 def business_objects abc, other = query('BOEntityDefinition').map(&:entity).partition { it.include?('ABC') } { abc:, other: } end |
#fetch(field_key) ⇒ Object Also known as: []
Convenience alias for reading mapped fields
166 |
# File 'lib/usps/imis/api.rb', line 166 def fetch(field_key) = mapper.fetch(field_key) |
#fetch_all(*fields) ⇒ Array
Convenience alias for reading multiple mapped fields
173 |
# File 'lib/usps/imis/api.rb', line 173 def fetch_all(*fields) = mapper.fetch_all(*fields) |
#imis_id_for(certificate) ⇒ Integer
Convert a member’s certificate number into an iMIS ID number
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/usps/imis/api.rb', line 80 def imis_id_for(certificate) raise Errors::LockedIdError if lock_imis_id logger.debug "Fetching iMIS ID for #{certificate}" begin result = query(Imis.configuration.imis_id_query_name, { certificate: }) page = result.page.tap { logger.tagged('Response').debug it } self.imis_id = page.first['ID'].to_i rescue StandardError raise Errors::NotFoundError, 'Member not found' end end |
#instance_variables_to_inspect ⇒ Object
Ruby 3.5 instance variable filter
215 |
# File 'lib/usps/imis/api.rb', line 215 def instance_variables_to_inspect = i[@token_expiration @imis_id] |
#mapper ⇒ Object
An instance of Mapper, using this instance as its parent Api
158 159 160 |
# File 'lib/usps/imis/api.rb', line 158 def mapper @mapper ||= Mapper.new(self) end |
#on(business_object_name, ordinal: nil) ⇒ Usps::Imis::BusinessObject
Run requests as DSL, with specific BusinessObject only maintained for this scope
If no block is given, this returns the specified BusinessObject.
149 150 151 152 153 154 |
# File 'lib/usps/imis/api.rb', line 149 def on(business_object_name, ordinal: nil, &) object = BusinessObject.new(self, business_object_name, ordinal:) return object unless block_given? object.instance_eval(&) end |
#panels ⇒ Object
Convenience accessor for available Panel objects, each using this instance as its parent Api
201 202 203 |
# File 'lib/usps/imis/api.rb', line 201 def panels @panels ||= Panels.all(self) end |
#put_field(field_key, value) ⇒ Array Also known as: []=
Convenience alias for updating mapped fields
179 |
# File 'lib/usps/imis/api.rb', line 179 def put_field(field_key, value) = update(field_key => value) |
#query(query_name, query_params = nil) ⇒ Usps::Imis::Query
Build a Query interface
Works with IQA queries and Business Objects
138 |
# File 'lib/usps/imis/api.rb', line 138 def query(query_name, query_params = nil) = Query.new(self, query_name, **query_params) |
#update(data) ⇒ Usps::Imis::Data
Convenience alias for updating mapped fields
186 |
# File 'lib/usps/imis/api.rb', line 186 def update(data) = mapper.update(data) |
#with(id = nil, certificate: nil) ⇒ Usps::Imis::Api
Run requests as DSL, with specific iMIS ID only maintained for this scope
Either id or certificate is required.
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.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/usps/imis/api.rb', line 112 def with(id = nil, certificate: nil, &) raise ArgumentError, 'Must provide id or certificate' unless id || certificate old_id = imis_id id.nil? ? imis_id_for(certificate) : 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 |