Class: HttpUtilities::Http::Response

Inherits:
Object
  • Object
show all
Includes:
Format, Logger
Defined in:
lib/http_utilities/http/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#log

Methods included from Format

#as_html, #as_json, #as_multi_xml, #as_xml

Constructor Details

#initialize(body = nil, request = nil, options = {}) ⇒ Response

Returns a new instance of Response.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/http_utilities/http/response.rb', line 9

def initialize(body = nil, request = nil, options = {})
  options               =   options.clone()

  self.body             =   body
  self.request          =   request

  self.parsed_body      =   nil

  self.format           =   options.delete(:format) { |e| nil }
  self.force_encoding   =   options.delete(:force_encoding) { |e| true }

  encode if (self.force_encoding)
  parse_response
end

Instance Attribute Details

#body ⇒ Object

Returns the value of attribute body.



7
8
9
# File 'lib/http_utilities/http/response.rb', line 7

def body
  @body
end

#force_encoding ⇒ Object

Returns the value of attribute force_encoding.



7
8
9
# File 'lib/http_utilities/http/response.rb', line 7

def force_encoding
  @force_encoding
end

#format ⇒ Object

Returns the value of attribute format.



7
8
9
# File 'lib/http_utilities/http/response.rb', line 7

def format
  @format
end

#page ⇒ Object

Returns the value of attribute page.



7
8
9
# File 'lib/http_utilities/http/response.rb', line 7

def page
  @page
end

#parsed_body ⇒ Object

Returns the value of attribute parsed_body.



7
8
9
# File 'lib/http_utilities/http/response.rb', line 7

def parsed_body
  @parsed_body
end

#request ⇒ Object

Returns the value of attribute request.



7
8
9
# File 'lib/http_utilities/http/response.rb', line 7

def request
  @request
end

Instance Method Details

#encode ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/http_utilities/http/response.rb', line 24

def encode
  if (self.body)
    begin
      self.body = self.body.force_encoding('UTF-8').encode("UTF-8", :invalid => :replace, :undef => :replace, :replace => "")
    rescue Exception => e
      log(:error, "[HttpUtilities::Http::Format] - Failed to convert response with String#encode. Error: #{e.class.name}. Message: #{e.message}.")
    end
  end
end

#parse_response ⇒ Object



34
35
36
# File 'lib/http_utilities/http/response.rb', line 34

def parse_response
  self.send("as_#{self.format}".to_sym) if (self.body && self.format)
end

#set_page(page) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/http_utilities/http/response.rb', line 38

def set_page(page)
  self.page = page

  if (page && page.parser)
    self.body         =   page.parser.content
    self.parsed_body  =   page.parser
  end
end