Class: Transbank::Oneclick::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/transbank/oneclick/response.rb

Constant Summary collapse

RESPONSE_CODE =
{
  authorize: {
    '0'   => 'aprobado',
    '-1'  => 'rechazo',
    '-2'  => 'rechazo',
    '-3'  => 'rechazo',
    '-4'  => 'rechazo',
    '-5'  => 'rechazo',
    '-6'  => 'rechazo',
    '-7'  => 'rechazo',
    '-8'  => 'rechazo',
    '-97' => 'limites Oneclick, máximo monto diario de pago excedido',
    '-98' => 'limites Oneclick, máximo monto de pago excedido',
    '-99' => 'limites Oneclick, máxima cantidad de pagos diarios excedido'
  },
  default: {
    '0'   => 'aprobado',
    '-98' => 'Error'
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, action) ⇒ Response

Returns a new instance of Response.



27
28
29
30
31
32
33
# File 'lib/transbank/oneclick/response.rb', line 27

def initialize(content, action)
  self.content = content
  self.action = action
  self.attributes = Hash[*xml_result.map{|e| [e.name.underscore.to_sym, e.text]}.flatten]
  self.errors = []
  validate!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



86
87
88
# File 'lib/transbank/oneclick/response.rb', line 86

def method_missing(method_name, *args, &block)
  attributes[method_name.to_sym] || super
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



4
5
6
# File 'lib/transbank/oneclick/response.rb', line 4

def action
  @action
end

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/transbank/oneclick/response.rb', line 4

def attributes
  @attributes
end

#contentObject

Returns the value of attribute content.



4
5
6
# File 'lib/transbank/oneclick/response.rb', line 4

def content
  @content
end

#errorsObject

Returns the value of attribute errors.



4
5
6
# File 'lib/transbank/oneclick/response.rb', line 4

def errors
  @errors
end

#exceptionObject

Returns the value of attribute exception.



4
5
6
# File 'lib/transbank/oneclick/response.rb', line 4

def exception
  @exception
end

Instance Method Details

#bodyObject



35
36
37
# File 'lib/transbank/oneclick/response.rb', line 35

def body
  content.body
end

#docObject



43
44
45
# File 'lib/transbank/oneclick/response.rb', line 43

def doc
  @doc ||= Nokogiri::XML body
end

#errors_displayObject



55
56
57
# File 'lib/transbank/oneclick/response.rb', line 55

def errors_display
  errors.join ', '
end

#exception?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/transbank/oneclick/response.rb', line 78

def exception?
  false
end

#http_codeObject



39
40
41
# File 'lib/transbank/oneclick/response.rb', line 39

def http_code
  content.code
end

#inspectObject



71
72
73
74
75
76
# File 'lib/transbank/oneclick/response.rb', line 71

def inspect
  result = ["valid: #{valid?}"]
  result << attributes_display if attributes.any?
  result << "error: \"#{errors_display}\"" if errors.any?
  "#<#{self.class} #{result.join(', ')} >"
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/transbank/oneclick/response.rb', line 90

def respond_to_missing?(method_name, include_private = false)
  attributes.keys.include?(method_name.to_sym) || super
end

#response_code_displayObject



63
64
65
66
67
68
69
# File 'lib/transbank/oneclick/response.rb', line 63

def response_code_display
  if respond_to?(:response_code)
    # key = RESPONSE_CODE.keys.include?(action) ? action : :default
    # RESPONSE_CODE[key].fetch(response_code, response_code)
    RESPONSE_CODE[action] && RESPONSE_CODE[action][response_code] || RESPONSE_CODE[:default][response_code] || response_code
  end
end

#valid?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/transbank/oneclick/response.rb', line 59

def valid?
  errors.empty?
end

#xml_errorObject



51
52
53
# File 'lib/transbank/oneclick/response.rb', line 51

def xml_error
  doc.xpath("//faultstring")
end

#xml_resultObject



47
48
49
# File 'lib/transbank/oneclick/response.rb', line 47

def xml_result
  doc.at_xpath("//return") && doc.at_xpath("//return").children || []
end