Sha256: aa03a93a014e2995fdd32e5bbdb94a8bcaf22aec34ebece9c33d22dda36c84b2
Contents?: true
Size: 1.7 KB
Versions: 2
Compression:
Stored size: 1.7 KB
Contents
require 'sqlite3' require 'fileutils' module Ush class Sqlite # An accessor to the underlying Sqlite3::Database instance attr_accessor :db # Initializes the database connection and creates the table if it doesn't exist already def initialize(file = File.expand_path(File.join(File.dirname(__FILE__), '../../../db/ush.sqlite.db'))) FileUtils.touch(file) unless File.exist?(file) # create the db if it doesnt already exist puts "Database is being stored at #{file}" @db = SQLite3::Database.new(file) @db.execute 'CREATE TABLE IF NOT EXISTS pairings(url TEXT, code TEXT)' end # Adds a url and shortcode association to the database def add_url(url, shortcode) @db.execute "INSERT INTO pairings VALUES('#{url}', '#{shortcode}')" return true rescue SQLite3::Exception => e puts 'Unable to add url:' puts e return false end # Gets the associated url for the given shortcode def get_url_for_code(code) statement = @db.prepare("SELECT * FROM pairings WHERE code='#{code}'") rs = statement.execute rs.each do |row| return row[0] # if row.class == Array # This is the first url, shouldn't be more than one. end rescue SQLite3::Exception => e puts "Unable to get url for code #{code}" puts e end # Checks if the given shortcode exists within the database def code_exists?(code) statement = @db.prepare("SELECT * FROM pairings WHERE code='#{code}'") rs = statement.execute rs.each do |row| return true if row != '' return false end rescue SQLite3::Exception => e puts "Unable to get url for code #{code}" puts e end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
ush-0.1.1 | lib/ush/db/sqlite.rb |
ush-0.1.0b | lib/ush/db/sqlite.rb |