Class: HttpUtilities::Http::Response
- Inherits:
-
Object
- Object
- HttpUtilities::Http::Response
- Includes:
- Logger
- Defined in:
- lib/http_utilities/http/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#force_encoding ⇒ Object
Returns the value of attribute force_encoding.
-
#format ⇒ Object
Returns the value of attribute format.
-
#page ⇒ Object
Returns the value of attribute page.
-
#parsed_body ⇒ Object
Returns the value of attribute parsed_body.
-
#request ⇒ Object
Returns the value of attribute request.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #as_html ⇒ Object
- #as_json ⇒ Object
- #as_xml ⇒ Object
- #encode ⇒ Object
-
#initialize(response: nil, request: nil, options: {}) ⇒ Response
constructor
A new instance of Response.
- #parse_response ⇒ Object
- #set_page(page) ⇒ Object
Methods included from Logger
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 = .fetch(:format, nil) self.force_encoding = .fetch(:force_encoding, true) encode if self.force_encoding parse_response end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
6 7 8 |
# File 'lib/http_utilities/http/response.rb', line 6 def body @body end |
#force_encoding ⇒ Object
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 |
#format ⇒ Object
Returns the value of attribute format.
6 7 8 |
# File 'lib/http_utilities/http/response.rb', line 6 def format @format end |
#page ⇒ Object
Returns the value of attribute page.
6 7 8 |
# File 'lib/http_utilities/http/response.rb', line 6 def page @page end |
#parsed_body ⇒ Object
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 |
#request ⇒ Object
Returns the value of attribute request.
6 7 8 |
# File 'lib/http_utilities/http/response.rb', line 6 def request @request end |
#status ⇒ Object
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_html ⇒ Object
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_json ⇒ Object
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_xml ⇒ Object
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 |
#encode ⇒ Object
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.}.") end end end |
#parse_response ⇒ Object
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 |