Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7508c0
commit a79b4c7
Showing
11 changed files
with
95 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
HOSTNAME = ENV['HOSTNAME'] || "example.com" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
Shortstuff::Application.routes.draw do | ||
resources :short_urls, only: [:new, :create] | ||
root to: "short_urls#new" | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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