Class: HttpUtilities::Http::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#log

Constructor Details

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



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

def initialize(response = nil, request = nil, options = {})
  options               =   options.dup

  self.body             =   (response && response.body) ? response.body : nil
  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

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#force_encodingObject

Returns the value of attribute force_encoding.



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

def force_encoding
  @force_encoding
end

#formatObject

Returns the value of attribute format.



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

def format
  @format
end

#pageObject

Returns the value of attribute page.



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

def page
  @page
end

#parsed_bodyObject

Returns the value of attribute parsed_body.



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

def parsed_body
  @parsed_body
end

#requestObject

Returns the value of attribute request.



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

def request
  @request
end

Instance Method Details

#as_htmlObject



37
38
39
# File 'lib/http_utilities/http/response.rb', line 37

def as_html
  self.parsed_body = (self.body && self.body != "") ? Nokogiri::HTML(self.body.to_s.force_encoding("utf-8"), nil, "utf-8") : nil
end

#as_jsonObject



45
46
47
# File 'lib/http_utilities/http/response.rb', line 45

def as_json
  self.parsed_body = (self.body && self.body != "") ? self.body.to_s.force_encoding("utf-8").to_json : nil
end

#as_xmlObject



41
42
43
# File 'lib/http_utilities/http/response.rb', line 41

def as_xml
  self.parsed_body = (self.body && self.body != "") ? Nokogiri::XML(self.body.to_s.force_encoding("utf-8"), nil, "utf-8") : nil
end

#encodeObject



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

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_responseObject



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

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

#set_page(page) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/http_utilities/http/response.rb', line 49

def set_page(page)
  self.page = page

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