Sha256: 05977fefe0f927e1e89b317e4098b05bb8745d2d068032bffe55fbbb31b57649

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

require 'rubygems'
require 'dm-core'
require 'sqlite3'

class Corto
  
  attr_accessor :db_name
  attr_reader :db
  
  def initialize (db_name = nil)
    @db_name= db_name
    @db_name= './db/corto.db' unless db_name
    init_db
  end
  
  
  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
  
  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
  
  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
  
  def deflate(shrinked_url)
    @db = SQLite3::Database.new(@db_name)
    result = @db.execute("SELECT original FROM urls WHERE url='" +shrinked_url+"'")
    (! result.nil?) ? result.first.first : ''
  end
  
  private
    def init_db
      if ! File.exists?(@db_name)
        @db = SQLite3::Database.new(@db_name)
        @db.execute("CREATE TABLE urls (id integer primary key, url varchar2(256), original varchar2(256));")
        @db.close
      end
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
corto-0.60.0 lib/corto.rb