Class: Corto
- Inherits:
-
Object
- Object
- Corto
- Defined in:
- lib/corto.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#db_name ⇒ Object
Returns the value of attribute db_name.
Instance Method Summary collapse
- #count ⇒ Object
- #deflate(shrunk_url) ⇒ Object
-
#initialize(db_name = nil) ⇒ Corto
constructor
A new instance of Corto.
- #purge ⇒ Object
- #shrink(url) ⇒ Object
Constructor Details
#initialize(db_name = nil) ⇒ Corto
Returns a new instance of Corto.
9 10 11 12 13 |
# File 'lib/corto.rb', line 9 def initialize (db_name = nil) @db_name= db_name @db_name= './db/corto.db' unless db_name init_db end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
7 8 9 |
# File 'lib/corto.rb', line 7 def db @db end |
#db_name ⇒ Object
Returns the value of attribute db_name.
6 7 8 |
# File 'lib/corto.rb', line 6 def db_name @db_name end |
Instance Method Details
#count ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/corto.rb', line 25 def count @db = SQLite3::Database.new(@db_name) count = @db.execute("SELECT COUNT(*) FROM urls;") @db.close # output is an array of arrays... so [[int]] count.first.first end |
#deflate(shrunk_url) ⇒ Object
48 49 50 51 52 |
# File 'lib/corto.rb', line 48 def deflate(shrunk_url) @db = SQLite3::Database.new(@db_name) result = @db.execute("SELECT original FROM urls WHERE url='" +shrunk_url+"'") (! result.first.nil?) ? result.first.first : nil end |
#purge ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/corto.rb', line 16 def purge if File.exists?(@db_name) File.delete(@db_name) end @db = SQLite3::Database.new(@db_name) @db.execute("CREATE TABLE urls (id integer primary key, url varchar2(256), original varchar2(256));") @db.close end |
#shrink(url) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/corto.rb', line 34 def shrink(url) uri = URI::parse(url) return nil unless uri.kind_of? URI::HTTP or uri.kind_of? URI::HTTPS @db = SQLite3::Database.new(@db_name) count = @db.execute("SELECT COUNT(*) FROM urls WHERE url='" +url.hash.abs.to_s(36)+"'") if count.first.first == 0 @db.execute("INSERT INTO urls (url, original) VALUES ('"+url.hash.abs.to_s(36)+ "', '"+url+"')") end @db.close url.hash.abs.to_s(36) end |