Class: UrlExpander::Client
- Inherits:
-
Object
- Object
- UrlExpander::Client
- Defined in:
- lib/url_expander.rb
Class Method Summary collapse
-
.expand(url = "", options = {}) ⇒ Object
Expand a given url.
Class Method Details
.expand(url = "", options = {}) ⇒ Object
Expand a given url.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/url_expander.rb', line 11 def self.(url="", = {}) # Setup the default options [:nested_shortening] = true unless .has_key?(:nested_shortening) if [:nested_shortening] [:limit] = 10 unless .has_key?(:limit) end [:config_file] = "#{ENV['HOME']}/url_expander_credentials.yml" unless [:config_file] # We Reached the maximum number of redirections, quit. raise ArgumentError, 'HTTP redirect too deep' if [:nested_shortening] && [:limit] == 0 # Make sure we have a url raise ArgumentError.new('Expander requires a short url') if url.nil? || url.empty? exclude_klasses = ['UrlExpander::Expanders::Basic', 'UrlExpander::Expanders::API', 'UrlExpander::Expanders::Scrape'] # Get the names for all the expanders except the basic default one. mod = UrlExpander::Expanders = mod.constants.collect{|c| mod.const_get(c)}.select{|c| c.class == Class && !exclude_klasses.include?(c.to_s)} = nil # Find the correct expander .each do |exp| if exp.respond_to?("is_me?") = exp if exp.is_me?(url) elsif(exp::PATTERN.match(url)) = exp end end @expander = (!.nil?) ? .new(url,) : nil if @expander.nil? && ![:is_redirection] raise ArgumentError.new('Unknow url') end if [:nested_shortening] & !@expander.nil? [:limit] -= 1 [:is_redirection] = true UrlExpander::Client.(@expander.long_url, ) else (@expander.nil?) ? url : @expander.long_url end end |