Class: ContentOMatic::Response
- Inherits:
-
Object
- Object
- ContentOMatic::Response
- Defined in:
- lib/content_o_matic/response.rb
Overview
This class wraps the response received from pulling down content from a web page.
Instance Attribute Summary collapse
-
#body(normalize_assets = false) ⇒ Object
Returns the ‘full’ HTML body of the requested page.
-
#status ⇒ Object
The status of the response.
-
#url ⇒ Object
The url of the requested page.
Instance Method Summary collapse
-
#exception ⇒ Object
Returns a ContentOMatic::InvalidResponseError exception if the response was not a success.
-
#has_layout? ⇒ Boolean
Returns
true
if the page is wrapped inhtml
tags. -
#html_body(normalize_assets = false) ⇒ Object
Returns just the content within the HTML ‘body’ tag.
-
#initialize(url, status, body = '') ⇒ Response
constructor
Takes the url of the requested page, the status code of the response, and the body of the response.
-
#success? ⇒ Boolean
Returns
true
if the status of the page was 200. -
#to_s ⇒ Object
:nodoc:.
-
#to_str ⇒ Object
:nodoc:.
Constructor Details
#initialize(url, status, body = '') ⇒ Response
Takes the url of the requested page, the status code of the response, and the body of the response.
14 15 16 17 18 |
# File 'lib/content_o_matic/response.rb', line 14 def initialize(url, status, body = '') self.url = url self.status = status.to_i self.body = body end |
Instance Attribute Details
#body(normalize_assets = false) ⇒ Object
Returns the ‘full’ HTML body of the requested page. If you pass in true
it will normalize all the links found in the body.
Example:
# http://www.example.com/foo/bar.html
<img src='image.jpg'> # => <img src='http://www.example.com/foo/image.jpg'>
<img src='/image.jpg'> # => <img src='http://www.example.com/image.jpg'>
<img src='http://www.example.org/image.jpg'> # => <img src='http://www.example.org/image.jpg'>
The following tags get ‘normalized’ with the true
parameter:
img
script
link
a
iframe
form
object
8 9 10 |
# File 'lib/content_o_matic/response.rb', line 8 def body @body end |
#status ⇒ Object
The status of the response
6 7 8 |
# File 'lib/content_o_matic/response.rb', line 6 def status @status end |
#url ⇒ Object
The url of the requested page
10 11 12 |
# File 'lib/content_o_matic/response.rb', line 10 def url @url end |
Instance Method Details
#exception ⇒ Object
Returns a ContentOMatic::InvalidResponseError exception if the response was not a success.
93 94 95 |
# File 'lib/content_o_matic/response.rb', line 93 def exception ContentOMatic::InvalidResponseError.new("URL: '#{self.url}' did not return a valid response! Status: '#{self.status}'") unless self.success? end |
#has_layout? ⇒ Boolean
Returns true
if the page is wrapped in html
tags.
34 35 36 |
# File 'lib/content_o_matic/response.rb', line 34 def has_layout? return self.body != self.html_body end |
#html_body(normalize_assets = false) ⇒ Object
Returns just the content within the HTML ‘body’ tag. If there is no ‘body’ tag, then the whole response body is returned. If you pass in true
it will normalize all the links found in the body. See the body method for more details.
82 83 84 85 86 87 88 89 |
# File 'lib/content_o_matic/response.rb', line 82 def html_body(normalize_assets = false) unless @__doc_body doc = Nokogiri::HTML(self.body(normalize_assets)) @__doc_body = doc.at('body') @__doc_body = @__doc_body.nil? ? self.body(normalize_assets) : @__doc_body.inner_html end return @__doc_body end |
#success? ⇒ Boolean
Returns true
if the status of the page was 200
21 22 23 |
# File 'lib/content_o_matic/response.rb', line 21 def success? self.status == 200 end |
#to_s ⇒ Object
:nodoc:
25 26 27 |
# File 'lib/content_o_matic/response.rb', line 25 def to_s # :nodoc: self.success? ? self.body : '' end |
#to_str ⇒ Object
:nodoc:
29 30 31 |
# File 'lib/content_o_matic/response.rb', line 29 def to_str # :nodoc: self.to_s end |