class RichUrls::UrlFetcher

Constants

DEFAULT_TIMEOUT

Public Class Methods

fetch( url, attributes = [], browser: PatronBrowser.new, cache_time: nil ) click to toggle source
# File lib/url_fetcher.rb, line 9
def self.fetch(
  url,
  attributes = [],
  browser: PatronBrowser.new,
  cache_time: nil
)
  new(url, attributes, browser, cache_time).fetch
end
new(url, attributes, browser, cache_time) click to toggle source
# File lib/url_fetcher.rb, line 20
def initialize(url, attributes, browser, cache_time)
  @url = url
  @attributes = attributes
  @browser = browser
  @cache_time = cache_time
end

Public Instance Methods

fetch() click to toggle source
# File lib/url_fetcher.rb, line 27
def fetch
  cached = RichUrls.cache.get(digest)

  if cached
    RichUrls.cache.extend(digest, @cache_time)
    Oj.load(cached)
  else
    patron_call
  end
end

Private Instance Methods

digest() click to toggle source
# File lib/url_fetcher.rb, line 40
def digest
  @digest ||= Digest::MD5.hexdigest(@url + @attributes.sort.join('-'))
end
patron_call() click to toggle source
# File lib/url_fetcher.rb, line 44
def patron_call
  status, return_url, body = @browser.remote_call(@url)

  if status < 400
    decorated = BodyDecorator.decorate(return_url, body, @attributes)
    RichUrls.cache.set(digest, Oj.dump(decorated), @cache_time)
    decorated
  else
    raise UrlFetcherError, 'url cannot be found'
  end
end