Class: Usps::Imis::BusinessObject

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

Overview

DEV

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



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

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



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

def api
  @api
end

#business_object_nameObject (readonly)

Name of the iMIS Business Object



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

def business_object_name
  @business_object_name
end

#loggerObject (readonly)

Tagged logger



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

def logger
  @logger
end

#ordinalObject (readonly)

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



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

def ordinal
  @ordinal
end

Instance Method Details

#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)



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

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:



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

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



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

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



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

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

#instance_variables_to_inspectObject

Ruby 3.5 instance variable filter



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

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:



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

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

#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:



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

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:



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

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:



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

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

#queryUsps::Imis::Query

Run a query on the entire business object

Returns:



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

def query = api.query(business_object_name)

#with(imis_id) ⇒ Object

Support passthrough for Api#with



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

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