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



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

def initialize(api, business_object_name, ordinal: nil)
  @api = api
  @business_object_name = business_object_name
  @ordinal = ordinal
end

Instance Attribute Details

#apiObject (readonly)

The parent Api object



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

def api
  @api
end

#business_object_nameObject (readonly)

Name of the iMIS Business Object



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

def business_object_name
  @business_object_name
end

#ordinalObject (readonly)

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



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

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)



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

def delete = submit(uri, authorize(Net::HTTP::Delete.new(uri))).body == '' # rubocop:disable Naming/PredicateMethod

#get(*fields) ⇒ Hash+ 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:

  • (Hash, Array<Hash>)

    Response data from the API



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

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

#get_field(field) ⇒ Hash 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:

  • (Hash)

    Response data from the API



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

def get_field(field) = extract_field_value(raw_object_values, field)

#get_fields(*fields) ⇒ Array<Hash> 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<Hash>)

    Response data from the API



61
62
63
64
# File 'lib/usps/imis/business_object.rb', line 61

def get_fields(*fields)
  values = raw_object_values
  fields.map { extract_field_value(values, it) }
end

#post(body) ⇒ Hash Also known as: create

Create a business object for the current member

Parameters:

  • body (Hash)

    Full raw API object data

Returns:

  • (Hash)

    Response data from the API



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

def post(body) = put_object(Net::HTTP::Post.new(uri(id: '')), body)

#put(body) ⇒ Hash 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:

  • (Hash)

    Response data from the API



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

def put(body) = put_object(Net::HTTP::Put.new(uri), body)

#put_fields(fields) ⇒ Hash 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:

  • (Hash)

    Response data from the API



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

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