Class: Earl

Inherits:
Object
  • Object
show all
Defined in:
lib/earl/earl.rb,
lib/earl.rb,
lib/earl/version.rb

Overview

Earl is a class that represents a URL and provides methods to fetch metadata about the page

Defined Under Namespace

Classes: Scraper

Constant Summary collapse

VERSION =
'2.0.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Earl

Returns a new instance of Earl.



8
9
10
11
# File 'lib/earl/earl.rb', line 8

def initialize(url, options = {})
  @url = url
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Dispatch missing methods if a match for:

  • uri_response_attributes

  • scraper attributes



77
78
79
80
81
82
83
84
85
# File 'lib/earl/earl.rb', line 77

def method_missing(method, *args)
  if uri_response_attributes.include?(method)
    uri_response_attribute(method)
  elsif scraper&.attribute?(method)
    scraper.attribute(method)
  else
    super
  end
end

Instance Attribute Details

#oembed(options = nil) ⇒ Object

Returns the oembed meta data hash for the URL (or nil if not defined/available) e.g. for www.youtube.com/watch?v=hNSkCqMUMQA:

:title=>"[JA][Keynote] Ruby Taught Me About Encoding Under the Hood / Mari Imaizumi @ima1zumi",
:author_name=>"RubyKaigi",
:author_url=>"https://www.youtube.com/@rubykaigi4884",
:type=>"video",
:height=>113,
:width=>200,
:version=>"1.0",
:provider_name=>"YouTube",
:provider_url=>"https://www.youtube.com/",
:thumbnail_height=>360,
:thumbnail_width=>480,
:thumbnail_url=>"https://i.ytimg.com/vi/hNSkCqMUMQA/hqdefault.jpg",
:html=> "<iframe width=\"200\" height=\"113\" src=\"https://www.youtube.com/embed/hNSkCqMUMQA?feature=oembed\" \
  frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" \
  referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen \
  title=\"[JA][Keynote] Ruby Taught Me About Encoding Under the Hood / Mari Imaizumi @ima1zumi\"></iframe>"

options defines a custom oembed options hash and will cause a re-fetch of the oembed metadata



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/earl/earl.rb', line 119

def oembed(options = nil)
  if options # use custom options, refetch oembed metadata
    @options[:oembed] = options
    @oembed = nil
  end
  @oembed ||= begin
    h = fetch_oembed(base_url).fields
    if h
      h.keys.each do |key| # symbolize_keys!
        new_key = begin
          key.to_sym
        rescue StandardError
          key
        end
        h[new_key] = h.delete(key)
      end
      h
    end
  rescue StandardError
    nil
  end
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/earl/earl.rb', line 5

def options
  @options
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/earl/earl.rb', line 5

def url
  @url
end

Class Method Details

.[](url) ⇒ Object



169
170
171
# File 'lib/earl/earl.rb', line 169

def self.[](url)
  new(url)
end

Instance Method Details

#attributesObject

Returns a full array of attributes available for the link



88
89
90
# File 'lib/earl/earl.rb', line 88

def attributes
  scraper.attributes.keys + uri_response_attributes + [:feed]
end

#feed(prefer = :rss) ⇒ Object

Returns the feed URL associated with this URL. Returns RSS by default, or ATOM if prefer is not :rss.



159
160
161
162
163
164
165
166
167
# File 'lib/earl/earl.rb', line 159

def feed(prefer = :rss)
  rss = rss_feed
  atom = atom_feed
  if rss && atom
    prefer == :rss ? rss : atom
  else
    rss || atom
  end
end

#feed?Boolean

Returns true if there is an ATOM or RSS feed associated with this URL.

Returns:

  • (Boolean)


153
154
155
# File 'lib/earl/earl.rb', line 153

def feed?
  !feed.nil?
end

#metadataObject

Returns a hash of link meta data, including: :title, :description, :image (all attributes) :base_url



60
61
62
63
64
65
66
67
68
# File 'lib/earl/earl.rb', line 60

def 
  data = oembed || {}
  attributes.each do |attribute|
    if attribute_value = send(attribute)
      data[attribute] ||= attribute_value
    end
  end
  data
end

#oembed_htmlObject

Returns the oembed code for the url (or nil if not defined/available)



148
149
150
# File 'lib/earl/earl.rb', line 148

def oembed_html
  oembed && oembed[:html]
end

#oembed_optionsObject

Returns the options to be used for oembed



93
94
95
# File 'lib/earl/earl.rb', line 93

def oembed_options
  { maxwidth: '560', maxheight: '315' }.merge(options[:oembed] || {})
end

#respond_to_missing?(name, include_private) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/earl/earl.rb', line 70

def respond_to_missing?(name, include_private)
  uri_response_attributes.include?(name) || scraper&.attribute?(name) || super
end

#responseObject



53
54
55
# File 'lib/earl/earl.rb', line 53

def response
  scraper&.response
end

#scraperObject



49
50
51
# File 'lib/earl/earl.rb', line 49

def scraper
  @scraper ||= Scraper.for(url, self)
end

#to_sObject



13
14
15
# File 'lib/earl/earl.rb', line 13

def to_s
  url
end

#uriObject



17
18
19
# File 'lib/earl/earl.rb', line 17

def uri
  @uri ||= URI.parse(url)
end

#uri_responseObject



21
22
23
# File 'lib/earl/earl.rb', line 21

def uri_response
  @uri_response ||= uri.open
end