Class: Expurrel
- Inherits:
-
Object
- Object
- Expurrel
- 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
-
.load_providers! ⇒ Object
Loads the list with providers into the PROVIDERS constant.
-
.reverse_shorturl(tinyurl) ⇒ Object
Uses the header file sent back to decode the shortened url.
Instance Method Summary collapse
-
#decode ⇒ Object
This method checks first if it’s a tiny url domain from the list.
-
#domain ⇒ Object
Get the domain.
-
#initialize(url) ⇒ Expurrel
constructor
Give expurrel a new url.
-
#is_short_domain?(domain) ⇒ Boolean
Is it a short domain?.
-
#is_short_url? ⇒ Boolean
Is this a short url.
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
#decode ⇒ Object
This method checks first if it’s a tiny url domain from the list.
-
Is it a short url?
-
It is cached? (tinyurls won’t change we can cache forever)
-
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 |
#domain ⇒ Object
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?
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
38 39 40 |
# File 'lib/expurrel.rb', line 38 def is_short_url? is_short_domain?(self.domain) end |