class VideoInfo::Provider
Attributes
data[W]
options[RW]
url[RW]
video_id[RW]
Public Class Methods
new(url, options = {})
click to toggle source
# File lib/video_info/provider.rb, line 12 def initialize(url, options = {}) @options = _clean_options(options) @url = url _set_video_id_from_url end
usable?(_url)
click to toggle source
# File lib/video_info/provider.rb, line 18 def self.usable?(_url) raise NotImplementedError.new( "Provider class must implement .usable? public method" ) end
Public Instance Methods
available?()
click to toggle source
# File lib/video_info/provider.rb, line 38 def available? !%w[403 404 400 451 401].include?(_response_code) end
data()
click to toggle source
# File lib/video_info/provider.rb, line 34 def data @data ||= _set_data_from_api end
embed_code(options = {})
click to toggle source
# File lib/video_info/provider.rb, line 24 def embed_code(options = {}) iframe_attributes = options.fetch(:iframe_attributes, {}) iframe_attrs = ["src=\"#{_embed_url(options)}\"", 'frameborder="0"'] iframe_attrs << _hash_to_attributes( _default_iframe_attributes.merge(iframe_attributes) ) "<iframe #{iframe_attrs.reject(&:empty?).join(" ")}></iframe>" end
stats()
click to toggle source
# File lib/video_info/provider.rb, line 56 def stats {} end
thumbnail()
click to toggle source
# File lib/video_info/provider.rb, line 42 def thumbnail if defined?(thumbnail_large) thumbnail_large end end
thumbnail_large_2x()
click to toggle source
# File lib/video_info/provider.rb, line 48 def thumbnail_large_2x nil end
thumbnail_maxres()
click to toggle source
# File lib/video_info/provider.rb, line 52 def thumbnail_maxres nil end
Private Instance Methods
_api_url()
click to toggle source
# File lib/video_info/provider.rb, line 148 def _api_url raise NotImplementedError.new( "Provider class must implement #_api_url private method" ) end
_clean_options(options)
click to toggle source
# File lib/video_info/provider.rb, line 85 def _clean_options(options) options = {"User-Agent" => "VideoInfo/#{VideoInfo::VERSION}"}.merge(options) options.dup.each do |key, value| if _not_openuri_option_symbol?(key) options[_http_header_field(key)] = value options.delete key end end options end
_embed_url(options)
click to toggle source
# File lib/video_info/provider.rb, line 154 def _embed_url(options) url_attrs = options.fetch(:url_attributes, {}) url_attrs = _default_url_attributes.merge(url_attrs) url = embed_url url += "?#{_hash_to_params(url_attrs)}" unless url_attrs.empty? url end
_hash_to_attributes(hash)
click to toggle source
# File lib/video_info/provider.rb, line 163 def _hash_to_attributes(hash) return unless hash.is_a?(Hash) hash.map { |k, v| "#{k}=\"#{v}\"" }.join(" ") end
_hash_to_params(hash)
click to toggle source
# File lib/video_info/provider.rb, line 168 def _hash_to_params(hash) URI.encode_www_form(hash) end
_http_header_field(key)
click to toggle source
# File lib/video_info/provider.rb, line 127 def _http_header_field(key) key.to_s.split(/[^a-z]/i).map(&:capitalize).join("-") end
_http_response_code(http)
click to toggle source
# File lib/video_info/provider.rb, line 73 def _http_response_code(http) response = http.head(_api_path, @options) response.code end
_https_response_code(http)
click to toggle source
# File lib/video_info/provider.rb, line 78 def _https_response_code(http) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE response = http.head(_api_path, @options) response.code end
_not_openuri_option_symbol?(key)
click to toggle source
# File lib/video_info/provider.rb, line 123 def _not_openuri_option_symbol?(key) key.is_a?(Symbol) && !OpenURI::Options.key?(key) end
_response_code()
click to toggle source
# File lib/video_info/provider.rb, line 62 def _response_code uri = URI.parse(_api_url) http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == "https" _https_response_code(http) else _http_response_code(http) end end
_set_data_from_api(api_url = _api_url)
click to toggle source
# File lib/video_info/provider.rb, line 97 def _set_data_from_api(api_url = _api_url) _set_data_from_api_impl(api_url) rescue OpenURI::HTTPError, *NetHttpTimeoutErrors.all => e if e.instance_of?(OpenURI::HTTPError) && e.respond_to?(:io) && e.io.respond_to?(:status) response_code = e.io.status[0] if response_code == "400" log_warn("Your API key is probably invalid. Please verify it.") end end msg = "Unexpected network error while fetching information about the video" msg << " (response code: #{response_code})" if defined?(response_code) raise VideoInfo::HttpError.new(msg) end
_set_data_from_api_impl(api_url)
click to toggle source
# File lib/video_info/provider.rb, line 118 def _set_data_from_api_impl(api_url) uri = URI.parse(api_url) JSON.parse(uri.read(options)) end
_set_video_id_from_url()
click to toggle source
# File lib/video_info/provider.rb, line 131 def _set_video_id_from_url @url.gsub(_url_regex) { @video_id = $1 || $2 || $3 } unless _valid_video_id? raise UrlError, "Url is not valid, video_id is not found: #{url}" end end
_url_regex()
click to toggle source
# File lib/video_info/provider.rb, line 142 def _url_regex raise NotImplementedError.new( "Provider class must implement #_url_regex private method" ) end
_valid_video_id?()
click to toggle source
# File lib/video_info/provider.rb, line 138 def _valid_video_id? video_id && video_id != url && !video_id.empty? end
log_warn(message)
click to toggle source
# File lib/video_info/provider.rb, line 114 def log_warn(message) VideoInfo.logger.warn(message) end