Class: EVSS::PCIU::RequestBody

Inherits:
Object
  • Object
show all
Defined in:
lib/evss/pciu/request_body.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_attrs, pciu_key:, date_attr: 'effective_date') ⇒ RequestBody

Returns a new instance of RequestBody.



8
9
10
11
12
# File 'lib/evss/pciu/request_body.rb', line 8

def initialize(request_attrs, pciu_key:, date_attr: 'effective_date')
  @request_attrs = request_attrs
  @pciu_key = pciu_key
  @date_attr = date_attr
end

Instance Attribute Details

#date_attrObject (readonly)

Returns the value of attribute date_attr.



6
7
8
# File 'lib/evss/pciu/request_body.rb', line 6

def date_attr
  @date_attr
end

#pciu_keyObject (readonly)

Returns the value of attribute pciu_key.



6
7
8
# File 'lib/evss/pciu/request_body.rb', line 6

def pciu_key
  @pciu_key
end

#request_attrsObject (readonly)

Returns the value of attribute request_attrs.



6
7
8
# File 'lib/evss/pciu/request_body.rb', line 6

def request_attrs
  @request_attrs
end

Instance Method Details

#convert_to_jsonObject (private)



44
45
46
47
48
49
50
# File 'lib/evss/pciu/request_body.rb', line 44

def convert_to_json
  {
    pciu_key =>
      request_attrs.as_json.transform_keys { |k| k.camelize(:lower) }

  }.to_json
end

#remove_empty_attrsObject (private)



40
41
42
# File 'lib/evss/pciu/request_body.rb', line 40

def remove_empty_attrs
  @request_attrs = request_attrs.as_json.delete_if { |_k, v| v.blank? }
end

#setString

Adjusts the passed request attributes to be formatted for an EVSS POST or PUT request body.

Examples:

Here is a parsed version of the returned JSON:

{
  'cnpPhone' => {
    'countryCode' => '1',
    'number' => '4445551212',
    'extension' => '101',
    'effectiveDate' => '2018-04-02T16:01:50+00:00'
  }
}

Returns:

  • (String)

    Returns a string of JSON, nested in the passed pciu_key.



28
29
30
31
32
# File 'lib/evss/pciu/request_body.rb', line 28

def set
  set_effective_date
  remove_empty_attrs
  convert_to_json
end

#set_effective_dateObject (private)



36
37
38
# File 'lib/evss/pciu/request_body.rb', line 36

def set_effective_date
  request_attrs.tap { |instance| instance[date_attr] = DateTime.now.utc }
end