Module: Bitly
Overview
A basic, caching, Bitlifier
Constant Summary collapse
- TIME_TO_LIVE =
1 year
365 * 24 * 3600
- TIME_TO_LIVE_FAILURE =
1 hour
3600
Instance Attribute Summary collapse
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#config ⇒ Object
Returns the value of attribute config.
Class Method Summary collapse
- .shorten(url) ⇒ Object
-
.url_base ⇒ Object
self.config = { “user” => “radiospiel”, “key” => “R_b01d6497ec9a1b2fff03fe942f7c3e1c” }.
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
12 13 14 |
# File 'lib/simple_bitlify.rb', line 12 def cache @cache end |
#config ⇒ Object
Returns the value of attribute config.
13 14 15 |
# File 'lib/simple_bitlify.rb', line 13 def config @config end |
Class Method Details
.shorten(url) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/simple_bitlify.rb', line 40 def self.shorten(url) return url if !url || url.index("/bit.ly/") cache.fetch(url) || begin bitly_url = "#{self.url_base}&longUrl=#{CGI.escape(url)}" parsed = JSON.parse get(bitly_url) shortened = parsed["data"]["url"] if parsed["status_code"] == 200 cache.store(url, shortened || url, shortened ? TIME_TO_LIVE : TIME_TO_LIVE_FAILURE) end end |
.url_base ⇒ Object
self.config =
"user" => "radiospiel",
"key" => "R_b01d6497ec9a1b2fff03fe942f7c3e1c"
31 32 33 34 35 36 37 38 |
# File 'lib/simple_bitlify.rb', line 31 def self.url_base @url_base ||= begin user, key = (self.config || {}).values_at "user", "key" raise "Missing or invalid bitly configuration: #{config.inspect}" unless user && key "https://api-ssl.bitly.com/v3/shorten?login=#{user}&apiKey=#{key}" end end |