Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add ability to create short urls.

  • Loading branch information...
commit a79b4c702b931ef737bf6f773482b0b97c1dc871 1 parent f7508c0
Jason Draper authored
2  Gemfile
View
@@ -14,6 +14,8 @@ gem 'jquery-rails'
gem 'thin'
gem 'flutie'
gem 'bourbon'
+gem 'redis'
+gem 'hiredis'
group :development, :test do
gem 'foreman'
4 Gemfile.lock
View
@@ -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)
@@ -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)
@@ -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
15 app/controllers/short_urls_controller.rb
View
@@ -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 app/models/short_url.rb
View
@@ -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 app/views/layouts/application.html.erb
View
@@ -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 %>
8 app/views/short_urls/new.html.erb
View
@@ -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  config/initializers/constants.rb
View
@@ -0,0 +1 @@
+HOSTNAME = ENV['HOSTNAME'] || "example.com"
2  config/routes.rb
View
@@ -1,2 +1,4 @@
Shortstuff::Application.routes.draw do
+ resources :short_urls, only: [:new, :create]
+ root to: "short_urls#new"
end
9 spec/features/create_short_url_spec.rb
View
@@ -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 spec/models/short_url_spec.rb
View
@@ -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 spec/spec_helper.rb
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.