Module: CachedBitly
- Extended by:
- CachedBitly
- Included in:
- CachedBitly
- Defined in:
- lib/cached_bitly.rb,
lib/cached_bitly/version.rb
Constant Summary collapse
- VERSION =
'0.0.2'
Instance Method Summary collapse
- #allowed_hostnames ⇒ Object
- #allowed_hostnames=(hostnames = []) ⇒ Object
- #bitly_client ⇒ Object
- #bitly_client=(client) ⇒ Object
- #clean(html) ⇒ Object
- #clean_doc(doc) ⇒ Object
-
#fetch(url, default = url) ⇒ Object
Handles retreiving cached short urls and generating new ones if we don’t have the short url for a particular url.
- #redis ⇒ Object
- #redis=(redis) ⇒ Object
- #redis_namespace ⇒ Object
- #redis_namespace=(namespace) ⇒ Object
-
#save(long_url, short_url) ⇒ Object
Save the url along with it’s associated short url for easy retrieval.
-
#shorten(url) ⇒ Object
Handles generating the short url and storing it.
-
#shortened(url) ⇒ Object
Look to see if the url has already been shortened.
- #stats_enabled ⇒ Object
- #stats_enabled=(enabled) ⇒ Object
- #stats_enabled? ⇒ Boolean
- #totals ⇒ Object
- #url_scheme ⇒ Object
- #url_scheme=(scheme) ⇒ Object
Instance Method Details
#allowed_hostnames ⇒ Object
25 26 27 |
# File 'lib/cached_bitly.rb', line 25 def allowed_hostnames @allowed_hostnames ||= [] end |
#allowed_hostnames=(hostnames = []) ⇒ Object
29 30 31 |
# File 'lib/cached_bitly.rb', line 29 def allowed_hostnames=(hostnames=[]) @allowed_hostnames = hostnames end |
#bitly_client ⇒ Object
33 34 35 36 37 38 |
# File 'lib/cached_bitly.rb', line 33 def bitly_client @bitly_client ||= begin Bitly.use_api_version_3 Bitly.new(ENV['BITLY_LOGIN'], ENV['BITLY_API_KEY']) end end |
#bitly_client=(client) ⇒ Object
40 41 42 |
# File 'lib/cached_bitly.rb', line 40 def bitly_client=(client) @bitly_client = client end |
#clean(html) ⇒ Object
65 66 67 |
# File 'lib/cached_bitly.rb', line 65 def clean(html) clean_doc(html).css('body').inner_html end |
#clean_doc(doc) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/cached_bitly.rb', line 69 def clean_doc(doc) doc = doc.is_a?(String) ? Nokogiri::HTML(doc) : doc doc.css('a[href^=http]').each do |link| url = link.attributes['href'].value if !allowed_hostnames.empty? && url.match(Regexp.union(*allowed_hostnames)) next end link.attributes['href'].value = fetch(url) end doc end |
#fetch(url, default = url) ⇒ Object
Handles retreiving cached short urls and generating new ones if we don’t have the short url for a particular url.
Returns short url, default if save goes wrong.
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cached_bitly.rb', line 85 def fetch(url, default=url) short_url = shortened(url) if short_url hit! short_url.gsub(/^http\:/, url_scheme + ':') else miss! shorten(url) || default end end |
#redis ⇒ Object
9 10 11 |
# File 'lib/cached_bitly.rb', line 9 def redis @redis ||= Redis.new end |
#redis=(redis) ⇒ Object
13 14 15 |
# File 'lib/cached_bitly.rb', line 13 def redis=(redis) @redis = redis end |
#redis_namespace ⇒ Object
17 18 19 |
# File 'lib/cached_bitly.rb', line 17 def redis_namespace @redis_namespace ||= 'bitly' end |
#redis_namespace=(namespace) ⇒ Object
21 22 23 |
# File 'lib/cached_bitly.rb', line 21 def redis_namespace=(namespace) @redis_namespace = namespace end |
#save(long_url, short_url) ⇒ Object
Save the url along with it’s associated short url for easy retrieval.
Returns true if saved, false if not.
120 121 122 |
# File 'lib/cached_bitly.rb', line 120 def save(long_url, short_url) !!redis.hset("#{redis_namespace}:url", digest(long_url), short_url) end |
#shorten(url) ⇒ Object
Handles generating the short url and storing it.
Returns short url if stored, false if not.
99 100 101 102 103 104 105 106 |
# File 'lib/cached_bitly.rb', line 99 def shorten(url) url = bitly_client.shorten(url) if save(url.long_url, url.short_url) url.short_url else false end end |
#shortened(url) ⇒ Object
Look to see if the url has already been shortened. If the url has been shortened, return the url.
Returns short url if it has been shortened, nil if not
112 113 114 |
# File 'lib/cached_bitly.rb', line 112 def shortened(url) redis.hget("#{redis_namespace}:url", digest(url)) end |
#stats_enabled ⇒ Object
48 49 50 |
# File 'lib/cached_bitly.rb', line 48 def stats_enabled @stats_enabled ||= false end |
#stats_enabled=(enabled) ⇒ Object
52 53 54 |
# File 'lib/cached_bitly.rb', line 52 def stats_enabled=(enabled) @stats_enabled = enabled end |
#stats_enabled? ⇒ Boolean
44 45 46 |
# File 'lib/cached_bitly.rb', line 44 def stats_enabled? !!@stats_enabled end |
#totals ⇒ Object
124 125 126 127 128 |
# File 'lib/cached_bitly.rb', line 124 def totals { :hit => redis.get("#{redis_namespace}:url:hit").to_i, :miss => redis.get("#{redis_namespace}:url:miss").to_i, :total => redis.hlen("#{redis_namespace}:url") } end |
#url_scheme ⇒ Object
56 57 58 |
# File 'lib/cached_bitly.rb', line 56 def url_scheme @url_scheme ||= 'http' end |
#url_scheme=(scheme) ⇒ Object
60 61 62 63 |
# File 'lib/cached_bitly.rb', line 60 def url_scheme=(scheme) raise ArgumentError unless ['http', 'https'].include?(scheme) @url_scheme = scheme end |