Class: Guillotine::ActiveRecordAdapter

Inherits:
Adapter
  • Object
show all
Defined in:
lib/guillotine/adapters/active_record_adapter.rb

Defined Under Namespace

Classes: Url

Instance Method Summary collapse

Methods inherited from Adapter

#get_code, #parse_url, #shorten, #shorten_fixed_charset

Constructor Details

#initialize(config = nil) ⇒ ActiveRecordAdapter

Returns a new instance of ActiveRecordAdapter.



7
8
9
# File 'lib/guillotine/adapters/active_record_adapter.rb', line 7

def initialize(config=nil)
  Url.establish_connection config if config
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
34
# File 'lib/guillotine/adapters/active_record_adapter.rb', line 19

def add(url, code = nil, options = nil)
  if row = Url.select(:code).where(:url => url).first
    row[:code]
  else
    code = get_code(url, code, options)

    begin
      Url.create :url => url, :code => code
    rescue ActiveRecord::RecordNotUnique, ActiveRecord::StatementInvalid
      row = Url.select(:url).where(:code => code).first
      existing_url = row && row[:url]
      raise DuplicateCodeError.new(existing_url, url, code)
    end
    code
  end
end

#clear(url) ⇒ Object

Public: Removes the assigned short code for a URL.

url - The String URL to remove.

Returns nothing.



59
60
61
# File 'lib/guillotine/adapters/active_record_adapter.rb', line 59

def clear(url)
  Url.where(:url => url).delete_all
end

#clear_code(code) ⇒ Object

Public: Removes the assigned short code.

code - The String code to remove.

Returns nothing.



68
69
70
# File 'lib/guillotine/adapters/active_record_adapter.rb', line 68

def clear_code(code)
  Url.where(:code => code).delete_all
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
# File 'lib/guillotine/adapters/active_record_adapter.rb', line 50

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.



41
42
43
# File 'lib/guillotine/adapters/active_record_adapter.rb', line 41

def find(code)
  select :url, :code => code
end

#select(field, query) ⇒ Object



83
84
85
86
87
# File 'lib/guillotine/adapters/active_record_adapter.rb', line 83

def select(field, query)
  if row = Url.select(field).where(query).first
    row[field]
  end
end

#setupObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/guillotine/adapters/active_record_adapter.rb', line 72

def setup
  conn = Url.connection
  conn.create_table :urls do |t|
    t.string :url
    t.string :code
  end

  conn.add_index :urls, :url, :unique => true
  conn.add_index :urls, :code, :unique => true
end