Sha256: 438eca049328606420b32257aab22181613c2f7f2a90ab11fbc1997b4985911d

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 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

    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

5 entries across 5 versions & 1 rubygems

Version Path
film_snob-0.3.13 lib/film_snob/video_site.rb
film_snob-0.3.12 lib/film_snob/video_site.rb
film_snob-0.3.11 lib/film_snob/video_site.rb
film_snob-0.3.10 lib/film_snob/video_site.rb
film_snob-0.3.9 lib/film_snob/video_site.rb