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.
-
#token ⇒ Object
readonly
API bearer token.
-
#token_expiration ⇒ Object
readonly
Expiration time for the API bearer token.
Instance Method Summary collapse
-
#fetch(field_key) ⇒ Object
(also: #[])
Convenience alias for reading mapped fields.
-
#fetch_all(*fields) ⇒ Object
Convenience alias for reading multiple mapped fields.
-
#imis_id_for(certificate) ⇒ String
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) ⇒ 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. -
#put_field(field_key, value) ⇒ Object
(also: #[]=)
Convenience alias for updating mapped fields.
-
#query(query_name, query_params = {}) ⇒ Usps::Imis::Query
Build a Query interface.
-
#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(imis_id: nil) ⇒ Api
A new instance of Api
41 42 43 |
# File 'lib/usps/imis/api.rb', line 41 def initialize(imis_id: nil) self.imis_id = imis_id if imis_id 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 |
#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 |
Instance Method Details
#fetch(field_key) ⇒ Object Also known as: []
Convenience alias for reading mapped fields
135 |
# File 'lib/usps/imis/api.rb', line 135 def fetch(field_key) = mapper.fetch(field_key) |
#fetch_all(*fields) ⇒ Object
Convenience alias for reading multiple mapped fields
140 |
# File 'lib/usps/imis/api.rb', line 140 def fetch_all(*fields) = mapper.fetch_all(*fields) |
#imis_id_for(certificate) ⇒ String
Convert a member’s certificate number into an iMIS ID number
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/usps/imis/api.rb', line 61 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: }).tap { logger.debug it } page = result.page.tap { logger.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
160 |
# File 'lib/usps/imis/api.rb', line 160 def instance_variables_to_inspect = %i[@token_expiration @imis_id] |
#mapper ⇒ Object
An instance of Mapper, using this instance as its parent Api
129 130 131 |
# File 'lib/usps/imis/api.rb', line 129 def mapper @mapper ||= Mapper.new(self) end |
#on(business_object_name, ordinal: nil) ⇒ Object
Run requests as DSL, with specific BusinessObject only maintained for this scope
If no block is given, this returns the specified BusinessObject.
120 121 122 123 124 125 |
# File 'lib/usps/imis/api.rb', line 120 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
154 155 156 |
# File 'lib/usps/imis/api.rb', line 154 def panels @panels ||= Panels.all(self) end |
#put_field(field_key, value) ⇒ Object Also known as: []=
Convenience alias for updating mapped fields
144 |
# File 'lib/usps/imis/api.rb', line 144 def put_field(field_key, value) = update(field_key => value) |
#query(query_name, query_params = {}) ⇒ Usps::Imis::Query
Build a Query interface
Works with IQA queries and Business Objects
111 |
# File 'lib/usps/imis/api.rb', line 111 def query(query_name, query_params = {}) = Query.new(self, query_name, **query_params) |
#update(data) ⇒ Object
Convenience alias for updating mapped fields
149 |
# File 'lib/usps/imis/api.rb', line 149 def update(data) = mapper.update(data) |
#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.
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/usps/imis/api.rb', line 88 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 |