class Elong::Response

Elong Http Response Class

Attributes

body[R]
code[R]
cookies[R]
dataset[R]
error[R]
headers[R]
status[R]

Public Class Methods

new(response) click to toggle source

Initializes a Request instance

@param [RestClient::Response] @return [Elong::Response]

# File lib/elong/response.rb, line 14
def initialize(response)
  @status = response.code
  @body = response.to_str
  @headers = response.headers
  @cookies = response.cookies

  # 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

Public Instance Methods

json?() click to toggle source

Whether or not the content is json content type

@return [Boolean]

# File lib/elong/response.rb, line 40
def json?
  (@headers[:content_type].include?'json') ? true : false
end
to_json() click to toggle source

Convert response content to JSON format

@return [Hash]

# File lib/elong/response.rb, line 61
def to_json
  MultiJson.load(@body)
end
to_s() click to toggle source

Output response content

@return [String]

# File lib/elong/response.rb, line 54
def to_s
  @body
end
to_xml(parser=nil) click to toggle source

Convert response content to XML format

@return [Hash]

# 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?() click to toggle source

Whether or not the content is xml content type

@return [Boolean]

# File lib/elong/response.rb, line 47
def xml?
  (['xml', 'html'].any? { |word| @headers[:content_type].include?(word) }) ? true : false
end