Class: Sappy::Response
- Inherits:
-
Object
show all
- Defined in:
- lib/sappy/response.rb
Direct Known Subclasses
Sappy::Responses::AccountInfo, Sappy::Responses::AddMonitor, Sappy::Responses::AnnualStatistics, Sappy::Responses::Auth, Sappy::Responses::DailyStatistics, Sappy::Responses::DisableMonitor, Sappy::Responses::EditMonitor, Sappy::Responses::EnableMonitor, Sappy::Responses::Monitors, Sappy::Responses::MonthlyStatistics, Sappy::Responses::RemoveMonitor, Sappy::Responses::SummaryStatistics
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
#data ⇒ Object
Returns the value of attribute data.
12
13
14
|
# File 'lib/sappy/response.rb', line 12
def data
@data
end
|
#xml ⇒ Object
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_response ⇒ Object
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
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
|
#parse ⇒ Object
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
|
#success ⇒ Object
50
51
52
|
# File 'lib/sappy/response.rb', line 50
def success
raise NotImplementedError, "Overwrite #success in a Response subclass"
end
|