Class: Earl
- Inherits:
-
Object
- Object
- Earl
- 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
-
#oembed(options = nil) ⇒ Object
Returns the oembed meta data hash for the URL (or nil if not defined/available) e.g.
-
#options ⇒ Object
Returns the value of attribute options.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
-
#attributes ⇒ Object
Returns a full array of attributes available for the link.
-
#feed(prefer = :rss) ⇒ Object
Returns the feed URL associated with this URL.
-
#feed? ⇒ Boolean
Returns true if there is an ATOM or RSS feed associated with this URL.
-
#initialize(url, options = {}) ⇒ Earl
constructor
A new instance of Earl.
-
#metadata ⇒ Object
Returns a hash of link meta data, including: :title, :description, :image (all attributes) :base_url.
-
#method_missing(method, *args) ⇒ Object
Dispatch missing methods if a match for: - uri_response_attributes - scraper attributes.
-
#oembed_html ⇒ Object
Returns the oembed code for the url (or nil if not defined/available).
-
#oembed_options ⇒ Object
Returns the options to be used for oembed.
- #respond_to_missing?(name, include_private) ⇒ Boolean
- #response ⇒ Object
- #scraper ⇒ Object
- #to_s ⇒ Object
- #uri ⇒ Object
- #uri_response ⇒ Object
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, = {}) @url = url @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 ( = nil) if # use custom options, refetch oembed metadata @options[:oembed] = @oembed = nil end @oembed ||= begin h = (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 |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/earl/earl.rb', line 5 def @options end |
#url ⇒ Object
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
#attributes ⇒ Object
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.
153 154 155 |
# File 'lib/earl/earl.rb', line 153 def feed? !feed.nil? end |
#metadata ⇒ Object
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 = || {} attributes.each do |attribute| if attribute_value = send(attribute) data[attribute] ||= attribute_value end end data end |
#oembed_html ⇒ Object
Returns the oembed code for the url (or nil if not defined/available)
148 149 150 |
# File 'lib/earl/earl.rb', line 148 def && [:html] end |
#oembed_options ⇒ Object
Returns the options to be used for oembed
93 94 95 |
# File 'lib/earl/earl.rb', line 93 def { maxwidth: '560', maxheight: '315' }.merge([:oembed] || {}) end |
#respond_to_missing?(name, include_private) ⇒ 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 |
#response ⇒ Object
53 54 55 |
# File 'lib/earl/earl.rb', line 53 def response scraper&.response end |
#scraper ⇒ Object
49 50 51 |
# File 'lib/earl/earl.rb', line 49 def scraper @scraper ||= Scraper.for(url, self) end |
#to_s ⇒ Object
13 14 15 |
# File 'lib/earl/earl.rb', line 13 def to_s url end |
#uri ⇒ Object
17 18 19 |
# File 'lib/earl/earl.rb', line 17 def uri @uri ||= URI.parse(url) end |
#uri_response ⇒ Object
21 22 23 |
# File 'lib/earl/earl.rb', line 21 def uri_response @uri_response ||= uri.open end |