Class: Guillotine::CassandraAdapter
- Defined in:
- lib/guillotine/adapters/cassandra_adapter.rb
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(url) ⇒ Object
Public: Removes the assigned short code.
-
#code_for(url) ⇒ Object
Public: Retrieves the code for a given URL.
-
#find(code) ⇒ Object
Public: Retrieves a URL from the code.
-
#initialize(cassandra, read_only = false) ⇒ CassandraAdapter
constructor
Public: Initialise the adapter with a Redis instance.
- #purge(code, url) ⇒ Object
Methods inherited from Adapter
#get_code, #parse_url, #shorten, #shorten_fixed_charset
Constructor Details
#initialize(cassandra, read_only = false) ⇒ CassandraAdapter
Public: Initialise the adapter with a Redis instance.
cassandra - A Cassandra instance to persist urls and codes to.
6 7 8 9 |
# File 'lib/guillotine/adapters/cassandra_adapter.rb', line 6 def initialize(cassandra, read_only = false) @cassandra = cassandra @read_only = read_only 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.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/guillotine/adapters/cassandra_adapter.rb', line 19 def add(url, code = nil, = nil) return if @read_only if existing_code = code_for(url) existing_code else code = get_code(url, code, ) if existing_url = find(code) raise DuplicateCodeError.new(existing_url, url, code) if url != existing_url end @cassandra.insert("codes", code, 'url' => url) @cassandra.insert("urls", url, 'code' => code) code end end |
#clear(url) ⇒ Object
Public: Removes the assigned short code for a URL.
url - The String URL to remove.
Returns nothing.
60 61 62 63 64 |
# File 'lib/guillotine/adapters/cassandra_adapter.rb', line 60 def clear(url) if code = code_for(url) purge(code, url) end end |
#clear_code(url) ⇒ Object
Public: Removes the assigned short code.
code - The String code to remove.
Returns nothing.
71 72 73 74 75 |
# File 'lib/guillotine/adapters/cassandra_adapter.rb', line 71 def clear_code(url) if url = find(code) purge(code, url) 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.
50 51 52 53 |
# File 'lib/guillotine/adapters/cassandra_adapter.rb', line 50 def code_for(url) obj = @cassandra.get("urls", url) obj.nil? ? nil : obj["code"] end |
#find(code) ⇒ Object
Public: Retrieves a URL from the code.
code - The String code to lookup the URL.
Returns the String URL, or nil if none is found.
40 41 42 43 |
# File 'lib/guillotine/adapters/cassandra_adapter.rb', line 40 def find(code) obj = @cassandra.get("codes", code) obj.nil? ? nil : obj["url"] end |
#purge(code, url) ⇒ Object
77 78 79 80 |
# File 'lib/guillotine/adapters/cassandra_adapter.rb', line 77 def purge(code, url) @cassandra.remove("urls", url) @cassandra.remove("codes", code) end |