Module: Embedify

Defined in:
lib/embedify.rb

Defined Under Namespace

Classes: Object

Class Method Summary collapse

Class Method Details

.fetch(uri, options = {}) ⇒ Object

Fetch Open Graph data from the specified URI. Makes an HTTP GET request and returns an OpenGraph::Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/embedify.rb', line 13

def self.fetch(uri, options = {})
  # TODO: use the options hash
  # by default: do not load images    
  opengraph = begin
    html = get_with_redirects(uri)
    page = parse(html)
    page['url'] = html.env[:url].to_s unless page.include? 'url'
    page
  rescue Exception => e
    raise e
  end

  # check if the opengraph object is complete
  unless opengraph.valid?
    # TODO: break this out (call each attribute directly)
    # the opengraph object is lacking some mandatory attributes
    attributes = Embedify::Object::MANDATORY_ATTRIBUTES
    attributes.delete 'url'
    attributes.each do |attribute|
      self.send(attribute.to_sym, opengraph)
    end
  end

  # fill in extra attributes
  unless opengraph.include? 'description'
    self.description(opengraph)
  end
  opengraph
end