Class: Flickrcaptionr::Fetchers::OEmbed

Inherits:
Base
  • Object
show all
Defined in:
lib/flickrcaptionr/fetchers/oembed.rb

Instance Method Summary collapse

Methods inherited from Base

#download_file

Constructor Details

#initializeOEmbed

Returns a new instance of OEmbed.



3
4
5
# File 'lib/flickrcaptionr/fetchers/oembed.rb', line 3

def initialize
  OEmbed::Providers.register_all
end

Instance Method Details

#can_handle_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
# File 'lib/flickrcaptionr/fetchers/oembed.rb', line 18

def can_handle_url?(url)
  OEmbed::Providers.urls.each_pair do |k,v|
    if k.match(url)
      return true
    end
  end
  return false
end

#fetch(url) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/flickrcaptionr/fetchers/oembed.rb', line 6

def fetch(url)
  # OEmbed::Providers.get("https://www.youtube.com/watch?v=mNrXMOSkBas")
  # Try and fetch this URL
  resp = OEmbed::Providers.get(url)
  # Now figure out what the heck in our oembed response is actually the image we're seeking
  if resp
    return resp.url if (resp.url rescue nil)
    return resp.thumbnail_url if (resp.thumbnail_url rescue nil)
  end
  # If we're here, we either didn't retrieve any suitable image URL, or our provider broke
  raise Flickrcaptionr::RequestNotFetchableException, "Could not retrieve #{url}"
end