Class: ChargeIO::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/chargeio/models/message.rb

Overview

Simple class for serializing ChargeIO <message> responses

Constant Summary collapse

DEFAULT_RESPONSE_MAPPINGS =
{
  'error timeout' => 'The operation timed out'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Message

Returns a new instance of Message.


9
10
11
12
13
14
15
16
17
# File 'lib/chargeio/models/message.rb', line 9

def initialize(attributes)
  @attributes = attributes
  @level = attributes.with_indifferent_access['level']
  @code = attributes.with_indifferent_access['code']
  @sub_code = attributes.with_indifferent_access['sub_code']
  @context = attributes.with_indifferent_access['context']
  @message = attributes.with_indifferent_access['message']
  @facility = attributes.with_indifferent_access['facility']
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes


7
8
9
# File 'lib/chargeio/models/message.rb', line 7

def attributes
  @attributes
end

#codeObject

Returns the value of attribute code


7
8
9
# File 'lib/chargeio/models/message.rb', line 7

def code
  @code
end

#contextObject

Returns the value of attribute context


7
8
9
# File 'lib/chargeio/models/message.rb', line 7

def context
  @context
end

#facilityObject

Returns the value of attribute facility


7
8
9
# File 'lib/chargeio/models/message.rb', line 7

def facility
  @facility
end

#levelObject

Returns the value of attribute level


7
8
9
# File 'lib/chargeio/models/message.rb', line 7

def level
  @level
end

#messageObject

Returns the value of attribute message


7
8
9
# File 'lib/chargeio/models/message.rb', line 7

def message
  @message
end

#sub_codeObject

Returns the value of attribute sub_code


7
8
9
# File 'lib/chargeio/models/message.rb', line 7

def sub_code
  @sub_code
end

Class Method Details

.response_mappingsObject


23
24
25
# File 'lib/chargeio/models/message.rb', line 23

def self.response_mappings
  @@response_mappings ||= DEFAULT_RESPONSE_MAPPINGS
end

.response_mappings=(_mappings) ⇒ Object


26
27
28
# File 'lib/chargeio/models/message.rb', line 26

def self.response_mappings=(_mappings)
  @@response_mappings = DEFAULT_RESPONSE_MAPPINGS.merge(_mappings)
end

Instance Method Details

#descriptionObject


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chargeio/models/message.rb', line 30

def description
  # Check for overridden message definition
  _key = [level, code, context, sub_code].find_all{|i| i != nil}.join(' ')
  if self.class.response_mappings[_key].present?
    return self.class.response_mappings[_key]
  end

  _key = [level, code, context].find_all{|i| i != nil}.join(' ')
  if self.class.response_mappings[_key].present?
    return self.class.response_mappings[_key]
  end

  _key = [level, code].find_all{|i| i != nil}.join(' ')
  if self.class.response_mappings[_key].present?
    self.class.response_mappings[_key]
  end

  message || "Unknown ChargeIO #{level.capitalize}"
end