Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
marekjelen committed Apr 6, 2012
0 parents commit 3c9b847
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source :rubygems

gem 'sinatra'
gem 'erubis'
gem 'mongoid'

gem 'json'
gem 'httparty'
44 changes: 44 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
GEM
remote: http://rubygems.org/
specs:
activemodel (3.2.3)
activesupport (= 3.2.3)
builder (~> 3.0.0)
activesupport (3.2.3)
i18n (~> 0.6)
multi_json (~> 1.0)
bson (1.6.2)
builder (3.0.0)
erubis (2.7.0)
httparty (0.8.1)
multi_json
multi_xml
i18n (0.6.0)
json (1.6.6)
mongo (1.6.2)
bson (~> 1.6.2)
mongoid (2.4.7)
activemodel (~> 3.1)
mongo (~> 1.3)
tzinfo (~> 0.3.22)
multi_json (1.2.0)
multi_xml (0.4.2)
rack (1.4.1)
rack-protection (1.2.0)
rack
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
tilt (1.3.3)
tzinfo (0.3.32)

PLATFORMS
ruby

DEPENDENCIES
erubis
httparty
json
mongoid
sinatra
9 changes: 9 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'rubygems'
require 'bundler/setup'

$:.unshift(File.expand_path('../lib', __FILE__))

require 'database'
require 'web'

run BeerShift
14 changes: 14 additions & 0 deletions config/mongoid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
development:
host: localhost
database: beershift_development

test:
host: localhost
database: beershift_test

production:
host: <%= ENV['OPENSHIFT_NOSQL_DB_HOST'] %>
port: <%= ENV['OPENSHIFT_NOSQL_DB_PORT'] %>
database: <%= ENV['OPENSHIFT_APP_NAME'] %>
username: <%= ENV['OPENSHIFT_NOSQL_DB_USERNAME'] %>
password: <%= ENV['OPENSHIFT_NOSQL_DB_PASSWORD'] %>
47 changes: 47 additions & 0 deletions lib/database.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'mongoid'

Mongoid.load!(File.expand_path('../../config/mongoid.yml', __FILE__))

class User

include Mongoid::Document

field :username, :type => String
field :password, :type => String

def self.create_user(username, password)
u = self.new
u.username = username
u.password = password
u.save
end

end

class Beer

include Mongoid::Document

store_in :drank

field :username, :type => String
field :when, :type => DateTime
field :beer, :type => String

def self.firehose
self.limit(50).order_by([[:when, :desc]]).all
end

def self.for_user(username)
Beer.where(:username => username).limit(50).order_by([[:when, :desc]]).all
end

def self.drink(username, beer, date)
b = self.new
b.username = username
b.beer = beer
b.when = date
b.save
end

end
117 changes: 117 additions & 0 deletions lib/web.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
require 'sinatra/base'
require 'httparty'
require 'json'

class BeerShift < Sinatra::Base

set :views, File.expand_path('../../views', __FILE__)

get '/' do

@beers = Beer.firehose
erb :index

end

get '/user/username/:username' do

halt 400 unless params[:username]

username = params[:username].downcase
user = User.where(:username => username).first

if user
JSON.dump({ :username => user.username, :password => user.password })
else
halt 404
end

end

post '/user' do

halt 400 unless params[:username]
halt 400 unless params[:password]

username = params[:username].downcase

user = User.create_user(username, params[:password])

if user
JSON.dump({ :status => 'success' })
else
JSON.dump({ :status => 'failed' })
end

end

get '/userbeers/username/:username' do

halt 400 unless params[:username]

username = params[:username].downcase

beers = Beer.for_user(username)

if beers
response = beers.inject([]) { |list, beer| list << { :username => beer.username, :beer => beer.beer, :when => beer.when } }
JSON.dump(response)
else
halt 404
end

end

get '/firehose' do

beers = Beer.firehose

if beers
response = beers.inject([]) { |list, beer| list << { :username => beer.username, :beer => beer.beer, :when => beer.when } }
JSON.dump(response)
else
halt 404
end

end

get '/beers/name/:name' do

halt 400 unless params[:name]

query = {
:key => 'A1029384756B',
:q => params[:name],
:type => 'beer',
:withBreweries => 'Y'
}
response = HTTParty.post('http://api.playground.brewerydb.com/search/', :query => query)
parsed = JSON.parse(response.body)

if parsed['status'] && parsed['status'] == 'success'
response
else
halt 404
end

end

post '/beers' do

halt 400 unless params[:username]
halt 400 unless params[:beerName]
halt 400 unless params[:when]

username = params[:username].downcase

beer = Beer.drink(username, params[:beerName], DateTime.strptime(params[:when].gsub(/(\d+)(th|st|nd|rd)/, '\1'), '%B %e, %Y, %l:%M:%S %p'))

if beer
JSON.dump({ :status => 'success' })
else
JSON.dump({ :status => 'failed' })
end

end

end
80 changes: 80 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to BeerShift</title>
<link rel="SHORTCUT ICON" href="http://www.redhat.com/favicon.ico" />
<style type="text/css">

::selection{ background-color: #E13300; color: white; }
::moz-selection{ background-color: #E13300; color: white; }
::webkit-selection{ background-color: #E13300; color: white; }

body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}

a {
color: #003399;
background-color: transparent;
font-weight: normal;
}

h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 19px;
font-weight: normal;
margin: 0 0 14px 0;
padding: 14px 15px 10px 15px;
}

code {
font-family: Consolas, Monaco, Courier New, Courier, monospace;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}

#body{
margin: 0 15px 0 15px;
}

p.footer{
text-align: right;
font-size: 11px;
border-top: 1px solid #D0D0D0;
line-height: 32px;
padding: 0 10px 0 10px;
margin: 20px 0 0 0;
}

#container{
margin: 10px;
border: 1px solid #D0D0D0;
-webkit-box-shadow: 0 0 8px #D0D0D0;
}
</style>
</head>
<body>

<div id="container">
<h1>Welcome to BeerShift!</h1>
<div id="body">
<p>Have a look-see at who is drinking.....</p>
<% @beers.each do |beer| %>
<code><%= beer.username %> drank <%= beer.beer %>! (<%= beer.when%>)</code>
<% end %>
</div>
</div>

</body>
</html>

0 comments on commit 3c9b847

Please sign in to comment.