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



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

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
38
# File 'lib/prowly/response.rb', line 17

def self.map_xml(response)
  response_info = parse_xml(response)
  
  response_info.elements.each do |r|
    #define dynamic methods based on the prowl api response
    #posible methods on success: code, remaining, resetdate
    #posible methods on error: code
    r.attributes.each do |key, value|
      # puts "attribute #{key}: #{value}"
      if key == "code"
        add_instance_method(:status, value == "200" ? "success" : "error") 
        #define a method named status and it'll return "success" or "error"
        boolean_status = value == "200" ? true : false
        add_instance_method(:succeeded?, boolean_status)
        add_instance_method(:message, response_info[1].text) unless boolean_status
      end
      add_instance_method(key, value)
    end
  end
  
  true
end

.parse_xml(response) ⇒ Object



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

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