Class: Elong::Response
- Inherits:
-
Object
- Object
- Elong::Response
- Defined in:
- lib/elong/response.rb
Overview
Elong Http Response Class
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#dataset ⇒ Object
readonly
Returns the value of attribute dataset.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#initialize(response) ⇒ Elong::Response
constructor
Initializes a Request instance.
-
#json? ⇒ Boolean
Whether or not the content is json content type.
-
#to_json ⇒ Hash
Convert response content to JSON format.
-
#to_s ⇒ String
Output response content.
-
#to_xml(parser = nil) ⇒ Hash
Convert response content to XML format.
-
#xml? ⇒ Boolean
Whether or not the content is xml content type.
Constructor Details
#initialize(response) ⇒ Elong::Response
Initializes a Request instance
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/elong/response.rb', line 14 def initialize(response) @status = response.code @body = response.to_str @headers = response.headers @cookies = response. # mark status if @status == 200 if self.json? content = self.to_json @code = content['Code'] @dataset = content['Request'] elsif self.xml? content = self.to_xml @code = content['ApiResult']['Code'] @dataset = content['ApiResult']['Result'] end @code, @error = @code.split('|') if @code and @code.include?'|' @code = @code end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
8 9 10 |
# File 'lib/elong/response.rb', line 8 def body @body end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
8 9 10 |
# File 'lib/elong/response.rb', line 8 def code @code end |
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
8 9 10 |
# File 'lib/elong/response.rb', line 8 def @cookies end |
#dataset ⇒ Object (readonly)
Returns the value of attribute dataset.
8 9 10 |
# File 'lib/elong/response.rb', line 8 def dataset @dataset end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
8 9 10 |
# File 'lib/elong/response.rb', line 8 def error @error end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
8 9 10 |
# File 'lib/elong/response.rb', line 8 def headers @headers end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
8 9 10 |
# File 'lib/elong/response.rb', line 8 def status @status end |
Instance Method Details
#json? ⇒ Boolean
Whether or not the content is json content type
40 41 42 |
# File 'lib/elong/response.rb', line 40 def json? (@headers[:content_type].include?'json') ? true : false end |
#to_json ⇒ Hash
Convert response content to JSON format
61 62 63 |
# File 'lib/elong/response.rb', line 61 def to_json MultiJson.load(@body) end |
#to_s ⇒ String
Output response content
54 55 56 |
# File 'lib/elong/response.rb', line 54 def to_s @body end |
#to_xml(parser = nil) ⇒ Hash
Convert response content to XML format
68 69 70 71 |
# File 'lib/elong/response.rb', line 68 def to_xml(parser=nil) MultiXml.parser = p if parser and [:ox, :libxml, :nokogiri, :rexml].include?parser.to_sym MultiXml.parse(@body) end |
#xml? ⇒ Boolean
Whether or not the content is xml content type
47 48 49 |
# File 'lib/elong/response.rb', line 47 def xml? (['xml', 'html'].any? { |word| @headers[:content_type].include?(word) }) ? true : false end |