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

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.

Returns:

  • (Hash{String => Declaration, Class}) —

    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.

Parameters:

  • url (String)

Returns:

  • (Declaration, Class<Scraper>, nil) —

    nil when the host is not registered or the URL does not parse



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.

Returns:

  • (Array<String>) —

    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.

Parameters:

  • host (String) —

    the host of the site

  • also (Array<String>) (defaults to: []) —

    more hosts read the same way, such as a regional domain

Yields:

Returns:



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.

Parameters:

Returns:

  • (Class<Scraper>) —

    the class it was given



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.

Parameters:



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.

Returns:



67
68
69
# File 'lib/recipe_scrapers/registry.rb', line 67

def snapshot
  entries.dup
end