Created
December 27, 2011 19:58
-
-
Save russ/1524964 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "sequel" | |
DB = Sequel.connect("sqlite://urls.db") | |
class Url < Sequel::Model | |
plugin :schema | |
set_schema do | |
primary_key(:id) | |
text(:url) | |
end | |
create_table! unless table_exists? | |
def self.find(id) | |
url = super(id: id.to_i(36)) | |
url[:url] | |
end | |
def shorten | |
id.to_s(36) | |
end | |
end | |
puts "What do you want to do? " | |
puts "1. Shorten a url" | |
puts "2. Retreive a url" | |
print "Type number selection from above: " | |
choice = gets.chomp | |
case choice | |
when "1" | |
print "What is the url? " | |
url = gets | |
print Url.create(url: url).shorten | |
when "2" | |
print "What is the id? " | |
id = gets | |
url = Url.find(id) | |
if url.nil? | |
puts "There is no url by the id of " + id | |
else | |
puts url | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment