Class: Usps::Imis::Api
- Inherits:
-
Object
- Object
- Usps::Imis::Api
- Defined in:
- lib/usps/imis/api.rb
Defined Under Namespace
Classes: PANELS
Constant Summary collapse
- AUTHENTICATION_PATH =
'Token'
- API_PATH =
'api'
- QUERY_PATH =
'api/Query'
Instance Attribute Summary collapse
-
#imis_id ⇒ Object
Returns the value of attribute imis_id.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#token_expiration ⇒ Object
readonly
Returns the value of attribute token_expiration.
Instance Method Summary collapse
-
#delete(business_object_name, url_id: nil) ⇒ Object
Remove a business object for the current member.
-
#get(business_object_name, url_id: nil) ⇒ Object
Get a business object for the current member.
-
#imis_id_for(certificate) ⇒ Object
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.
- #mapper ⇒ Object
- #panels ⇒ Object
-
#post(business_object_name, body, url_id: nil) ⇒ Object
Create a business object for the current member.
-
#put(business_object_name, body, url_id: nil) ⇒ Object
Update a business object for the current member.
-
#put_fields(business_object_name, fields, url_id: nil) ⇒ Object
Update only specific fields on a business object for the current member.
-
#query(query_name, query_params = {}) ⇒ Object
Run an IQA Query.
- #update(data) ⇒ Object
-
#with(id, &block) ⇒ Object
Run requests as DSL, with specific iMIS ID only maintained for this scope.
Constructor Details
#initialize(skip_authentication: false, imis_id: nil) ⇒ Api
Returns a new instance of Api.
13 14 15 16 |
# File 'lib/usps/imis/api.rb', line 13 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
Returns the value of attribute imis_id.
11 12 13 |
# File 'lib/usps/imis/api.rb', line 11 def imis_id @imis_id end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
11 12 13 |
# File 'lib/usps/imis/api.rb', line 11 def token @token end |
#token_expiration ⇒ Object (readonly)
Returns the value of attribute token_expiration.
11 12 13 |
# File 'lib/usps/imis/api.rb', line 11 def token_expiration @token_expiration end |
Instance Method Details
#delete(business_object_name, url_id: nil) ⇒ Object
Remove a business object for the current member
Returns empty string on success
85 86 87 88 89 90 |
# File 'lib/usps/imis/api.rb', line 85 def delete(business_object_name, url_id: nil) uri = uri_for(business_object_name, url_id: url_id) request = Net::HTTP::Delete.new(uri) result = submit(uri, (request)) result.body end |
#get(business_object_name, url_id: nil) ⇒ Object
Get a business object for the current member
45 46 47 48 49 50 |
# File 'lib/usps/imis/api.rb', line 45 def get(business_object_name, url_id: nil) uri = uri_for(business_object_name, url_id: url_id) request = Net::HTTP::Get.new(uri) result = submit(uri, (request)) JSON.parse(result.body) end |
#imis_id_for(certificate) ⇒ Object
Convert a member’s certificate number into an iMIS ID number
26 27 28 29 |
# File 'lib/usps/imis/api.rb', line 26 def imis_id_for(certificate) result = query(Imis.configuration.imis_id_query_name, { certificate: certificate }) @imis_id = result['Items']['$values'][0]['ID'] end |
#mapper ⇒ Object
106 107 108 |
# File 'lib/usps/imis/api.rb', line 106 def mapper @mapper ||= Mapper.new(self) end |
#panels ⇒ Object
110 111 112 113 114 |
# File 'lib/usps/imis/api.rb', line 110 def panels @panels ||= PANELS.new( Panel::Vsc.new(self) ) end |
#post(business_object_name, body, url_id: nil) ⇒ Object
Create a business object for the current member
73 74 75 76 77 78 79 |
# File 'lib/usps/imis/api.rb', line 73 def post(business_object_name, body, url_id: nil) uri = uri_for(business_object_name, url_id: 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) ⇒ Object
Update a business object for the current member
63 64 65 66 67 68 69 |
# File 'lib/usps/imis/api.rb', line 63 def put(business_object_name, body, url_id: nil) uri = uri_for(business_object_name, url_id: 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) ⇒ Object
Update only specific fields on a business object for the current member
fields - hash of shape: { field_name => new_value }
56 57 58 59 |
# File 'lib/usps/imis/api.rb', line 56 def put_fields(business_object_name, fields, url_id: nil) updated = filter_fields(business_object_name, fields) put(business_object_name, updated, url_id: url_id) end |
#query(query_name, query_params = {}) ⇒ Object
Run an IQA Query
query_name - the full path of the query in IQA, e.g. `$/_ABC/Fiander/iMIS_ID`
query_params - hash of shape: { param_name => param_value }
97 98 99 100 101 102 103 104 |
# File 'lib/usps/imis/api.rb', line 97 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
116 117 118 |
# File 'lib/usps/imis/api.rb', line 116 def update(data) mapper.update(data) end |
#with(id, &block) ⇒ Object
Run requests as DSL, with specific iMIS ID only maintained for this scope
This should be used with methods that do not change the value of ‘imis_id`
35 36 37 38 39 40 41 |
# File 'lib/usps/imis/api.rb', line 35 def with(id, &block) old_id = imis_id self.imis_id = id instance_eval(&block) ensure self.imis_id = old_id end |