Class: Prowly::Response

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

Overview

This is a Prowl API response wrapper

Defined Under Namespace

Modules: ErrorCode, SuccessCode

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml_response, full_http_response) ⇒ Response

Returns a new instance of Response.



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

def initialize(xml_response, full_http_response)
  @response = xml_response
  @http_response = full_http_response
  Response.map_xml @response
end

Instance Attribute Details

#http_responseObject

Returns the value of attribute http_response.



9
10
11
# File 'lib/prowly/response.rb', line 9

def http_response
  @http_response
end

#response=(value) ⇒ Object (writeonly)

Sets the attribute response

Parameters:

  • value

    the value to set the attribute response to.



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

def response=(value)
  @response = value
end

Class Method Details

.add_instance_method(name, content) ⇒ Object

define dynamic methods



45
46
47
# File 'lib/prowly/response.rb', line 45

def self.add_instance_method(name, content)
  define_method(name) { content }
end

.map_xml(response) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/prowly/response.rb', line 17

def self.map_xml(response)
  response_info = parse_xml(response)
  
  #define dynamic methods based on the prowl api response
  #posible methods on success: code, remaining, resetdate
  #posible methods on error: code
  response_info.attributes.each do |key, value|
    add_instance_method(key, value)
  end
  
  response_info_name = response_info.name
  #define a method named status and it'll return "success" or "error"
  add_instance_method(:status, response_info_name)

  boolean_status = response_info_name == "success" ? true : false

  add_instance_method(:message, response_info.text) unless boolean_status

  add_instance_method(:succeeded?, boolean_status)
  true
end

.parse_xml(response) ⇒ Object



39
40
41
42
# File 'lib/prowly/response.rb', line 39

def self.parse_xml(response)
  data = REXML::Document.new response
  data.root[1]
end