Method: Grubby::Scraper.scrape

Defined in:
lib/grubby/scraper.rb

.scrape(url, agent = $grubby) ⇒ Grubby::Scraper

Instantiates the Scraper class with the resource indicated by url. This method acts as a default factory method, and provides a standard interface for overrides.

Examples:

Default factory method

class PostPageScraper < Grubby::PageScraper
  # ...
end

PostPageScraper.scrape("https://example.com/posts/42")
  # == PostPageScraper.new($grubby.get("https://example.com/posts/42"))

Override factory method

class PostApiScraper < Grubby::JsonScraper
  # ...

  def self.scrape(url, agent = $grubby)
    api_url = url.to_s.sub(%r"//example.com/(.+)", '//api.example.com/\1.json')
    super(api_url, agent)
  end
end

PostApiScraper.scrape("https://example.com/posts/42")
  # == PostApiScraper.new($grubby.get("https://api.example.com/posts/42.json"))

Parameters:

  • url (String, URI)
  • agent (Mechanize) (defaults to: $grubby)

Returns:

Raises:

[View source]

139
140
141
# File 'lib/grubby/scraper.rb', line 139

def self.scrape(url, agent = $grubby)
  self.new(agent.get(url))
end