Class: Earl

Inherits:
Object
  • Object
show all
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

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



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
  @oembed
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#urlObject

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

#attributesObject

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.

Returns:

  • (Boolean)


134
135
136
# File 'lib/earl/earl.rb', line 134

def has_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 = self.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)



129
130
131
# File 'lib/earl/earl.rb', line 129

def oembed_html
  oembed && oembed[:html]
end

#oembed_optionsObject

Returns the options to be used for oembed



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

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

#responseObject



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

def response
  scraper && 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 ||= open(uri)
end