Class: TinyurlShortener

Inherits:
Object
  • Object
show all
Defined in:
lib/tinyurl_shortener.rb,
lib/tinyurl_shortener/version.rb

Constant Summary collapse

VERSION =
"1.0.3"

Class Method Summary collapse

Class Method Details

.expand(short_url) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tinyurl_shortener.rb', line 11

def self.expand(short_url)

  # there are some ways that tinyurl redirects
  #   * using 301 location redirection
  #   * using content refresh redirection
  #
  #   when we cannot detect the redirecting way, use current url for results
  RestClient.get(short_url) do |response, request, result, &block|
    if [301, 302, 307].include? response.code
      return response.headers[:location]
    elsif ! (match = response.body.scan(/content="1; url=(.*?)"/)).empty?
      return match.last.first
    else
      response.request.url
    end
  end
end

.shorten(long_url) ⇒ Object



7
8
9
# File 'lib/tinyurl_shortener.rb', line 7

def self.shorten(long_url)
  RestClient.get 'http://tinyurl.com/api-create.php', {params: {url: long_url}}
end