Class: Earl
- Inherits:
-
Object
- Object
- Earl
- Defined in:
- lib/earl.rb,
lib/earl/earl.rb,
lib/earl/version.rb
Defined Under Namespace
Classes: Scraper
Constant Summary collapse
- VERSION =
"1.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.
-
#has_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.
- #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
73 74 75 76 77 78 79 80 |
# File 'lib/earl/earl.rb', line 73 def method_missing(method, *args) if uri_response_attributes.include?(method) return uri_response_attribute(method) elsif scraper && scraper.has_attribute?(method) return scraper.attribute(method) end super 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=g3DCEcSlfhw:
{
"provider_url"=>"http://www.youtube.com/",
"thumbnail_url"=>"http://i4.ytimg.com/vi/g3DCEcSlfhw/hqdefault.jpg",
"title"=>"'Virtuosos of Guitar 2008' festival, Moscow. Marcin Dylla",
"html"=>"<iframe width=\"459\" height=\"344\" src=\"http://www.youtube.com/embed/g3DCEcSlfhw?fs=1&feature=oembed\" frameborder=\"0\" allowfullscreen></iframe>",
"author_name"=>"guitarmagnet",
"height"=>344,
"thumbnail_width"=>480,
"width"=>459,
"version"=>"1.0",
"author_url"=>"http://www.youtube.com/user/guitarmagnet",
"provider_name"=>"YouTube",
"type"=>"video",
"thumbnail_height"=>360
}
options
defines a custom oembed options hash and will cause a re-fetch of the oembed metadata
111 112 113 |
# File 'lib/earl/earl.rb', line 111 def @oembed end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/earl/earl.rb', line 6 def @options end |
#url ⇒ Object
Returns the value of attribute url.
6 7 8 |
# File 'lib/earl/earl.rb', line 6 def url @url end |
Class Method Details
.[](url) ⇒ Object
152 153 154 |
# File 'lib/earl/earl.rb', line 152 def [](url) new(url) end |
Instance Method Details
#attributes ⇒ Object
Returns a full array of attributes available for the link
83 84 85 |
# File 'lib/earl/earl.rb', line 83 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.
140 141 142 143 144 145 146 147 148 |
# File 'lib/earl/earl.rb', line 140 def feed(prefer = :rss) rss = rss_feed atom = atom_feed if rss && atom prefer == :rss ? rss : atom else rss || atom end end |
#has_feed? ⇒ Boolean
Returns true if there is an ATOM or RSS feed associated with this URL.
134 135 136 |
# File 'lib/earl/earl.rb', line 134 def has_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 = self.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)
129 130 131 |
# File 'lib/earl/earl.rb', line 129 def && [:html] end |
#oembed_options ⇒ Object
Returns the options to be used for oembed
88 89 90 |
# File 'lib/earl/earl.rb', line 88 def { :maxwidth => "560", :maxheight => "315" }.merge([:oembed]||{}) end |
#response ⇒ Object
53 54 55 |
# File 'lib/earl/earl.rb', line 53 def response scraper && 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 ||= open(uri) end |