Class: Usps::Imis::Api
- Inherits:
-
Object
- Object
- Usps::Imis::Api
- Defined in:
- lib/usps/imis/api.rb
Overview
The core API wrapper
Constant Summary collapse
- AUTHENTICATION_PATH =
Endpoint for (re-)authentication requests
'Token'- API_PATH =
Endpoint for general API requests
'api'- 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
-
#delete(business_object_name, url_id: nil) ⇒ String
Remove a business object for the current member.
-
#get(business_object_name, url_id: nil) ⇒ Hash
Get a business object for the current member.
-
#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 parent
Api. -
#panels ⇒ Object
Convenience accessor for available Panel objects, each using this instance as its parent
Api. -
#post(business_object_name, body, url_id: nil) ⇒ Hash
Create a business object for the current member.
-
#put(business_object_name, body, url_id: nil) ⇒ Hash
Update a business object for the current member.
-
#put_fields(business_object_name, fields, url_id: nil) ⇒ Hash
Update only specific fields on a business object for the current member.
-
#query(query_name, query_params = {}) ⇒ Hash
Run an IQA Query.
-
#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
43 44 45 46 |
# File 'lib/usps/imis/api.rb', line 43 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
32 33 34 |
# File 'lib/usps/imis/api.rb', line 32 def imis_id @imis_id end |
#lock_imis_id ⇒ Object (readonly)
Whether to lock changes to the selected iMIS ID
36 37 38 |
# File 'lib/usps/imis/api.rb', line 36 def lock_imis_id @lock_imis_id end |
#token ⇒ Object (readonly)
API bearer token
22 23 24 |
# File 'lib/usps/imis/api.rb', line 22 def token @token end |
#token_expiration ⇒ Object (readonly)
Expiration time for the API bearer token
Used to automatically re-authenticate as needed
28 29 30 |
# File 'lib/usps/imis/api.rb', line 28 def token_expiration @token_expiration end |
Instance Method Details
#delete(business_object_name, url_id: nil) ⇒ String
Remove a business object for the current member
163 164 165 166 167 168 |
# File 'lib/usps/imis/api.rb', line 163 def delete(business_object_name, url_id: nil) uri = uri_for(business_object_name, url_id:) request = Net::HTTP::Delete.new(uri) result = submit(uri, (request)) result.body end |
#get(business_object_name, url_id: nil) ⇒ Hash
Get a business object for the current member
104 105 106 107 108 109 |
# File 'lib/usps/imis/api.rb', line 104 def get(business_object_name, url_id: nil) uri = uri_for(business_object_name, url_id:) request = Net::HTTP::Get.new(uri) result = submit(uri, (request)) JSON.parse(result.body) end |
#imis_id_for(certificate) ⇒ String
Convert a member’s certificate number into an iMIS ID number
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/usps/imis/api.rb', line 64 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
210 |
# File 'lib/usps/imis/api.rb', line 210 def instance_variables_to_inspect = %i[@token_expiration @imis_id] |
#mapper ⇒ Object
An instance of Mapper, using this instance as its parent Api
188 189 190 |
# File 'lib/usps/imis/api.rb', line 188 def mapper @mapper ||= Mapper.new(self) end |
#panels ⇒ Object
Convenience accessor for available Panel objects, each using this instance as its parent Api
195 196 197 198 199 200 |
# File 'lib/usps/imis/api.rb', line 195 def panels @panels ||= Struct.new(:vsc, :education).new( Panel::Vsc.new(self), Panel::Education.new(self) ) end |
#post(business_object_name, body, url_id: nil) ⇒ Hash
Create a business object for the current member
148 149 150 151 152 153 154 |
# File 'lib/usps/imis/api.rb', line 148 def post(business_object_name, body, url_id: nil) uri = uri_for(business_object_name, url_id:) request = Net::HTTP::Post.new(uri) request.body = JSON.dump(body) result = submit(uri, (request)) JSON.parse(result.body) end |
#put(business_object_name, body, url_id: nil) ⇒ Hash
Update a business object for the current member
132 133 134 135 136 137 138 |
# File 'lib/usps/imis/api.rb', line 132 def put(business_object_name, body, url_id: nil) uri = uri_for(business_object_name, url_id:) request = Net::HTTP::Put.new(uri) request.body = JSON.dump(body) result = submit(uri, (request)) JSON.parse(result.body) end |
#put_fields(business_object_name, fields, url_id: nil) ⇒ Hash
Update only specific fields on a business object for the current member
119 120 121 122 |
# File 'lib/usps/imis/api.rb', line 119 def put_fields(business_object_name, fields, url_id: nil) updated = filter_fields(business_object_name, fields) put(business_object_name, updated, url_id:) end |
#query(query_name, query_params = {}) ⇒ Hash
Run an IQA Query
177 178 179 180 181 182 183 184 |
# File 'lib/usps/imis/api.rb', line 177 def query(query_name, query_params = {}) query_params[:QueryName] = query_name path = "#{QUERY_PATH}?#{query_params.to_query}" uri = URI(File.join(imis_hostname, path)) request = Net::HTTP::Get.new(uri) result = submit(uri, (request)) JSON.parse(result.body) end |
#update(data) ⇒ Object
Convenience alias for updating mapped fields
204 205 206 |
# File 'lib/usps/imis/api.rb', line 204 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
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/usps/imis/api.rb', line 86 def with(id, &) old_id = imis_id self.imis_id = id @lock_imis_id = true instance_eval(&) ensure @lock_imis_id = false self.imis_id = old_id end |