class OhMyEmbed::Crawler
Attributes
Public Class Methods
Initialize the OhMyEmbed::Crawler
@param *providers [Symbol|OhMyEmbed::Provider|Hash] the providers to register or options as the last argument
# File lib/oh_my_embed/crawler.rb, line 8 def initialize(*providers) @providers = Set.new options = providers.extract_options! register_all_built_in_providers if options[:all] providers.each do |provider| register provider end end
Public Instance Methods
Select all provider constants from the OhMyEmbed::Providers
module
@return [Array<Symbol>]
# File lib/oh_my_embed/crawler.rb, line 72 def built_in_providers OhMyEmbed::Providers.constants .select { |c| OhMyEmbed::Providers.const_get(c) < OhMyEmbed::Provider } end
Check if any provider matches the given content_url
@param [String] content_url @return [true|false]
# File lib/oh_my_embed/crawler.rb, line 65 def embeddable?(content_url) @providers.any?{ |provider| provider.regex =~ content_url } end
Fetch the embed response for the given content_url
@param [String] content_url @return [OhMyEmbed::Response]
# File lib/oh_my_embed/crawler.rb, line 47 def fetch(content_url) provider = self.provider_for(content_url) provider.fetch(content_url) end
Get the provider for given content_url
@param [String] content_url @raise [OhMyEmbed::UnknownProvider] if no registered provider matches the given context_url @return [OhMyEmbed::Provider]
# File lib/oh_my_embed/crawler.rb, line 57 def provider_for(content_url) @providers.find{ |provider| provider.regex =~ content_url } || raise(OhMyEmbed::ProviderNotFound.new(content_url)) end
Register a provider
@param provider [Symbol|OhMyEmbed::Provider] @raise [OhMyEmbed::UnknownProvider] if you try to register an unknown provider
# File lib/oh_my_embed/crawler.rb, line 23 def register(provider) if provider.is_a? Symbol begin provider = OhMyEmbed::Providers.const_get(provider.to_s.camelize) rescue NameError raise OhMyEmbed::UnknownProvider.new(provider) end end @providers.add provider end
Register all built-in providers for this crawler
# File lib/oh_my_embed/crawler.rb, line 37 def register_all_built_in_providers built_in_providers.each do |provider| register provider end end