Class: Veritrans::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/veritrans/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, url, options, time) ⇒ Result

Returns a new instance of Result.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/veritrans/result.rb', line 7

def initialize(response, url, options, time)
  begin
    if url =~ %r{/v2/.+/transcript$}
      @data = {}
    else
      @data = Veritrans._json_decode(response.body)

      # Failback for Hash#symbolize_keys
      @data.keys.each do |key|
        @data[(key.to_sym rescue key) || key] = @data.delete(key)
      end
    end

  rescue => e
    Veritrans.logger.info "Error parsing papi response #{e.message}"
    Veritrans.logger.info e.backtrace.join("\n")
    @data = {}
  end

  @time = time
  @status = response.status
  @response = response
  @url = url
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/veritrans/result.rb', line 67

def method_missing(method_name, *args)
  if args.size == 0 && @data && @data.has_key?(method_name)
    return @data[method_name]
  else
    super
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/veritrans/result.rb', line 3

def data
  @data
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/veritrans/result.rb', line 5

def response
  @response
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/veritrans/result.rb', line 4

def status
  @status
end

Instance Method Details

#bodyObject



63
64
65
# File 'lib/veritrans/result.rb', line 63

def body
  response.body
end

#created?Boolean

for VT-Link

Returns:

  • (Boolean)


38
39
40
# File 'lib/veritrans/result.rb', line 38

def created?
  @data[:status_code] == '201'
end

#inspectObject



75
76
77
78
79
# File 'lib/veritrans/result.rb', line 75

def inspect
  time_ms = (@time * 1000).round
  data = @data.inspect.gsub(/:([^\s]+)=>/, "\\1: ")
  "#<Veritrans::Result:#{object_id} ^^ http_status: #{@status} time: #{time_ms}ms ^^ data: #{data}>"
end

#messagesObject



55
56
57
58
59
60
61
# File 'lib/veritrans/result.rb', line 55

def messages
  if @data[:message].present?
    @data[:message].chomp(']').sub(/^\[/, '').split(',').map(&:strip)
  else
    []
  end
end

#redirect_urlObject



51
52
53
# File 'lib/veritrans/result.rb', line 51

def redirect_url
  @data[:redirect_url]
end

#status_codeObject



43
44
45
# File 'lib/veritrans/result.rb', line 43

def status_code
  @data[:status_code].to_i
end

#status_messageObject



47
48
49
# File 'lib/veritrans/result.rb', line 47

def status_message
  @data[:status_message]
end

#success?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/veritrans/result.rb', line 33

def success?
  @data[:status_code] == '200' || @data[:status_code] == '201' || @data[:status_code] == '407'
end