Sha256: 2fba68c845518a5a8faa27ee466cac9660e6e44bf40f22b4fc97c645f6caff02

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 KB

Contents

require "net/http"
require "json"

class FilmSnob
  class VideoSite
    attr_reader :url, :options
    def initialize(url, options = {})
      @url = url
      @options = options
    end

    def id
      @id ||= matching_pattern.match(url)[1]
    end

    def site
      @site ||= self.class.to_s.split("::").last.downcase.to_sym
    end

    SUBCLASSES = []
    def self.inherited(base)
      SUBCLASSES << base
    end

    def self.subclasses
      SUBCLASSES
    end

    def self.valid_url_patterns
      []
    end

    def self.oembed_endpoint
      ""
    end

    def self.http
      Net::HTTP.new(uri.host, uri.port).tap do |uri|
        uri.use_ssl = use_ssl?
      end
    end

    def self.use_ssl?
      "https" == uri.scheme
    end

    def title
      lookup :title
    end

    def html
      lookup :html
    end

    private

    def not_embeddable!
      raise NotEmbeddableError, "#{clean_url} is not embeddable"
    end

    def self.uri
      URI.parse(oembed_endpoint)
    end

    def lookup(attribute)
      oembed[attribute.to_s] || not_embeddable!
    end

    def matching_pattern
      self.class.valid_url_patterns.find do |pattern|
        pattern.match(url)
      end
    end

    def oembed
      @oembed ||= JSON.parse response.body
    rescue
      @oembed = {}
    end

    def response
      self.class.http.request get
    end

    def get
      Net::HTTP::Get.new uri.request_uri
    end

    def uri
      URI(self.class.oembed_endpoint).tap do |uri|
        uri.query = URI.encode_www_form({ url: clean_url }.merge(options))
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
film_snob-0.6.2 lib/film_snob/video_site.rb
film_snob-0.6.1 lib/film_snob/video_site.rb
film_snob-0.6.0 lib/film_snob/video_site.rb
film_snob-0.5.0 lib/film_snob/video_site.rb