-
Notifications
You must be signed in to change notification settings - Fork 0
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
Mike Edwards
committed
Sep 5, 2013
0 parents
commit 4ec20c2
Showing
12 changed files
with
325 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
*.swp | ||
tmp | ||
.bundle | ||
foreman/* |
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,3 @@ | ||
--color | ||
--format documentation | ||
--backtrace |
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 @@ | ||
rvm ruby-1.9.3-p194@fio --create |
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,12 @@ | ||
source 'http://rubygems.org' | ||
|
||
#setup the server gems | ||
gem 'sinatra', '1.4.3' | ||
gem 'thin', '1.5.1' | ||
|
||
gem "rake", "~> 10.1.0" | ||
|
||
group :test do | ||
gem 'rspec', '2.12.0' | ||
gem 'rack-test', '0.6.2' | ||
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,39 @@ | ||
GEM | ||
remote: http://rubygems.org/ | ||
specs: | ||
daemons (1.1.9) | ||
diff-lcs (1.1.3) | ||
eventmachine (1.0.3) | ||
rack (1.4.3) | ||
rack-protection (1.5.0) | ||
rack | ||
rack-test (0.6.2) | ||
rack (>= 1.0) | ||
rake (10.1.0) | ||
rspec (2.12.0) | ||
rspec-core (~> 2.12.0) | ||
rspec-expectations (~> 2.12.0) | ||
rspec-mocks (~> 2.12.0) | ||
rspec-core (2.12.2) | ||
rspec-expectations (2.12.1) | ||
diff-lcs (~> 1.1.3) | ||
rspec-mocks (2.12.1) | ||
sinatra (1.4.3) | ||
rack (~> 1.4) | ||
rack-protection (~> 1.4) | ||
tilt (~> 1.3, >= 1.3.4) | ||
thin (1.5.1) | ||
daemons (>= 1.0.9) | ||
eventmachine (>= 0.12.6) | ||
rack (>= 1.0.0) | ||
tilt (1.4.1) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
rack-test (= 0.6.2) | ||
rake (~> 10.1.0) | ||
rspec (= 2.12.0) | ||
sinatra (= 1.4.3) | ||
thin (= 1.5.1) |
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 @@ | ||
web: bundle exec rackup -s thin -p $PORT |
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,133 @@ | ||
# fio - File Input Output Generator | ||
|
||
## Dependencies | ||
* You will need to setup an rvm environment running ruby-1.9.3-p194. | ||
* You will also need to create a gemset called `fio` | ||
* note: make sure you remember to issue `rvm use ruby-1.9.3-p194@fio` from the command line. | ||
|
||
## Setup | ||
1. Install the dependencies | ||
2. Run `gem install bundler` | ||
3. Run `bundle install` | ||
4. Run `bundle exec rackup -s thin -p 8000` | ||
|
||
## Tests | ||
1. Run `RACK_ENV=test bundle exec rake spec` | ||
|
||
## API Spec for Web based clients | ||
|
||
##### Notes: | ||
|
||
- This doc and APIs are a work in progress. The APIs signatures and responses should not be considered final in any form. | ||
See the section on versioning for details on currently supported versions. | ||
|
||
- All calls will use HTTP endpoints. | ||
- All requests are in text format and must include a valid URI string. | ||
- All responses will be in JSON format. | ||
- GET requests must include a url key in order for a redirect to be generated. | ||
|
||
#### Summary of the HTTP Status Codes: | ||
|
||
200 - OK | ||
404 - Bad request - we return a custom message | ||
500 - Server Error | ||
|
||
## API Features | ||
|
||
##### API Endpoints | ||
|
||
* Staging - http://staging.sbrd.me | ||
|
||
* Production - http://www.sbrd.me | ||
|
||
##### Url Minifier | ||
|
||
A client may shorten any valid url by submitting a POST request to the service. The POST request must contain | ||
a key labeled 'original' and the value of this key must be a valid URI. | ||
|
||
Sample request with curl: | ||
|
||
curl --data 'original=http://www.songbird.me' http://www.sbrd.me | ||
|
||
Valid Response: | ||
|
||
{ | ||
"url" : "http://www.sbrd.me/1" | ||
} | ||
Status Code: 200 | ||
|
||
Sample request with curl to Staging: | ||
|
||
curl --data 'original=http://www.songbird.me/' http://staging.sbrd.me | ||
|
||
Valid Response: | ||
|
||
{ | ||
"url" : "http://staging.sbrd.me/1" | ||
} | ||
Status Code: 200 | ||
|
||
##### Minified Url Redirector | ||
|
||
A client may submit a minified url back to the service in order to be redirected back to the original url contained | ||
within the database. Clients need only to submit a parameter with the GET request and the system will lookup the | ||
original URL and generate a 301 redirect to the original URL. | ||
|
||
Sample request with curl: | ||
|
||
curl http://www.sbrd.me/1 -v | ||
|
||
Valid Response: | ||
|
||
< HTTP/1.1 301 Moved Permanently | ||
< Content-Type: text/html;charset=utf-8 | ||
< Location: http://www.songbird.me | ||
< Server: thin 1.2.7 codename No Hup | ||
< X-Content-Type-Options: nosniff | ||
< X-Frame-Options: SAMEORIGIN | ||
< X-Xss-Protection: 1; mode=block | ||
< Content-Length: 0 | ||
< Connection: keep-alive | ||
< | ||
|
||
## Branches | ||
|
||
This repo consist of three branches, they are: | ||
1. checkin | ||
2. 7digital | ||
3. birdhouse | ||
|
||
The branches correspond to apps running at heroku, each with a slightly different version of the sbrdme.rb file. | ||
We use the sbrdme platform to act as a router for two of the Songbird Classic Desktop legacy apps. 7digital refers | ||
to the old 7digital music store which used to be available to users of the Classic Desktop. Birdhouse refers to | ||
the old startup url for first time users of the Classic Desktop application. Since there are specific urls which | ||
these apps use it, was preferable to just create separate apps at heroku to handle the routing requests. Since the | ||
base code was very similar it was simplest to just branch off of checkin and then fine tune the routes for each | ||
application. Then the appropriate apps were created at heroku and each branch pushed to the master for the app. | ||
|
||
#### Pushing | ||
To push to each app you'll need to do the following: | ||
|
||
##### sbrdme-staging: | ||
git remote add staging git@heroku.com:sbrdme-staging.git | ||
git push staging checkin:master | ||
|
||
##### sbrdme-producion: | ||
git remote add production git@heroku.com:sbrdme-production.git | ||
git push production checkin:master | ||
|
||
##### sbrdme-7digital-staging | ||
git remote add 7digital-staging git@heroku.com:sbrdme-7digital-staging.git | ||
git push 7digital-staging 7digital:master | ||
|
||
##### sbrdme-7digital-production | ||
git remote add 7digital-production git@heroku.com:sbrdme-7digital-production.git | ||
git push 7digital-production 7digital:master | ||
|
||
##### sbrdme-birdhouse-staging | ||
git remote add birdhouse-staging git@heroku.com:sbrdme-birdhouse-staging.git | ||
git push birdhouse-staging birdhouse:master | ||
|
||
#####sbrdme-birdhouse-production | ||
git remote add birdhouse-production git@heroku.com:sbrdme-birdhouse-production.git | ||
git push birdhouse-production birdhouse:master |
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,7 @@ | ||
desc "run the tests!" | ||
task :spec do | ||
require 'rspec/core/rake_task' | ||
RSpec::Core::RakeTask.new do |t| | ||
t.pattern = "spec/**/*_spec.rb" | ||
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,3 @@ | ||
require 'sinatra' | ||
require './fio' | ||
run Sinatra::Application |
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,48 @@ | ||
%w(sinatra data_mapper json).each { |lib| require lib} | ||
|
||
require_relative 'lib/url.rb' | ||
|
||
DataMapper::Logger.new($stdout, :info) | ||
DataMapper.setup(:default, ENV['DATABASE_URL'] || "mysql://root:#{ENV['DB_PWD'] || 'password'}@localhost/sbrdme") | ||
|
||
configure :test do | ||
DataMapper.setup(:default, "mysql://root:#{ENV['DB_PWD'] || 'password'}@localhost/sbrdme_test") | ||
DataMapper.auto_migrate! | ||
end | ||
|
||
#DataMapper.auto_migrate! | ||
DataMapper.finalize | ||
|
||
error do | ||
content_type :json | ||
status 500 | ||
|
||
e = env['sinatra.error'] | ||
{:status => 500, :message => e.message}.to_json | ||
end | ||
|
||
not_found do | ||
status 404 | ||
'The page you are looking for does not exist.' | ||
end | ||
|
||
get '/' do redirect 'http://songbird.me', 301 end | ||
|
||
post '/' do | ||
content_type :json | ||
|
||
host = request.host | ||
port = request.port | ||
host << ":#{port}" if port != 80 | ||
|
||
uri = URI::parse(params[:original]) | ||
raise "Invalid URL" unless uri.kind_of? URI::HTTP or uri.kind_of? URI::HTTPS | ||
url = Url.first_or_create(:original => uri.to_s) | ||
|
||
{ 'url' => 'http://' + host + '/' + url.out }.to_json | ||
end | ||
|
||
get '/:url' do | ||
url = Url.first(:id => params[:url].to_i(36)) | ||
url ? redirect(url.original, 301) : raise(Sinatra::NotFound) | ||
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,52 @@ | ||
require 'spec_helper' | ||
|
||
describe 'App' do | ||
|
||
describe 'GET / ' do | ||
it 'should redirect to songbird.me' do | ||
get '/' | ||
last_response.should be_redirect | ||
follow_redirect! | ||
last_request.url.should == original | ||
end | ||
end | ||
|
||
describe 'POST /' do | ||
it "should create a db entry for the url" do | ||
post "/", :original => original | ||
|
||
Url.count(:original => original).should == 1 | ||
end | ||
|
||
it 'should respond with a minified url' do | ||
post "/", :original => original | ||
|
||
res = JSON.parse(last_response.body) | ||
|
||
res["url"].should_not be_nil | ||
end | ||
|
||
context 'when the url already exists' do | ||
|
||
let :url do | ||
Url.create(:original => original) | ||
end | ||
|
||
before do | ||
url | ||
end | ||
|
||
it "should not create a new entry for the url" do | ||
Url.count(:original => original).should == 1 | ||
|
||
post "/", :original => original | ||
|
||
Url.count(:original => original).should == 1 | ||
end | ||
|
||
it "should return a short url" do | ||
end | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
ENV["RACK_ENV"] ||= "test" | ||
|
||
require "bundler" | ||
require "rack/test" | ||
|
||
Bundler.require(:defualt, :test) | ||
|
||
require File.expand_path(File.join(File.dirname(__FILE__), | ||
"..", | ||
"sbrdme.rb")) | ||
|
||
RSpec.configure do |config| | ||
config.mock_with :rspec | ||
config.include Rack::Test::Methods | ||
config.before(:each) { DataMapper.auto_migrate! } | ||
end | ||
|
||
def app | ||
Sinatra::Application | ||
end | ||
|