Class: Transbank::Oneclick::Response

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

Constant Summary collapse

RESPONSE_CODE =
{
  '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'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, action) ⇒ Response

Returns a new instance of Response.



21
22
23
24
25
26
27
# File 'lib/transbank/oneclick/response.rb', line 21

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



76
77
78
# File 'lib/transbank/oneclick/response.rb', line 76

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



29
30
31
# File 'lib/transbank/oneclick/response.rb', line 29

def body
  content.body
end

#docObject



37
38
39
# File 'lib/transbank/oneclick/response.rb', line 37

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

#errors_displayObject



49
50
51
# File 'lib/transbank/oneclick/response.rb', line 49

def errors_display
  errors.join ', '
end

#exception?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/transbank/oneclick/response.rb', line 68

def exception?
  false
end

#http_codeObject



33
34
35
# File 'lib/transbank/oneclick/response.rb', line 33

def http_code
  content.code
end

#inspectObject



61
62
63
64
65
66
# File 'lib/transbank/oneclick/response.rb', line 61

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)


80
81
82
# File 'lib/transbank/oneclick/response.rb', line 80

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

#response_code_displayObject



57
58
59
# File 'lib/transbank/oneclick/response.rb', line 57

def response_code_display
  RESPONSE_CODE.fetch(response_code, response_code)
end

#valid?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/transbank/oneclick/response.rb', line 53

def valid?
  errors.empty?
end

#xml_errorObject



45
46
47
# File 'lib/transbank/oneclick/response.rb', line 45

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

#xml_resultObject



41
42
43
# File 'lib/transbank/oneclick/response.rb', line 41

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