1+ # frozen_string_literal: true
2+
13require 'open-uri'
24require 'oembedr'
35
6+ # Earl is a class that represents a URL and provides methods to fetch metadata about the page
47class Earl
5- attr_accessor :url , :options , :oembed
8+ attr_accessor :url , :options
9+ attr_writer :oembed
610
7- def initialize ( url , options = { } )
11+ def initialize ( url , options = { } )
812 @url = url
913 @options = options
1014 end
@@ -18,7 +22,7 @@ def uri
1822 end
1923
2024 def uri_response
21- @uri_response ||= URI . open ( uri )
25+ @uri_response ||= uri . open
2226 end
2327
2428 # Returns
@@ -35,13 +39,13 @@ def uri_response_attribute(name)
3539 when :headers
3640 uri_response_attribute ( :meta )
3741 else
38- uri_response && uri_response . respond_to? ( name ) && uri_response . send ( name )
42+ uri_response . respond_to? ( name ) && uri_response . send ( name )
3943 end
4044 end
4145 protected :uri_response_attribute
4246
4347 def uri_response_attributes
44- [ : content_type, : base_url, : charset, : content_encoding, : headers]
48+ %i[ content_type base_url charset content_encoding headers ]
4549 end
4650 protected :uri_response_attributes
4751
@@ -50,7 +54,7 @@ def scraper
5054 end
5155
5256 def response
53- scraper && scraper . response
57+ scraper & .response
5458 end
5559
5660 # Returns a hash of link meta data, including:
@@ -59,23 +63,28 @@ def response
5963 def metadata
6064 data = oembed || { }
6165 attributes . each do |attribute |
62- if attribute_value = self . send ( attribute )
66+ if attribute_value = send ( attribute )
6367 data [ attribute ] ||= attribute_value
6468 end
6569 end
6670 data
6771 end
6872
73+ def respond_to_missing? ( name , include_private )
74+ uri_response_attributes . include? ( name ) || scraper &.attribute? ( name ) || super
75+ end
76+
6977 # Dispatch missing methods if a match for:
7078 # - uri_response_attributes
7179 # - scraper attributes
7280 def method_missing ( method , *args )
7381 if uri_response_attributes . include? ( method )
74- return uri_response_attribute ( method )
75- elsif scraper && scraper . has_attribute? ( method )
76- return scraper . attribute ( method )
82+ uri_response_attribute ( method )
83+ elsif scraper &.attribute? ( method )
84+ scraper . attribute ( method )
85+ else
86+ super
7787 end
78- super
7988 end
8089
8190 # Returns a full array of attributes available for the link
@@ -85,7 +94,7 @@ def attributes
8594
8695 # Returns the options to be used for oembed
8796 def oembed_options
88- { : maxwidth => " 560" , : maxheight => " 315" } . merge ( options [ :oembed ] || { } )
97+ { maxwidth : ' 560' , maxheight : ' 315' } . merge ( options [ :oembed ] || { } )
8998 end
9099
91100 # Returns the oembed meta data hash for the URL (or nil if not defined/available) e.g.
@@ -108,21 +117,27 @@ def oembed_options
108117 #
109118 # +options+ defines a custom oembed options hash and will cause a re-fetch of the oembed metadata
110119 # TODO: Oembedr is outdated and not longer works with most/all providers
111- def oembed ( options = nil )
120+ def oembed ( options = nil )
112121 if options # use custom options, refetch oembed metadata
113122 @options [ :oembed ] = options
114123 @oembed = nil
115124 end
116- begin
117- @oembed ||= if h = Oembedr . fetch ( base_url , :params => oembed_options ) . body
125+ @oembed ||= begin
126+ h = Oembedr . fetch ( base_url , params : oembed_options ) . body
127+ if h
118128 h . keys . each do |key | # symbolize_keys!
119- h [ ( key . to_sym rescue key ) || key ] = h . delete ( key )
129+ new_key = begin
130+ key . to_sym
131+ rescue StandardError
132+ key
133+ end
134+ h [ new_key ] = h . delete ( key )
120135 end
121136 h
122137 end
123- rescue
138+ rescue StandardError
139+ nil
124140 end
125- @oembed
126141 end
127142
128143 # Returns the oembed code for the url (or nil if not defined/available)
@@ -131,7 +146,7 @@ def oembed_html
131146 end
132147
133148 # Returns true if there is an ATOM or RSS feed associated with this URL.
134- def has_feed ?
149+ def feed ?
135150 !feed . nil?
136151 end
137152
0 commit comments