Class: OpenGraphReader::Fetcher Private
- Inherits:
-
Object
- Object
- OpenGraphReader::Fetcher
- Defined in:
- lib/open_graph_reader/fetcher.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Fetch an URI to retrieve its HTML body, if available.
Instance Method Summary collapse
-
#body ⇒ String
private
Retrieve the body.
-
#fetch ⇒ Faraday::Response
(also: #fetch_body)
private
Fetch the full page.
-
#fetch_headers ⇒ Faraday::Response
private
Fetch just the headers.
-
#fetched? ⇒ Bool
(also: #fetched_body?)
private
Whether the target URI was fetched.
-
#fetched_headers? ⇒ Bool
private
Whether the headers of the target URI were fetched.
-
#html? ⇒ Bool
private
Whether the target URI seems to return HTML.
-
#initialize(uri) ⇒ Fetcher
constructor
private
Create a new fetcher.
-
#url ⇒ String
private
The URL to fetch.
Constructor Details
#initialize(uri) ⇒ Fetcher
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a new fetcher.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/open_graph_reader/fetcher.rb', line 11 def initialize uri raise ArgumentError, "url needs to be an instance of URI" unless uri.is_a? URI @uri = uri @connection = Faraday.default_connection.dup if defined? FaradayMiddleware unless @connection.builder.handlers.include? FaradayMiddleware::FollowRedirects @connection.builder.insert(0, FaradayMiddleware::FollowRedirects) end end end |
Instance Method Details
#body ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Custom error class
Retrieve the body
50 51 52 53 54 |
# File 'lib/open_graph_reader/fetcher.rb', line 50 def body fetch_body unless fetched? raise ArgumentError, "Did not receive a HTML site at #{@uri}" unless html? @get_response.body end |
#fetch ⇒ Faraday::Response Also known as: fetch_body
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Fetch the full page.
33 34 35 |
# File 'lib/open_graph_reader/fetcher.rb', line 33 def fetch @get_response = @connection.get(@uri) end |
#fetch_headers ⇒ Faraday::Response
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Fetch just the headers
41 42 43 |
# File 'lib/open_graph_reader/fetcher.rb', line 41 def fetch_headers @head_response = @connection.head(@uri) end |
#fetched? ⇒ Bool Also known as: fetched_body?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Whether the target URI was fetched.
70 71 72 |
# File 'lib/open_graph_reader/fetcher.rb', line 70 def fetched? !@get_response.nil? end |
#fetched_headers? ⇒ Bool
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Whether the headers of the target URI were fetched.
78 79 80 |
# File 'lib/open_graph_reader/fetcher.rb', line 78 def fetched_headers? !@get_response.nil? || !@head_response.nil? end |
#html? ⇒ Bool
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Whether the target URI seems to return HTML
59 60 61 62 63 64 65 |
# File 'lib/open_graph_reader/fetcher.rb', line 59 def html? fetch_headers unless fetched_headers? response = @get_response || @head_response return false unless response.success? return false unless response['content-type'] response['content-type'].include? 'text/html' end |
#url ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The URL to fetch
26 27 28 |
# File 'lib/open_graph_reader/fetcher.rb', line 26 def url @uri.to_s end |