class OpenGraphReader::Fetcher

Fetch an URI to retrieve its HTML body, if available.

@api private

Constants

HEADERS

Public Class Methods

new(uri) click to toggle source

Create a new fetcher.

@param [URI] uri the URI to fetch.

# File lib/open_graph_reader/fetcher.rb, line 26
def initialize uri
  raise ArgumentError, "url needs to be an instance of URI" unless uri.is_a? URI
  @uri = uri
  @fetch_failed = false
  @connection = Faraday.default_connection.dup
  @connection.headers.replace(HEADERS)
  @head_response = nil
  @get_response = nil

  prepend_middleware Faraday::CookieJar if defined? Faraday::CookieJar
  prepend_middleware FaradayMiddleware::FollowRedirects if defined? FaradayMiddleware
end

Public Instance Methods

body() click to toggle source

Retrieve the body

@todo Custom error class @raise [ArgumentError] The received content does not seems to be HTML. @return [String]

# File lib/open_graph_reader/fetcher.rb, line 70
def body
  fetch_body unless fetched?
  raise NoOpenGraphDataError, "No response body received for #{@uri}" if fetch_failed?
  raise NoOpenGraphDataError, "Did not receive a HTML site at #{@uri}" unless html?
  @get_response.body
end
fetch() click to toggle source

Fetch the full page.

@return [Faraday::Response,nil]

# File lib/open_graph_reader/fetcher.rb, line 49
def fetch
  @get_response = @connection.get(@uri)
rescue Faraday::Error
  @fetch_failed = true
end
Also aliased as: fetch_body
fetch_body()
Alias for: fetch
fetch_headers() click to toggle source

Fetch just the headers

@return [Faraday::Response,nil]

# File lib/open_graph_reader/fetcher.rb, line 59
def fetch_headers
  @head_response = @connection.head(@uri)
rescue Faraday::Error
  @fetch_failed = true
end
fetched?() click to toggle source

Whether the target URI was fetched.

@return [Bool]

# File lib/open_graph_reader/fetcher.rb, line 93
def fetched?
  fetch_failed? || !@get_response.nil?
end
Also aliased as: fetched_body?
fetched_body?()
Alias for: fetched?
fetched_headers?() click to toggle source

Whether the headers of the target URI were fetched.

@return [Bool]

# File lib/open_graph_reader/fetcher.rb, line 101
def fetched_headers?
  fetch_failed? || !@get_response.nil? || !@head_response.nil?
end
html?() click to toggle source

Whether the target URI seems to return HTML

@return [Bool]

# File lib/open_graph_reader/fetcher.rb, line 80
def html?
  fetch_headers unless fetched_headers?
  response = @get_response || @head_response
  return false if fetch_failed?
  return false unless response
  return false unless response.success?
  return false unless response["content-type"]
  response["content-type"].include? "text/html"
end
url() click to toggle source

The URL to fetch

@return [String]

# File lib/open_graph_reader/fetcher.rb, line 42
def url
  @uri.to_s
end

Private Instance Methods

fetch_failed?() click to toggle source
# File lib/open_graph_reader/fetcher.rb, line 107
def fetch_failed?
  @fetch_failed
end
prepend_middleware(middleware) click to toggle source
# File lib/open_graph_reader/fetcher.rb, line 111
def prepend_middleware middleware
  return if @connection.builder.handlers.include? middleware

  @connection.builder.insert(0, middleware)
end