Class: Shortey::App

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

Constant Summary collapse

@@redis =
Redis.new(:host => 'localhost')

Class Method Summary collapse

Class Method Details

.call(env) ⇒ Object



5
6
7
8
9
10
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
# File 'lib/shortey/app.rb', line 5

def self.call(env)
  request = Rack::Request.new(env)
  response = Rack::Response.new
  response.header['Content-Type'] = "text/plain"
  case request.path
  when "/"
    url = request.params['url']
    uri = URI.parse(url)
    unless uri.kind_of? URI::HTTP or uri.kind_of? URI::HTTPS
      response.status = 500
      response.write "URL is invalid!"
    else
      hash = Zlib.crc32(url)
      unless @@redis.exists(hash)
        @@redis.set(hash, uri.to_s)
        @@redis.hset(:clicks, hash, 0)
      end
      response.status = 200
      response.write "#{request.scheme}://#{request.host_with_port}/#{hash}"
    end
  else
    hash = request.path.delete('/')
    url = @@redis.get(hash)
    unless url.nil? 
      @@redis.hincrby(:clicks, hash, 1)
      response.redirect(url, 301)
    else
      response.status = 404
      response.write "URL with hash #{hash} not found!"
    end
  end 
  response.finish
end