Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Commit

Permalink
TwitterInfo sinatra app with specs
Browse files Browse the repository at this point in the history
  • Loading branch information
bfaloona committed Nov 8, 2011
0 parents commit 87c47be
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.DS_Store
13 changes: 13 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
source :rubygems

gem 'sinatra'
gem 'twitter'
gem 'heroku'
gem 'haml'

group :test do
gem 'guard'
gem 'guard-rspec'
gem 'rspec'
gem 'rack-test'
end
6 changes: 6 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard 'rspec', :version => 2, :cli => "--color --format d" do
watch(%r{^spec/.+_spec\.rb$})
end
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Week 6 Sinatra Heroku Example

## Outline

* Developing, testing and deploying web services
** Rack and Sinatra
** Rack::Test
** Heroku
* Exercise
* Exploration
** Travis Continuous Integration

## Developing, Testing and Deploying web services

### Rack

[Rack](http://rack.rubyforge.org/) is an minimal interface for developing web applications.
Rails, Sinatra, and many other web frameworks are written on top of Rack.
If you want the simplest possible web application you can use the `rack` gem
and a file named config.ru that contains the following code:

app = lambda do |env|
body = "Hello, World!"
[200, {"Content-Type" => "text/plain", "Content-Length" => body.length.to_s}, [body]]
end

run app

To run the above application, simply use the command:

rackup

Your application is available at http://localhost:9292 once you see something like the following in your console:

[2011-11-08 09:06:12] INFO WEBrick 1.3.1
[2011-11-08 09:06:12] INFO ruby 1.9.2 (2011-07-09) [x86_64-darwin11.1.0]
[2011-11-08 09:06:12] INFO WEBrick::HTTPServer#start: pid=4318 port=9292

### Rack::Test

[Rack::Test](https://github.com/brynary/rack-test) is a small, simple testing API for Rack apps.
Rack uses mock objects to allow inspection of http requests and responses without actually starting your
web application or making any client requests. Don't worry about understanding mocks right now, except to understand
it is one more trick in the ruby toolbox that makes developing and testing easier.

# lib/my_app.rb
require 'sinatra'
get '/' do
'Hello World!'
end

# spec/my_app_spec.rb
require 'my_app'
require 'rack/test'
describe "my app" do
include Rack::Test::Methods
def app
Sinatra::Application
end

it "should say hello" do
# first we use rack-test to 'pretend' to make a GET request against the root of the server
get "/"

# rack-test creates a last_request and last_response object that we can interrogate
last_request.env['REQUEST_METHOD'].should == 'GET'
last_response.body.should match(/Hello World/)
last_response.status.should == 200
end
end

### Heroku

[Heroku](http://www.heroku.com/) is a cloud application platform that makes deploying web
applications trivial (and free).

1. Create an account if you don’t have one
1. gem install heroku
1. Make a config.ru in the root-directory
1. Create the app on heroku
1. Push to it

[Detailed Example](https://github.com/sinatra/heroku-sinatra-app)

## Excercise

## Exploration
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

desc "`rake` will default to running `rake:spec`"
task :default => :spec

desc "Run all the rspec examples"
task :spec do
system "bundle exec rspec -c -f d spec/*_spec.rb"
end
5 changes: 5 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$LOAD_PATH << File.dirname(__FILE__)

require 'lib/app'

run TwitterInfo
31 changes: 31 additions & 0 deletions lib/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'sinatra'
require 'twitter'
require 'haml'

class TwitterInfo < Sinatra::Application

set :views, settings.root + '/../views'

get '/' do
'<html><body>Append a twitter username on the url to display
how many followers the user has.<br/> For example:<br/><a href=\'/burtlo\'>http://localhost:9292/user/<strong>burtlo</strong></a>
</body></html>'
end

get /^\/user\/(\S+)$/ do |user|

@user = user

user_id = Twitter.user(@user).id

followers = Twitter.follower_ids(user_id).ids

@num_followers = followers.length
haml :index
end

post // do
[500, nil, 'Whoa. Sorry. No POSTs allowed.']
end

end
34 changes: 34 additions & 0 deletions spec/app_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'app'
require 'rspec'
require 'rack/test'

set :environment, :test

describe "Twitter Info" do
include Rack::Test::Methods

def app
TwitterInfo
end

it "should provide instructions" do

get "/"

last_response.body.should match(/Append a twitter username on the url to display/)
last_response.status.should == 200

end

it "should return a 500 status for any POST" do

post "/user/burtlo"

last_response.status.should == 500
last_response.body.should match(/Sorry/)

end

it "should retrieve the user's follower count for any valid username"

end
8 changes: 8 additions & 0 deletions views/index.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
%html
%head
%title Twitter Info Application

%body
%h4 #{@user}

%h1 #{@num_followers}

0 comments on commit 87c47be

Please sign in to comment.