Class: Guillotine::SequelAdapter
- Defined in:
- lib/guillotine/adapters/sequel_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(code) ⇒ 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(db) ⇒ SequelAdapter
constructor
A new instance of SequelAdapter.
- #select(field, query) ⇒ Object
- #setup ⇒ Object
Methods inherited from Adapter
#get_code, #parse_url, #shorten, #shorten_fixed_charset
Constructor Details
#initialize(db) ⇒ SequelAdapter
Returns a new instance of SequelAdapter.
3 4 5 6 |
# File 'lib/guillotine/adapters/sequel_adapter.rb', line 3 def initialize(db) @db = db @table = @db[:urls] 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.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/guillotine/adapters/sequel_adapter.rb', line 16 def add(url, code = nil, = nil) if existing = code_for(url) existing else code = get_code(url, code, ) begin @table << {:url => url, :code => code} rescue Sequel::DatabaseError if existing_url = @table.select(:url).where(:code => code).first raise DuplicateCodeError.new(existing_url, url, code) else raise end end code end end |
#clear(url) ⇒ Object
Public: Removes the assigned short code for a URL.
url - The String URL to remove.
Returns nothing.
57 58 59 |
# File 'lib/guillotine/adapters/sequel_adapter.rb', line 57 def clear(url) @table.where(:url => url).delete end |
#clear_code(code) ⇒ Object
Public: Removes the assigned short code.
code - The String code to remove.
Returns nothing.
66 67 68 |
# File 'lib/guillotine/adapters/sequel_adapter.rb', line 66 def clear_code(code) @table.where(:code => code).delete 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.
48 49 50 |
# File 'lib/guillotine/adapters/sequel_adapter.rb', line 48 def code_for(url) select :code, :url => url end |
#find(code) ⇒ Object
Public: Retrieves a URL from the code.
code - The String code to lookup the URL.
Returns the String URL.
39 40 41 |
# File 'lib/guillotine/adapters/sequel_adapter.rb', line 39 def find(code) select :url, :code => code end |
#select(field, query) ⇒ Object
80 81 82 83 84 |
# File 'lib/guillotine/adapters/sequel_adapter.rb', line 80 def select(field, query) if row = @table.select(field).where(query).first row[field] end end |
#setup ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/guillotine/adapters/sequel_adapter.rb', line 70 def setup @db.create_table :urls do String :url String :code unique :url unique :code end end |