class FilmSnob::OembedProvider

Attributes

options[R]
url[R]

Public Class Methods

http() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 35
def http
  Net::HTTP.new(uri.host, uri.port).tap do |uri|
    uri.use_ssl = use_ssl?
  end
end
inherited(base) click to toggle source
# File lib/film_snob/oembed_provider.rb, line 23
def inherited(base)
  subclasses << base
end
new(url, options = {}) click to toggle source
# File lib/film_snob/oembed_provider.rb, line 8
def initialize(url, options = {})
  @url = url
  @options = friendly_options(options)
  ensure_match unless options.delete(:matched)
end
oembed_endpoint() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 31
def oembed_endpoint
  ""
end
subclasses() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 45
def subclasses
  @subclasses ||= []
end
use_ssl?() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 41
def use_ssl?
  "https" == uri.scheme
end
valid_url_patterns() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 27
def valid_url_patterns
  []
end

Private Class Methods

uri() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 51
def uri
  URI.parse(oembed_endpoint)
end

Public Instance Methods

html() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 60
def html
  lookup :html
end
id() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 14
def id
  @id ||= matching_pattern.match(url)[1]
end
site() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 18
def site
  @site ||= self.class.to_s.split("::").last.downcase.to_sym
end
title() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 56
def title
  lookup :title
end

Private Instance Methods

ensure_match() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 104
def ensure_match
  return unless matching_pattern.nil?
  raise NotSupportedURLError, "#{self.class} can not handle #{url.inspect}"
end
friendly_options(options) click to toggle source

a subclass can override this one to define some aliases to paper over some quirks in a specific oembed provider’s API, e.g. mapping “width” to “maxwidth”

# File lib/film_snob/oembed_provider.rb, line 112
def friendly_options(options)
  options
end
get() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 94
def get
  Net::HTTP::Get.new uri.request_uri
end
lookup(attribute) click to toggle source
# File lib/film_snob/oembed_provider.rb, line 70
def lookup(attribute)
  oembed[attribute.to_s] || not_embeddable!
end
matching_pattern() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 74
def matching_pattern
  @matching_pattern ||= self.class.valid_url_patterns.find do |pattern|
    pattern.match(url)
  end
end
not_embeddable!() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 66
def not_embeddable!
  raise NotEmbeddableError, "#{clean_url} is not embeddable"
end
oembed() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 80
def oembed
  @oembed ||= begin
                if response.code.start_with?("2")
                  JSON.parse(response.body)
                else
                  {}
                end
              end
end
response() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 90
def response
  @response ||= self.class.http.request get
end
uri() click to toggle source
# File lib/film_snob/oembed_provider.rb, line 98
def uri
  URI(self.class.oembed_endpoint).tap do |uri|
    uri.query = URI.encode_www_form({ :url => clean_url }.merge(options))
  end
end