Class: Guillotine::RiakAdapter
- Defined in:
- lib/guillotine/adapters/riak_adapter.rb
Overview
Stores shortened URLs in Riak. Totally scales.
Constant Summary collapse
- PLAIN =
'text/plain'.freeze
Instance Attribute Summary collapse
-
#code_bucket ⇒ Object
readonly
Returns the value of attribute code_bucket.
-
#url_bucket ⇒ Object
readonly
Returns the value of attribute url_bucket.
Instance Method Summary collapse
-
#add(url, code = nil, options = nil) ⇒ Object
Public: Stores the shortened version of a URL.
-
#clear(url) ⇒ Object
Public: Removes the assigned short code for a URL.
-
#clear_code(code) ⇒ Object
Public: Removes the assigned short code.
-
#code_for(url) ⇒ Object
Public: Retrieves the code for a given URL.
-
#code_object(url) ⇒ Object
Retrieves the code riak value for a given URL.
-
#find(code) ⇒ Object
Public: Retrieves a URL from the code.
-
#fix_url_object(obj, data = nil) ⇒ Object
Fixes a bug in Guillotine 1.0.2 where the content type on url objects was not being set.
-
#initialize(code_bucket, url_bucket = nil) ⇒ RiakAdapter
constructor
Initializes the adapter.
- #url_key(url) ⇒ Object
-
#url_object(code) ⇒ Object
Retrieves a URL riak value from the code.
Methods inherited from Adapter
#get_code, #parse_url, #shorten, #shorten_fixed_charset
Constructor Details
#initialize(code_bucket, url_bucket = nil) ⇒ RiakAdapter
Initializes the adapter.
code_bucket - The Riak::Bucket for all code keys. url_bucket - The Riak::Bucket for all url keys. If this is not
given, the code bucket is used for all keys.
14 15 16 17 |
# File 'lib/guillotine/adapters/riak_adapter.rb', line 14 def initialize(code_bucket, url_bucket = nil) @code_bucket = code_bucket @url_bucket = url_bucket || @code_bucket end |
Instance Attribute Details
#code_bucket ⇒ Object (readonly)
Returns the value of attribute code_bucket.
7 8 9 |
# File 'lib/guillotine/adapters/riak_adapter.rb', line 7 def code_bucket @code_bucket end |
#url_bucket ⇒ Object (readonly)
Returns the value of attribute url_bucket.
7 8 9 |
# File 'lib/guillotine/adapters/riak_adapter.rb', line 7 def url_bucket @url_bucket end |
Instance Method Details
#add(url, code = nil, options = nil) ⇒ Object
Public: Stores the shortened version of a URL.
url - The String URL to shorten and store. code - Optional String code for the URL. options - Optional Guillotine::Service::Options
Returns the unique String code for the URL. If the URL is added multiple times, this should return the same code.
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 |
# File 'lib/guillotine/adapters/riak_adapter.rb', line 27 def add(url, code = nil, = nil) sha = url_key url url_obj = @url_bucket.get_or_new sha, :r => 1 if url_obj.raw_data fix_url_object(url_obj) code = url_obj.data end code = get_code(url, code, ) code_obj = @code_bucket.get_or_new code code_obj.content_type = url_obj.content_type = PLAIN if existing_url = code_obj.data # key exists raise DuplicateCodeError.new(existing_url, url, code) if url_key(existing_url) != sha end if !url_obj.data # unsaved url_obj.data = code url_obj.store end code_obj.data = url code_obj.store code end |
#clear(url) ⇒ Object
Public: Removes the assigned short code for a URL.
url - The String URL to remove.
Returns nothing.
80 81 82 83 84 85 |
# File 'lib/guillotine/adapters/riak_adapter.rb', line 80 def clear(url) if code_obj = code_object(url) @url_bucket.delete code_obj.key @code_bucket.delete code_obj.data end end |
#clear_code(code) ⇒ Object
Public: Removes the assigned short code.
code - The String code to remove.
Returns nothing.
92 93 94 95 96 97 |
# File 'lib/guillotine/adapters/riak_adapter.rb', line 92 def clear_code(code) if url_obj = url_object(code) @url_bucket.delete url_key(url_obj.data) @code_bucket.delete url_obj.key end end |
#code_for(url) ⇒ Object
Public: Retrieves the code for a given URL.
url - The String URL to lookup.
Returns the String code, or nil if none is found.
69 70 71 72 73 |
# File 'lib/guillotine/adapters/riak_adapter.rb', line 69 def code_for(url) if obj = code_object(url) obj.data end end |
#code_object(url) ⇒ Object
Retrieves the code riak value for a given URL.
url - The String URL to lookup.
Returns a Riak::RObject, or nil if none is found.
115 116 117 118 119 120 121 122 |
# File 'lib/guillotine/adapters/riak_adapter.rb', line 115 def code_object(url) sha = url_key url if o = @url_bucket.get(sha, :r => 1) fix_url_object(o) end rescue Riak::FailedRequest => err raise unless err.not_found? end |
#find(code) ⇒ Object
Public: Retrieves a URL from the code.
code - The String code to lookup the URL.
Returns the String URL.
58 59 60 61 62 |
# File 'lib/guillotine/adapters/riak_adapter.rb', line 58 def find(code) if obj = url_object(code) obj.data end end |
#fix_url_object(obj, data = nil) ⇒ Object
Fixes a bug in Guillotine 1.0.2 where the content type on url objects was not being set. The ruby Riak::Client defaults to JSON, so strings were being saved as “somecode”, which is unparseable by JSON.
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/guillotine/adapters/riak_adapter.rb', line 127 def fix_url_object(obj, data = nil) if data obj.content_type = PLAIN obj.data = data obj.store return obj end case obj.content_type when /json/ then fix_url_object(obj, JSON.parse(%({"data":#{obj.raw_data}}))['data']) when PLAIN then obj else fix_url_object(obj, obj.data) # old values had the right data but the content type was application/x-www-form-urlencoded end end |
#url_key(url) ⇒ Object
141 142 143 |
# File 'lib/guillotine/adapters/riak_adapter.rb', line 141 def url_key(url) Digest::SHA1.hexdigest url.downcase end |
#url_object(code) ⇒ Object
Retrieves a URL riak value from the code.
code - The String code to lookup the URL.
Returns a Riak::RObject, or nil if none is found.
104 105 106 107 108 |
# File 'lib/guillotine/adapters/riak_adapter.rb', line 104 def url_object(code) @code_bucket.get(code, :r => 1) rescue Riak::FailedRequest => err raise unless err.not_found? end |