Module: RecipeScrapers::Registry
- Defined in:
- lib/recipe_scrapers/registry.rb
Overview
Maps hosts to the sites the gem supports. A host maps to a Declaration, or to a Scraper subclass for a site that needs code. A leading "www." is ignored.
Class Method Summary collapse
-
.clear! ⇒ void
private
Forgets every registered site.
-
.entries ⇒ Hash{String => Declaration, Class}
private
Every registered host and what reads it.
-
.for(url) ⇒ Declaration, ...
Finds what reads a URL.
-
.hosts ⇒ Array<String>
Every supported host.
-
.register(host, also: []) { ... } ⇒ Declaration
Registers a site by declaration.
-
.register_class(klass) ⇒ Class<Scraper>
Registers a Scraper subclass under the host it declares with Scraper.host.
-
.restore(saved) ⇒ void
private
Puts back entries a Registry.snapshot took.
-
.snapshot ⇒ Hash{String => Declaration, Class}
private
A copy of the entries, to put back with Registry.restore.
Class Method Details
.clear! ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Forgets every registered site.
58 59 60 |
# File 'lib/recipe_scrapers/registry.rb', line 58 def clear! @entries = {} end |
.entries ⇒ Hash{String => Declaration, Class}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns every registered host and what reads it.
13 14 15 |
# File 'lib/recipe_scrapers/registry.rb', line 13 def entries @entries ||= {} end |
.for(url) ⇒ Declaration, ...
Finds what reads a URL.
42 43 44 45 46 |
# File 'lib/recipe_scrapers/registry.rb', line 42 def for(url) entries[normalize(URI.parse(url).host.to_s)] rescue URI::Error nil end |
.hosts ⇒ Array<String>
Returns every supported host.
49 50 51 |
# File 'lib/recipe_scrapers/registry.rb', line 49 def hosts entries.keys end |
.register(host, also: []) { ... } ⇒ Declaration
Registers a site by declaration.
23 24 25 26 27 |
# File 'lib/recipe_scrapers/registry.rb', line 23 def register(host, also: [], &) declaration = Declaration.build(host, &) ([host] + Array(also)).each { |name| entries[normalize(name)] = declaration } declaration end |
.register_class(klass) ⇒ Class<Scraper>
Registers a Scraper subclass under the host it declares with Scraper.host.
33 34 35 36 |
# File 'lib/recipe_scrapers/registry.rb', line 33 def register_class(klass) entries[normalize(klass.declared_host)] = klass klass end |
.restore(saved) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Puts back entries a snapshot took.
77 78 79 |
# File 'lib/recipe_scrapers/registry.rb', line 77 def restore(saved) @entries = saved end |
.snapshot ⇒ Hash{String => Declaration, Class}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
A copy of the entries, to put back with restore. Specs use it to register a site for one example.
67 68 69 |
# File 'lib/recipe_scrapers/registry.rb', line 67 def snapshot entries.dup end |