lib/oso/server.rb in oso-1.0.0 vs lib/oso/server.rb in oso-1.0.1
- old
+ new
@@ -4,12 +4,14 @@
require "uri"
url = ENV.values_at("OSO_REDIS_URL", "REDISTOGO_URL").compact.first
url = URI.parse url || "redis://localhost:6379"
-$redis = Redis::Namespace.new :oso,
- redis: Redis.new(host: url.host, password: url.password, port: url.port)
+$redis = Redis::Namespace.new(:oso,
+ :redis => Redis.new(:host => url.host,
+ :password => url.password,
+ :port => url.port))
helpers do
def bad! message
halt 412, {}, message
end
@@ -34,10 +36,15 @@
$redis.expire longkey, life
$redis.expire shortkey, life
end
end
+ def shorten str
+ return str unless str.length > 40
+ str[0, 37] + "..."
+ end
+
def shorturl short
request.path_info = "/#{short}"
request.url
end
end
@@ -52,11 +59,12 @@
@misses = $redis.get(:misses).to_i
@byhits = Hash[*$redis.zrevrange("by:hits", 0, 10, :with_scores => true)]
@bytimes = Hash[*$redis.zrevrange("by:time", 0, 10, :with_scores => true)]
[@byhits, @bytimes].each do |h|
- h.each { |k, v| h[k] = { long: $redis.get("short:#{k}"), score: v } }
+ h.select! { |k, v| $redis.exists "short:#{k}" }
+ h.each { |k, v| h[k] = { :long => $redis.get("short:#{k}"), :score => v } }
end
@title = "Stats"
erb :stats
end
@@ -102,6 +110,18 @@
$redis.incr :hits
$redis.zincrby "by:hits", 1, short
$redis.zadd "by:time", Time.now.utc.to_i, short
redirect long
+end
+
+get "/:short/stats" do |short|
+ nope! unless @long = $redis.get("short:#{short}")
+
+ @short = short
+ @hits = $redis.zscore("by:hits", short).to_i
+ @limit = $redis.get("short:#{short}:limit").to_i
+ @time = $redis.zscore("by:time", short).to_i
+
+ @title = "Stats :: #@short"
+ erb :stat
end