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) ⇒ 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:

Returns:



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

def self.scrape_file(path)
  uri = URI.join("file:///", File.expand_path(path))
  body = File.read(path)
  self.new(Grubby::JsonParser.new(uri, nil, body, "200"))
end