Class: Wheretocard::Response

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

Overview

Object representing a response object with attributes provided by WTC

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ Response

Initializer to transform a Hash into an Payment object



20
21
22
23
24
25
26
# File 'lib/wheretocard/response.rb', line 20

def initialize(args=nil)
  @line_items = []
  return if args.nil?
  args.each do |k,v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



11
12
13
# File 'lib/wheretocard/response.rb', line 11

def body
  @body
end

#commentObject

Returns the value of attribute comment.



12
13
14
# File 'lib/wheretocard/response.rb', line 12

def comment
  @comment
end

#errorsObject

Returns the value of attribute errors.



10
11
12
# File 'lib/wheretocard/response.rb', line 10

def errors
  @errors
end

#statusObject

Returns the value of attribute status.



13
14
15
# File 'lib/wheretocard/response.rb', line 13

def status
  @status
end

#urlObject

Returns the value of attribute url.



14
15
16
# File 'lib/wheretocard/response.rb', line 14

def url
  @url
end

Class Method Details

.from_order_request(order) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/wheretocard/response.rb', line 87

def self.from_order_request(order)
      response = HTTParty.post(Wheretocard.url, body: order.to_xml)
      # puts response.body, response.code, response.message, response.headers.inspect
      

      # raise error if code is not 200
      parse_respose_code(order, response.code)

      # parse the response
      # <orderResponse status="NOT_OK">
      #  <error>
      #    <code>OI_0202</code>
      #    <description><![CDATA[Posted XML not valid, or not conform Schema definition. ]]></description>
      #  </error>
      # </orderResponse>
      
      r = Response.new()
      r.body = response.body
      r.status = r.nokogiri_document.xpath('//orderResponse/@status').text
      if r.nokogiri_document.search('comment[type="url"]').any?
r.url = r.nokogiri_document.search('comment[type="url"]').first.text
      end
      r.validate
      return r
end

.parse_respose_code(order, code) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/wheretocard/response.rb', line 77

def self.parse_respose_code(order, code)
  if code == 200
    return
  elsif code == 500
    raise WheretocardError.new(order), "A server error occured (500)"
  else
raise WheretocardError.new(order), "The request failed (http status code #{code})"
  end
end

Instance Method Details

#error_codesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wheretocard/response.rb', line 48

def error_codes
  {
    "OI_0001" => "Internal server error",
    "OI_0099" => "Unknown error",

    "OI_0101" => "Supplied client not found",
    "OI_0102" => "Interface not enabled for client",

    "OI_0201" => "Submitted client not found",
    "OI_0202" => "Interface not enabled for client",
    "OI_0203" => "Missing (or empty) order reference id attribute.",
    "OI_0204" => "Request contains invalid orderline.ticket-ref <-> ticket.ref link reference(s)",
    "OI_0205" => "Unknown delivery channel",
    "OI_0206" => "Unknown delivery format",
    "OI_0207" => "Device manufacturerer/type is not known",
    "OI_0208" => "The delivery address is not specified",
    "OI_0209" => "The submitted data for order reference id (max 32 chars), or one of the
properties (max 100 chars) is too long.",
    "OI_0210" => "Submitted number of persons is negative or 0",
    "OI_0211" => "Submitted times valid is negative or 0",
    "OI_0212" => "Supplied email address is missing or the format is not correct",
    "OI_0213" => "The submitted orderline type does not belong to the client",
    "OI_0214" => "Missing required orderlinetype",
    "OI_0215" => "Orderline type is not owned by the submitted case",
    "OI_0216" => "Could not generate ticket code for request, all ticket codes are in use (batch
full).",
  }
end

#nokogiri_documentObject

return the parsed xml document



44
45
46
# File 'lib/wheretocard/response.rb', line 44

def nokogiri_document 
  @nokogiri_document ||= Nokogiri::XML(body)
end

#successObject Also known as: success?

return true if status is “OK”



38
39
40
# File 'lib/wheretocard/response.rb', line 38

def success
  status == "OK"
end

#validateObject

raise errors if the API returns errors



29
30
31
32
33
34
35
# File 'lib/wheretocard/response.rb', line 29

def validate
  if nokogiri_document.xpath('//error').any?
    code = nokogiri_document.xpath('//code').text
    desc = nokogiri_document.xpath('//description').text
    raise WheretocardError.new(self), "#{desc}\nError code: #{code} (#{error_codes[code]})"
  end
end