Class: Sappy::Response

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

Defined Under Namespace

Classes: SessionExpired, UnhandledError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Response

Returns a new instance of Response.



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

def initialize(data)
  @data = data.to_s
  @xml  = Nokogiri::XML.parse(data)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#xmlObject (readonly)

Returns the value of attribute xml.



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

def xml
  @xml
end

Class Method Details

.parse(xml) ⇒ Object



6
7
8
9
10
# File 'lib/sappy/response.rb', line 6

def self.parse(xml)
  r = new(xml)
  r.parse
  r
end

Instance Method Details

#error_responseObject



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

def error_response
  resp = xml.xpath('//err')
  resp && resp.first
end

#error_response?Boolean

Returns:

  • (Boolean)


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

def error_response?
  resp = xml.xpath('//rsp')
  resp && resp.first['stat'] == 'fail'
end

#failure(code, message) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/sappy/response.rb', line 54

def failure(code, message)
  case code
  when "WRONG_DATA"
    raise ArgumentError, "You didn't provide a correct monitor id: #{message}"
  else
    raise NotImplementedError, "Overwrite #failure in a Response subclass"
  end
end

#first_xpath(path) ⇒ Object



29
30
31
32
# File 'lib/sappy/response.rb', line 29

def first_xpath(path)
  node = xml.xpath(path)
  node && node.first
end

#parseObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sappy/response.rb', line 34

def parse
  if error_response?
    error = error_response
    message = error["msg"]
    case code = error["code"]
    when "AUTH_EXPIRED"
      raise SessionExpired, "The auth session expired, reconnect to continue using the API"
    else
      failure(code, message)
      raise UnhandledError, "Unhandled error: #{code}, #{message}"
    end
  else
    success
  end
end

#successObject

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/sappy/response.rb', line 50

def success
  raise NotImplementedError, "Overwrite #success in a Response subclass"
end