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



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

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



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

def api
  @api
end

#business_object_nameObject (readonly)

Name of the iMIS Business Object



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

def business_object_name
  @business_object_name
end

#loggerObject (readonly)

Tagged logger



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

def logger
  @logger
end

#ordinalObject (readonly)

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



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

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)



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

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:



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

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



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

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



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

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

#instance_variables_to_inspectObject

Ruby 3.5 instance variable filter



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

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:



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

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:



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

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:



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

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:



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

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

#queryUsps::Imis::Query

Run a query on the entire business object

Returns:



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

def query = api.query(business_object_name)

#with(imis_id) ⇒ Object

Support passthrough for Api#with



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

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