Class: Shortener::Server::Brief

Inherits:
Object
  • Object
show all
Defined in:
lib/shortener/server/brief.rb

Class Method Summary collapse

Class Method Details

.allObject



8
9
10
11
12
13
14
15
# File 'lib/shortener/server/brief.rb', line 8

def all
  $redis.keys('data:*').map do |key|
    short = $redis.hgetall(key)
    puts "  url for #{key[-5..-1]} => #{short['url']}"
    short['expire'] = $redis.ttl(short['expire']) if short.has_key?('expire')
    short
  end
end

.delete(id) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/shortener/server/brief.rb', line 72

def delete(id)
  sha = $redis.get(id)
  unless sha.nil?
    $redis.multi do
      $redis.del "data:#{sha}:#{id}"
      $redis.del "expire:#{sha}:#{id}"
      $redis.del id
    end
    true
  else
    false
  end
end

.find(id, params) ⇒ Object



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/shortener/server/brief.rb', line 17

def find(id, params)
  sha = $redis.get(id)
  if sha.nil?   # => Short Not Found
    if (params[:captures].last == '.json')
      nope! "Short not found: #{id}"
    else
      puts "redirecting #{params[:captures].inspect} to default url"
      return $conf.default_url, :url
    end
  else         # => Short Found
    key = "data:#{sha}:#{id}"
    short = $redis.hgetall(key)
    not_expired = short.has_key?('expire') ? $redis.get(short['expire']) : true
    not_maxed = !(short['click_count'].to_i >= short['max_clicks'].to_i)
    short.has_key?('max_clicks') ? not_maxed : not_maxed = true
    if params[:captures].last == '.json'                                # => We just want JSON
      ret = short.merge({expired: not_expired.nil? , maxed: !not_maxed})
      return ret.to_json, :json
    else                                                                # => Redirect Me!
      $redis.hincrby(key, 'click_count', 1) if not_expired && not_maxed
      if not_expired
        unless short['s3'] == 'true' && !(short['type'] == 'download')
          if not_maxed
            puts "redirecting found short #{id} to #{short['url']}"
            return short['url'], :url
          end # => max clicks check
        else                                                            # => This is S3 content
          puts "rendering view for s3 content. #{id} => #{short['url']}"
          return short, :s3
        end # => it's S3 and needs displaying.
      end # => expired check
    end # => format
  end
  # =>    short was maxed or expired & not a JSON request
  return $conf.default_url, :url
end

.shorten(params) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/shortener/server/brief.rb', line 54

def shorten(params)
  bad! 'Missing url.' unless url = params['url']
  bad! 'Bad URL' unless params['url'] =~ /(^http|^www)/
  url = "http://#{url}" unless /^http/i =~ url
  bad! 'Bad URL' unless (url = URI.parse(url)) && /^http/ =~ url.scheme

  %w(max_clicks expire desired_short allow_override).each do |k|
    params[k] = false if params[k].nil? || params[k].empty?
  end

  unless params['max_clicks'] || params['expire'] || params['desired_short']
    data = check_cache(url)
  end
  data ||= get_short_key(url, params)

  data
end

.upload(params) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/shortener/server/brief.rb', line 86

def upload(params)
  bad! 'Missing content type.' unless type = params['type']
  fname = params['file_name'].gsub(' ', '+')
  url = "https://s3.amazonaws.com/#{$conf.s3_bucket}/#{$conf.s3_key_prefix}/#{fname}"
  data = {'s3' => true, 'extension' => File.extname(fname)[1..-1],
    'description' => params.delete('description'),
    'name' => params.delete('name'), 'type' => params.delete('type')}
  get_short_key(url, params, data)
end