Class: Usps::Imis::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/usps/imis/mapper.rb

Overview

Specific known fields mapping to facilitate updating across multiple business objects.

Constant Summary collapse

FIELD_MAPPING =

List of known mapped fields

{
  grade: %w[ABC_ASC_Individual_Demog Grade],
  edpro: %w[ABC_ASC_Individual_Demog Educ_Proficiency],
  edach: %w[ABC_ASC_Individual_Demog Educ_Achievement],
  vessel_examiner: %w[ABC_ASC_Individual_Demog Vol_Vessel_Examiner],
  mm: %w[ABC_ASC_Individual_Demog TotMMS],
  mm_updated: %w[ABC_ASC_Individual_Demog MMS_Updated]
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api = nil, imis_id: nil) ⇒ Mapper

A new instance of Mapper



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

def initialize(api = nil, imis_id: nil)
  @api = api || Api.new
  @api.imis_id = imis_id if imis_id
end

Instance Attribute Details

#apiObject (readonly)

The parent Api object



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

def api
  @api
end

Instance Method Details

#update(data) ⇒ Array<Hash>

Update a member’s data on multiple affected business objects by arbitrary field names

Does not require knowing which business object / iMIS-specific field name to use

Only available for fields defined in FIELD_MAPPING

Parameters:

  • data (Hash)

    Conforms to pattern { field_key => value }

Returns:

  • (Array<Hash>)

    Response data from the API for each internal update request



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/usps/imis/mapper.rb', line 41

def update(data)
  updates = data.each_with_object({}) do |(field_key, value), hash|
    map_update(field_key) do |business_object_name, field|
      hash[business_object_name] ||= {}
      hash[business_object_name][field] = value
    end
  end

  updates.map do |business_object_name, field_updates|
    api.put_fields(business_object_name, field_updates)
  end
end