Class: Elong::Response

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

Overview

Elong Http Response Class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Elong::Response

Initializes a Request instance

Parameters:

  • (RestClient::Response)


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.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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



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

def cookies
  @cookies
end

#datasetObject (readonly)

Returns the value of attribute dataset.



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

def dataset
  @dataset
end

#errorObject (readonly)

Returns the value of attribute error.



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

def error
  @error
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#statusObject (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

Returns:

  • (Boolean)


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

def json?
  (@headers[:content_type].include?'json') ? true : false
end

#to_jsonHash

Convert response content to JSON format

Returns:

  • (Hash)


61
62
63
# File 'lib/elong/response.rb', line 61

def to_json
  MultiJson.load(@body)
end

#to_sString

Output response content

Returns:

  • (String)


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

Returns:

  • (Hash)


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

Returns:

  • (Boolean)


47
48
49
# File 'lib/elong/response.rb', line 47

def xml?
  (['xml', 'html'].any? { |word| @headers[:content_type].include?(word) }) ? true : false
end