Class: Usps::Imis::BusinessObject

Inherits:
Object
  • Object
show all
Includes:
Requests
Defined in:
lib/usps/imis/business_object.rb

Overview

Configured accessor for a specific Business Object

Constant Summary collapse

API_PATH =

Endpoint for general API requests

'api'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, business_object_name, ordinal: nil) ⇒ BusinessObject

A new instance of BusinessObject



36
37
38
39
40
41
# File 'lib/usps/imis/business_object.rb', line 36

def initialize(api, business_object_name, ordinal: nil)
  @api = api
  @business_object_name = business_object_name
  @ordinal = ordinal
  @logger ||= Imis.logger('BusinessObject')
end

Instance Attribute Details

#apiObject (readonly)

The parent Api object



20
21
22
# File 'lib/usps/imis/business_object.rb', line 20

def api
  @api
end

#business_object_nameObject (readonly)

Name of the iMIS Business Object



24
25
26
# File 'lib/usps/imis/business_object.rb', line 24

def business_object_name
  @business_object_name
end

#loggerObject (readonly)

Tagged logger



32
33
34
# File 'lib/usps/imis/business_object.rb', line 32

def logger
  @logger
end

#ordinalObject (readonly)

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



28
29
30
# File 'lib/usps/imis/business_object.rb', line 28

def ordinal
  @ordinal
end

Instance Method Details

#blank_objectObject

Build a blank object for the current iMIS ID



142
# File 'lib/usps/imis/business_object.rb', line 142

def blank_object(&) = BlankObject.new(self).build(&)

#deletetrue Also known as: destroy

Remove a business object for the current member

Returns:

  • (true)

    Only on success response (i.e. blank string from the API)



137
# File 'lib/usps/imis/business_object.rb', line 137

def delete = submit(uri, authorize(http_delete)).body == '' # rubocop:disable Naming/PredicateMethod

#get(*fields) ⇒ Usps::Imis::Data+ Also known as: read

Get a business object for the current member

If fields is provided, will return only those field values

Parameters:

  • fields (String)

    Field names to return

Returns:



69
# File 'lib/usps/imis/business_object.rb', line 69

def get(*fields) = fields.any? ? get_fields(*fields) : raw_object

#get_field(field) ⇒ Object Also known as: fetch, []

Get a single named field from a business object for the current member

Parameters:

  • field (String)

    Field name to return

Returns:

  • Response data field value from the API



78
# File 'lib/usps/imis/business_object.rb', line 78

def get_field(field) = raw_object[field]

#get_fields(*fields) ⇒ Array Also known as: fetch_all

Get named fields from a business object for the current member

Parameters:

  • names (Array<String>)

    Field names to return

Returns:

  • (Array)

    Response data from the API



88
89
90
91
# File 'lib/usps/imis/business_object.rb', line 88

def get_fields(*fields)
  values = raw_object
  fields.map { values[it] }
end

#instance_variables_to_inspectObject

Ruby 3.5 instance variable filter



151
# File 'lib/usps/imis/business_object.rb', line 151

def instance_variables_to_inspect = instance_variables - %i[@api @logger]

#post(body) ⇒ Usps::Imis::Data Also known as: create

Create a business object for the current member

Parameters:

  • body (Hash)

    Full raw API object data

Returns:



130
# File 'lib/usps/imis/business_object.rb', line 130

def post(body) = put_object(http_post, body)

#post_from_blankObject Also known as: create_from_blank

Create a business object for the current member, starting with a blank object and building properties



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

def post_from_blank(&) = post(blank_object(&))

#put(body) ⇒ Usps::Imis::Data Also known as: update

Update a business object for the current member

Any properties not included will be left unmodified

Parameters:

  • body (Hash)

    Full raw API object data

Returns:



121
# File 'lib/usps/imis/business_object.rb', line 121

def put(body) = put_object(http_put, body)

#put_field(field, value) ⇒ Usps::Imis::Data Also known as: []=

Update a single named field on a business object for the current member

Parameters:

  • field (String)

    Name of the field

  • value

    Value of the field

Returns:



101
# File 'lib/usps/imis/business_object.rb', line 101

def put_field(field, value) = put(filter_fields(field => value))

#put_fields(fields) ⇒ Usps::Imis::Data Also known as: patch

Update only specific fields on a business object for the current member

Parameters:

  • fields (Hash)

    Conforms to pattern { field_key => value }

Returns:



110
# File 'lib/usps/imis/business_object.rb', line 110

def put_fields(fields) = put(filter_fields(fields))

#queryUsps::Imis::Query

Run a query on the entire business object

Returns:



47
# File 'lib/usps/imis/business_object.rb', line 47

def query = api.query(business_object_name)

#with(imis_id) ⇒ Object

Support passthrough for Api#with



51
52
53
54
55
56
57
58
59
# File 'lib/usps/imis/business_object.rb', line 51

def with(imis_id, &)
  # Bring into local scope
  wrapper_business_object_name = business_object_name
  wrapper_ordinal = ordinal

  api.with(imis_id) do
    on(wrapper_business_object_name, ordinal: wrapper_ordinal, &)
  end
end