Class: Grubby::JsonScraper

Inherits:
Scraper
  • Object
show all
Defined in:
lib/grubby/json_scraper.rb

Instance Attribute Summary collapse

Attributes inherited from Scraper

#errors, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Scraper

#[], each, fields, scrape, scrapes, #to_h

Constructor Details

#initialize(source) ⇒ JsonScraper

Returns a new instance of JsonScraper.

Parameters:



9
10
11
12
# File 'lib/grubby/json_scraper.rb', line 9

def initialize(source)
  @json = source.assert_kind_of!(Grubby::JsonParser).json
  super
end

Instance Attribute Details

#jsonHash, Array (readonly)

The parsed JSON data being scraped.

Returns:

  • (Hash, Array)


6
7
8
# File 'lib/grubby/json_scraper.rb', line 6

def json
  @json
end

Class Method Details

.scrape_file(path, agent = $grubby) ⇒ Grubby::JsonScraper

Scrapes a locally-stored file. This method is intended for use with subclasses of Grubby::JsonScraper.

Examples:

class MyScraper < Grubby::JsonScraper
  # ...
end

MyScraper.scrape_file("path/to/local_file.json").class  # == MyScraper

Parameters:

  • path (String)
  • agent (Mechanize) (defaults to: $grubby)

Returns:



27
28
29
# File 'lib/grubby/json_scraper.rb', line 27

def self.scrape_file(path, agent = $grubby)
  self.new(Grubby::JsonParser.read_local(path).tap{|parser| parser.mech = agent })
end