Skip to content

Commit

Permalink
Add ability to create short urls.
Browse files Browse the repository at this point in the history
  • Loading branch information
drapergeek committed Dec 16, 2012
1 parent f7508c0 commit a79b4c7
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -14,6 +14,8 @@ gem 'jquery-rails'
gem 'thin'
gem 'flutie'
gem 'bourbon'
gem 'redis'
gem 'hiredis'

group :development, :test do
gem 'foreman'
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Expand Up @@ -64,6 +64,7 @@ GEM
foreman (0.60.2)
thor (>= 0.13.6)
hike (1.2.1)
hiredis (0.4.5)
i18n (0.6.1)
journey (1.0.4)
jquery-rails (2.1.4)
Expand Down Expand Up @@ -108,6 +109,7 @@ GEM
rake (10.0.3)
rdoc (3.12)
json (~> 1.4)
redis (3.0.2)
rspec-core (2.12.2)
rspec-expectations (2.12.1)
diff-lcs (~> 1.1.3)
Expand Down Expand Up @@ -161,9 +163,11 @@ DEPENDENCIES
coffee-rails (~> 3.2.1)
flutie
foreman
hiredis
jquery-rails
launchy
rails (= 3.2.8)
redis
rspec-rails
sass-rails (~> 3.2.3)
thin
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/short_urls_controller.rb
@@ -0,0 +1,15 @@
class ShortUrlsController < ApplicationController
def new
end

def create
short_url = ShortUrl.new(url: params[:url], name: params[:name])
if short_url.save
flash[:notice] = "Your url: http://#{HOSTNAME}/#{short_url.name}"
else
flash[:error] = "That name is already taken"
end

redirect_to new_short_url_path
end
end
28 changes: 28 additions & 0 deletions app/models/short_url.rb
@@ -0,0 +1,28 @@
class ShortUrl
attr_reader :url, :name

def initialize(options)
@name = options[:name]
@url = options[:url]
end

def save
unless existing_entry_with_same_name?
create_entry
end
end

private

def existing_entry_with_same_name?
redis.exists(@name)
end

def redis
@redis ||= Redis.new(:driver => :hiredis)
end

def create_entry
redis.set(@name, @url)
end
end
5 changes: 0 additions & 5 deletions app/views/layouts/application.html.erb
Expand Up @@ -9,11 +9,6 @@
</head>
<body class="<%= body_class %>">
<header>
<% if signed_in? -%>
<%= link_to "Sign out", sign_out_path, :method => :delete %>
<% else -%>
<%= link_to "Sign in", sign_in_path %>
<% end -%>
</header>
<%= render 'flashes' -%>
<%= yield %>
Expand Down
8 changes: 8 additions & 0 deletions app/views/short_urls/new.html.erb
@@ -0,0 +1,8 @@
<%= form_tag short_urls_path do |form| %>
<%= label_tag :url %>
<%= text_field_tag :url %>
<%= label_tag :name %>
<%= text_field_tag :name %>
<%= submit_tag 'Create'%>
<% end %>

1 change: 1 addition & 0 deletions config/initializers/constants.rb
@@ -0,0 +1 @@
HOSTNAME = ENV['HOSTNAME'] || "example.com"
2 changes: 2 additions & 0 deletions config/routes.rb
@@ -1,2 +1,4 @@
Shortstuff::Application.routes.draw do
resources :short_urls, only: [:new, :create]
root to: "short_urls#new"
end
9 changes: 5 additions & 4 deletions spec/features/create_short_url_spec.rb
Expand Up @@ -2,9 +2,10 @@
feature 'user creates a link' do
scenario 'with a custom name' do
visit '/'
fill_in 'URL', with: 'http://google.com'
fill_in 'Name', with: 'search'
click_button 'Submit'
page.should have_content 'Your url: http://example.com/search'
fill_in 'Url', with: 'http://google.com'
fill_in 'Name', with: 'Search'
click_button 'Create'

page.should have_content 'Your url: http://example.com/Search'
end
end
26 changes: 26 additions & 0 deletions spec/models/short_url_spec.rb
@@ -0,0 +1,26 @@
require 'spec_helper'

describe ShortUrl do
describe "#save" do
context 'when there is not an entry with that name' do
it "returns false" do
shorturl = ShortUrl.new(name: "myname", url: "myurl").save
shorturl = ShortUrl.new(name: "myname", url: "http://www.google.com")
expect(shorturl.save).to be_false
end
end

context 'when there is not an entry with that name' do
it 'returns true' do
shorturl = ShortUrl.new(name: "myname", url: "myurl")
expect(shorturl.save).to be_true
end
end

it "will set the url and name properly" do
shorturl = ShortUrl.new(name: "myname", url: "myurl")
expect(shorturl.name).to eq("myname")
expect(shorturl.url).to eq("myurl")
end
end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -35,6 +35,10 @@
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.before(:each) do
redis = Redis.new(:driver => :hiredis)
redis.flushall
end
end

Capybara.javascript_driver = :webkit

0 comments on commit a79b4c7

Please sign in to comment.