Class: Panchira::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/panchira/resolvers/resolver.rb

Overview

Resolver is a class that actually get attributes by fetching designated url. This class is the default resolver for pages.
To create your own resolver, first you make a class that extends Resolver, and then register it by ::Panchira::Extensions::register(). Then ::Panchira::fetch will pick up your resolver when Resolver::applicable?() is true.

Constant Summary collapse

URL_REGEXP =

URL pattern that a resolver tries to resolve. You must override this in subclasses to limit which urls to resolve.

URI::DEFAULT_PARSER.make_regexp

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = nil) ⇒ Resolver

Returns a new instance of Resolver.



14
15
16
17
# File 'lib/panchira/resolvers/resolver.rb', line 14

def initialize(url, options = nil)
  @url = url
  @options = options
end

Class Method Details

.applicable?(url) ⇒ Boolean

Tell whether the url is applicable for this resolver. ::Panchira::fetch uses this method to choose a Resolver for a URL.

Returns:

  • (Boolean)


49
50
51
# File 'lib/panchira/resolvers/resolver.rb', line 49

def applicable?(url)
  url =~ self::URL_REGEXP
end

Instance Method Details

#fetchObject

This function is called right after this Resolver instance is made. Fetch page from @url and return PanchiraResult.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/panchira/resolvers/resolver.rb', line 21

def fetch
  result = PanchiraResult.new

  @page = fetch_page(@url)
  result.canonical_url = parse_canonical_url

  @page = fetch_page(result.canonical_url) if @url != result.canonical_url

  after_fetch if respond_to?(:after_fetch, true)

  result.title = parse_title
  result.description = parse_description
  result.image = parse_image
  result.tags = parse_tags
  if respond_to?(:parse_authors, true)
    result.authors = parse_authors
  else
    result.author = parse_author
  end
  result.circle = parse_circle
  result.resolver = parse_resolver

  result
end