Class: PicturePath::Response

Inherits:
Object
  • Object
show all
Includes:
REXML
Defined in:
lib/picturepath/response.rb

Overview

The PicturePath response returned from the client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Response

Create a response by parsing an xml response string.



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

def initialize(xml)
  @messages = []
  parse_xml(xml)
end

Instance Attribute Details

#messagesObject

Returns the value of attribute messages.



8
9
10
# File 'lib/picturepath/response.rb', line 8

def messages
  @messages
end

#order_numberObject

Returns the value of attribute order_number.



8
9
10
# File 'lib/picturepath/response.rb', line 8

def order_number
  @order_number
end

#priceObject

Returns the value of attribute price.



8
9
10
# File 'lib/picturepath/response.rb', line 8

def price
  @price
end

#status_codeObject

Returns the value of attribute status_code.



8
9
10
# File 'lib/picturepath/response.rb', line 8

def status_code
  @status_code
end

Instance Method Details

#charge_applies?Boolean

Return true if the submition charged the account money for the post.

Returns:

  • (Boolean)


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

def charge_applies?
  return price != null && price > 0
end

#error?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/picturepath/response.rb', line 24

def error?
  return !success? && !warning?
end

#parse_xml(xml) ⇒ Object

Parse the response xml to a Response object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/picturepath/response.rb', line 38

def parse_xml(xml) 
  doc = Document.new(xml)
  order_number_node = XPath.first(doc, '//ORDER_NUMBER')
  @order_number = (order_number_node && order_number_node[0])

  # which message do we use for the status_code??
  XPath.each(doc, '//MESSAGE') do |message|
    code = message.attributes['CODE'].to_i
    description = message.attributes['DESCRIPTION']
    text = message.text
    @messages.push :code => code, :description => description, :text => text
    if code >= 4000 && @order_number == nil && text =~ /Please use edit \(order number \w+\)/
      @order_number = /order number (\w+)/.match(text.to_s)[1]
    end
  end
  if @messages.size > 0
    @status_code = @messages[0][:code]
  end

  @price = 0
  XPath.each(doc, '//PRODUCT') do |message|
    @price += message.attributes['PRICE'].to_f
  end
end

#success?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/picturepath/response.rb', line 16

def success?
  return status_code >= 0 && status_code <= 3 
end

#system_unavailable?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/picturepath/response.rb', line 28

def system_unavailable?
  return status_code == 1000
end

#warning?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/picturepath/response.rb', line 20

def warning?
  return status_code >= 100 && status_code <= 200
end