Class: Usps::Imis::Api

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(skip_authentication: false, imis_id: nil) ⇒ Api

A new instance of Api

Parameters:

  • skip_authentication (bool) (defaults to: false)

    Skip authentication on initialization (used for tests)

  • imis_id (Integer, String) (defaults to: nil)

    iMIS ID to select immediately on initialization



42
43
44
45
# File 'lib/usps/imis/api.rb', line 42

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_idObject

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_idObject (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

#tokenObject (readonly)

API bearer token



21
22
23
# File 'lib/usps/imis/api.rb', line 21

def token
  @token
end

#token_expirationObject (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

#imis_id_for(certificate) ⇒ String

Convert a member’s certificate number into an iMIS ID number

Parameters:

  • certificate (String)

    Certificate number to lookup the corresponding iMIS ID for

Returns:

  • (String)

    Corresponding iMIS ID

Raises:



63
64
65
66
67
68
69
70
71
# File 'lib/usps/imis/api.rb', line 63

def imis_id_for(certificate)
  raise Errors::LockedIdError if lock_imis_id

  begin
    self.imis_id = query(Imis.configuration.imis_id_query_name, { certificate: }).first['ID'].to_i
  rescue StandardError
    raise Errors::NotFoundError, 'Member not found'
  end
end

#instance_variables_to_inspectObject

Ruby 3.5 instance variable filter



146
# File 'lib/usps/imis/api.rb', line 146

def instance_variables_to_inspect = %i[@token_expiration @imis_id]

#mapperObject

An instance of Mapper, using this instance as its parent Api



127
128
129
# File 'lib/usps/imis/api.rb', line 127

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.

Parameters:

  • business_object_name (String)

    Name of the business object

  • ordinal (Integer) (defaults to: nil)

    Ordinal to build override ID param of the URL (e.g. used for Panels)



118
119
120
121
122
123
# File 'lib/usps/imis/api.rb', line 118

def on(business_object_name, ordinal: nil, &)
  object = BusinessObject.new(self, business_object_name, ordinal:)
  return object unless block_given?

  object.instance_eval(&)
end

#panelsObject

Convenience accessor for available Panel objects, each using this instance as its parent Api



140
141
142
# File 'lib/usps/imis/api.rb', line 140

def panels
  @panels ||= Panels.all(self)
end

#query(query_name, query_params = {}) ⇒ Hash

Build an IQA Query interface

Parameters:

  • query_name (String)

    Full path of the query in IQA, e.g. $/_ABC/Fiander/iMIS_ID

Returns:

  • (Hash)

    Response data from the API



107
108
109
# File 'lib/usps/imis/api.rb', line 107

def query(query_name, query_params = {})
  Query.new(self, query_name, query_params)
end

#update(data) ⇒ Object

Convenience alias for updating mapped fields



133
134
135
# File 'lib/usps/imis/api.rb', line 133

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.

Examples:

with(12345) do
  update(mm: 15)
end

Parameters:

  • id (Integer, String)

    iMIS ID to select for requests within the block



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