Class: Expurrel

Inherits:
Object
  • Object
show all
Defined in:
lib/expurrel.rb

Overview

Expurrel is a url that can be a shortened one Expurrel can decode it then.

Constant Summary collapse

PROVIDERS =
[]
PROVIDERS_LIST_FILE =
File.join(File.dirname(__FILE__), '..', 'urlshorteners.txt')
@@cache =
SimpleCache.new(:max_size => 1000)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Expurrel

Give expurrel a new url.



13
14
15
# File 'lib/expurrel.rb', line 13

def initialize(url)
  @url = url
end

Class Method Details

.load_providers!Object

Loads the list with providers into the PROVIDERS constant.



69
70
71
72
73
74
75
# File 'lib/expurrel.rb', line 69

def load_providers!
  return if !PROVIDERS.empty?
  File.open(PROVIDERS_LIST_FILE, "r") .each_line do |line|
    l = line.strip
    PROVIDERS << l unless l.empty?
  end
end

.reverse_shorturl(tinyurl) ⇒ Object

Uses the header file sent back to decode the shortened url. Trick provided by: garrickvanburen.com/archive/how-to-decode-tinyurls-with-ruby



82
83
84
# File 'lib/expurrel.rb', line 82

def reverse_shorturl(tinyurl)
  Net::HTTP.get_response(URI.parse(tinyurl)).to_hash['location'].to_s
end

Instance Method Details

#decodeObject

This method checks first if it’s a tiny url domain from the list.

  1. Is it a short url?

  2. It is cached? (tinyurls won’t change we can cache forever)

  3. Decode (slow!), does http request.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/expurrel.rb', line 22

def decode
  return begin
    if !self.is_short_url?
      @url
    elsif @@cache.has_key?(@url)
      @@cache.value(@url)
    else
      # TODO add it to the cache
      decoded_url = self.class.reverse_shorturl(@url)
      @@cache.cache!(@url, decoded_url)
      decoded_url
    end
  end
end

#domainObject

Get the domain



57
58
59
60
61
62
# File 'lib/expurrel.rb', line 57

def domain
  return @domain if !@domain.nil? 
  unpacked_link = @url.scan(/(https?:\/\/|www\.)([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?/)
  @domain = unpacked_link.first[1]
  return @domain
end

#is_short_domain?(domain) ⇒ Boolean

Is it a short domain?

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
# File 'lib/expurrel.rb', line 44

def is_short_domain?(domain)
  PROVIDERS.each do |l|
    # TODO regular expression matching
    if domain.include?(l)
      return true
    end
  end
  false
end

#is_short_url?Boolean

Is this a short url

Returns:

  • (Boolean)


38
39
40
# File 'lib/expurrel.rb', line 38

def is_short_url?
  is_short_domain?(self.domain)
end