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

Returns a new instance of Response.



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

def initialize(response: nil, request: nil, options: {})
  self.status           =   (response && response.status) ? response.status : nil
  self.body             =   (response && response.body) ? response.body : nil
  self.request          =   request

  self.parsed_body      =   nil

  self.format           =   options.fetch(:format, nil)
  self.force_encoding   =   options.fetch(:force_encoding, 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

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#as_htmlObject



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

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

#as_jsonObject



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

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

#as_xmlObject



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

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

#encodeObject



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

def encode
  if self.body && self.body.is_a?(String)
    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



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

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

#set_page(page) ⇒ Object



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

def set_page(page)
  self.page           =   page

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