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



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

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



15
16
17
# File 'lib/usps/imis/business_object.rb', line 15

def api
  @api
end

#business_object_nameObject (readonly)

Name of the iMIS Business Object



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

def business_object_name
  @business_object_name
end

#ordinalObject (readonly)

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



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

def ordinal
  @ordinal
end

Instance Method Details

#deleteString Also known as: destroy

Remove a business object for the current member

Returns:

  • (String)

    Error response body from the API, or empty string on success



102
103
104
105
106
# File 'lib/usps/imis/business_object.rb', line 102

def delete
  request = Net::HTTP::Delete.new(uri)
  result = submit(uri, authorize(request))
  result.body
end

#getHash Also known as: read

Get a business object for the current member

Returns:

  • (Hash)

    Response data from the API



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

def get
  request = Net::HTTP::Get.new(uri)
  result = submit(uri, authorize(request))
  JSON.parse(result.body)
end

#get_field(name) ⇒ Hash Also known as: fetch

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

Parameters:

  • name (String)

    Field name to return

Returns:

  • (Hash)

    Response data from the API



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

def get_field(name)
  values = get['Properties']['$values']
  value = values.find { |hash| hash['Name'] == name }['Value']

  value.is_a?(String) ? value : value['$value']
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



90
91
92
93
94
95
# File 'lib/usps/imis/business_object.rb', line 90

def post(body)
  request = Net::HTTP::Post.new(uri(id: ''))
  request.body = JSON.dump(body)
  result = submit(uri, authorize(request))
  JSON.parse(result.body)
end

#put(body) ⇒ Hash Also known as: update

Update a business object for the current member

Parameters:

  • body (Hash)

    Full raw API object data

Returns:

  • (Hash)

    Response data from the API



76
77
78
79
80
81
# File 'lib/usps/imis/business_object.rb', line 76

def put(body)
  request = Net::HTTP::Put.new(uri)
  request.body = JSON.dump(body)
  result = submit(uri, authorize(request))
  JSON.parse(result.body)
end

#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



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

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