Class: OpenGraphReader::Fetcher Private

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • uri (URI)

    the URI to fetch.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/open_graph_reader/fetcher.rb', line 19

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
    prepend_middleware FaradayMiddleware::FollowRedirects
  end

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

Instance Method Details

#bodyString

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.

TODO:

Custom error class

Retrieve the body

Returns:

  • (String)

Raises:

  • (ArgumentError)

    The received content does not seems to be HTML.



60
61
62
63
64
# File 'lib/open_graph_reader/fetcher.rb', line 60

def body
  fetch_body unless fetched?
  raise ArgumentError, "Did not receive a HTML site at #{@uri}" unless html?
  @get_response.body
end

#fetchFaraday::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.

Returns:

  • (Faraday::Response)


43
44
45
# File 'lib/open_graph_reader/fetcher.rb', line 43

def fetch
  @get_response = @connection.get(@uri)
end

#fetch_headersFaraday::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

Returns:

  • (Faraday::Response)


51
52
53
# File 'lib/open_graph_reader/fetcher.rb', line 51

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.

Returns:

  • (Bool)


80
81
82
# File 'lib/open_graph_reader/fetcher.rb', line 80

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.

Returns:

  • (Bool)


88
89
90
# File 'lib/open_graph_reader/fetcher.rb', line 88

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

Returns:

  • (Bool)


69
70
71
72
73
74
75
# File 'lib/open_graph_reader/fetcher.rb', line 69

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

#urlString

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

Returns:

  • (String)


36
37
38
# File 'lib/open_graph_reader/fetcher.rb', line 36

def url
  @uri.to_s
end