module ActiveScraper
Constants
- VERSION
Public Class Methods
build_usable_response(request, response)
click to toggle source
Returns an object compatible with HTTParty, i.e. an ActiveScraper::FakeHTTPartyResponse
to be deprecated
# File lib/active_scraper.rb, line 63 def self.build_usable_response(request, response) ActiveScraper::FakeHTTPartyResponse.new(request, response) end
create_request_and_fetch_response(uri, opts={})
click to toggle source
# File lib/active_scraper.rb, line 46 def self.create_request_and_fetch_response(uri, opts={}) opts = normalize_hash(opts) # first, find or build the request request = find_or_build_request(uri, opts) # then find or build a matching response response = find_or_build_response(request, opts) # associate and save the two request.responses << response request.save obj = Hashie::Mash.new(request: request, response: response) return obj end
fetch_fresh(url, opts={})
click to toggle source
# File lib/active_scraper.rb, line 69 def self.fetch_fresh(url, opts={}) resp = HTTParty.get(url, opts) return ActiveScraper::ResponseObject.factory(resp) end
find_or_build_request(req, opts={})
click to toggle source
delegates to CachedRequest::find_or_build_from_uri
req (URI or String). If CachedRequest, is idempotent
returns a new or existing CachedRequest
# File lib/active_scraper.rb, line 23 def self.find_or_build_request(req, opts={}) CachedRequest.find_or_build_from_uri(req, opts) end
find_or_build_response(cached_request, opts={})
click to toggle source
cached_request (CachedRequest) => the request to find a response for
returns a new or existing CachedResponse
# File lib/active_scraper.rb, line 31 def self.find_or_build_response(cached_request, opts={}) raise ArgumentError, "Only accepted CachedRequest, but was passed in a #{cached_request.class}" unless cached_request.is_a?(CachedRequest) opts = normalize_hash(opts) response = CachedResponse.find_cache_for_cached_request(cached_request, opts) if response.blank? fetched_obj = fetch_fresh(cached_request.uri, opts) response = CachedResponse.build_from_response_object(fetched_obj) end return response end
get(uri, options={})
click to toggle source
returns a ActiveScraper::CachedResponse
# File lib/active_scraper.rb, line 11 def self.get(uri, options={}) o = create_request_and_fetch_response(uri, options) return o.response end
normalize_hash(hsh)
click to toggle source
# File lib/active_scraper.rb, line 78 def self.normalize_hash(hsh) unless hsh.is_a?(HashWithIndifferentAccess) hsh = HashWithIndifferentAccess.new(hsh) end return hsh end