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:

Raises:



11
12
13
14
# File 'lib/grubby/json_scraper.rb', line 11

def initialize(source)
  @json = source.assert!(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")  # === MyScraper

Parameters:

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

Returns:

Raises:



31
32
33
# File 'lib/grubby/json_scraper.rb', line 31

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