sinatra-example 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENSE +12 -0
  2. data/README.md +184 -0
  3. data/bin/sinatra-example +11 -0
  4. data/lib/application.rb +22 -0
  5. metadata +181 -0
data/LICENSE ADDED
@@ -0,0 +1,12 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2008 Simon Rozet <simon@rozet.name>
5
+ Everyone is permitted to copy and distribute verbatim or modified
6
+ copies of this license document, and changing it is allowed as long
7
+ as the name is changed.
8
+
9
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
10
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
11
+
12
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
@@ -0,0 +1,184 @@
1
+ This is the result of a blog post on creating a testable Sinatra app. If you just want to use it, clone
2
+ the repository, and start hacking. Below is the blog post that created it, which can also be found at
3
+ http://blog.blankpad.net/2010/07/04/starting-a-testable-sinatra-application/.
4
+
5
+ I'm getting into using Sinatra for smaller web applications, instead of the behemoth that is Rails.
6
+
7
+ For many purposes, Rails is still perfect, but I often find that it can be overkill if all you need
8
+ is to display a couple of pages, and possibly provide an API. Here's a quick guide on creating a
9
+ Sinatra application that can be tested using RSpec and Cucumber.
10
+
11
+ Install Bundler, and Create a Gemfile
12
+ -------------------------------------
13
+
14
+ [Bundler](http://gembundler.com) lets us manage the list of gems that our application depends on. I'm
15
+ using it on all new projects now, so that I don't have to spend half an hour remembering what needs to
16
+ be installed before starting.
17
+
18
+ gem install bundler
19
+ cd /path/to/example_app
20
+ bundle init
21
+
22
+ That will create `Gemfile` in the example application's root directory, which we can use to specify the
23
+ gems our application needs. Put the following in it for now:
24
+
25
+ source :gemcutter
26
+
27
+ gem "sinatra"
28
+ gem "unicorn"
29
+ gem "haml"
30
+
31
+ group :test do
32
+ gem "cucumber-sinatra"
33
+ gem "cucumber"
34
+ gem "capybara"
35
+ gem "rspec"
36
+ end
37
+
38
+ Now when you run `bundle install` it'll install any dependencies that are required.
39
+
40
+ Create a Stub Application
41
+ -------------------------
42
+
43
+ This is going to be a really simple stub application for now, since this article isn't really trying to
44
+ teach you how to use Sinatra.
45
+
46
+ Put the following in `lib/application.rb`.
47
+
48
+ require 'sinatra/base'
49
+ require 'haml'
50
+
51
+ class Application < Sinatra::Base
52
+ set :app_file, __FILE__
53
+ set :inline_templates, true
54
+
55
+ get '/' do
56
+ haml :index
57
+ end
58
+ end
59
+
60
+ __END__
61
+ @@ index
62
+ !!!
63
+ %html
64
+ %head
65
+ %title A test application
66
+ %body
67
+ %h1 Hello, world!
68
+
69
+ Just to check that it's all working, dump this in `config.ru`, which will load everything using
70
+ Bundler.
71
+
72
+ $: << "lib"
73
+
74
+ require 'application'
75
+ run Application
76
+
77
+ And run it with `bundle exec unicorn` - loading `bundle exec` ensures all the gems specified have been
78
+ loaded from the appropriate location before running the application itself.
79
+
80
+ Add a Slice of Cucumber
81
+ -----------------------
82
+
83
+ Now we're going to make this testable with Cucumber:
84
+
85
+ bundle exec cucumber-sinatra init Application lib/application.rb
86
+
87
+ That will tell you about the files it's generating. Lets have a look at `features/support/env.rb` which
88
+ is where Cucumber gets configured. You'll see that it simply loads `application.rb` and then tells
89
+ Capybara (the component that actually runs web apps) that the application under test is `Application`.
90
+
91
+ Lets write a feature to test that our simple application is in fact working. Put this in
92
+ `features/hello.feature`.
93
+
94
+ Feature: Welcoming new developers
95
+ As a software developer
96
+ I want the world to be welcomed
97
+ So I get a fuzzy feeling of success
98
+
99
+ Scenario: Loading the welcome page
100
+ When I go to the home page
101
+ Then I should see "Hello, world!"
102
+
103
+ When you run it with `bundle exec features/hello.feature` it should pass.
104
+
105
+ Cucumber Profiles
106
+ -----------------
107
+
108
+ By using profiles we can make use of the WIP (Work in Progress) tag to reduce the time it takes to run
109
+ our features when we're only interested in one or two features.
110
+
111
+ Start by creating `cucumber.yml` and put the following in it:
112
+
113
+ <% common = "--strict features" %>
114
+ default: --format progress <%= common %>
115
+ wip: --format pretty --tags @wip <%= common %>
116
+ ok: --format pretty --tags ~@wip <%= common %>
117
+
118
+ This will provide you with three profiles, `default`, `wip`, and `ok` for different tasks. `default` will
119
+ run all features, in progress mode - this is probably the one you want to use for continuous integration.
120
+
121
+ `wip` and `ok` are opposites, with `wip` running anything with the `@wip` tag applied, and `ok` doing
122
+ the opposite.
123
+
124
+ Now lets create some rake tasks to run them - put this in `Rakefile`:
125
+
126
+ require 'cucumber'
127
+ require 'cucumber/rake/task'
128
+
129
+ namespace :features do
130
+ Cucumber::Rake::Task.new(:all) do |t|
131
+ t.profile = "default"
132
+ end
133
+
134
+ Cucumber::Rake::Task.new(:ok) do |t|
135
+ t.profile = "ok"
136
+ end
137
+
138
+ Cucumber::Rake::Task.new(:all) do |t|
139
+ t.profile = "wip"
140
+ end
141
+ end
142
+
143
+ Now when you run the rake tasks you'll get the appropriate features run.
144
+
145
+ And now for some RSpec
146
+ ----------------------
147
+
148
+ Finally, we're going to set up RSpec. This probably won't be at all unusual to you if you've done it
149
+ before, but I'm going to demonstrate it as well.
150
+
151
+ In your Rakefile:
152
+
153
+ require 'spec/rake/spectask'
154
+
155
+ namespace :spec do
156
+ desc "Run all examples"
157
+ Spec::Rake::SpecTask.new('spec') do |t|
158
+ t.spec_files = FileList['spec/**/*_spec.rb']
159
+ end
160
+ end
161
+
162
+ Then put this in `spec/spec_helper.rb` to make sure anything in lib/ can be loaded correctly:
163
+
164
+ $: << File.expand_path(File.dirname(__FILE__) + '/../lib')
165
+
166
+ And that's about it. Create a `spec` directory, and start filling it up with specs ending in `_spec.rb`,
167
+ like this one (in `spec/application_spec.rb`):
168
+
169
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
170
+ require 'application'
171
+
172
+ describe Application do
173
+ it { should_not be_nil }
174
+ end
175
+
176
+ Now when you run `rake spec:all` if all went well you'll see a passing spec.
177
+
178
+ On With the Code
179
+ ----------------
180
+
181
+ That's as far as I'm going to go with this for now. Go write some code.
182
+
183
+ You can find the end result of this in the [sinatra-example](http://github.com/jellybob/sinatra-example)
184
+ project under my [GitHub account](http://github.com/jellybob/).
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ current_dir = File.expand_path(File.dirname(__FILE__))
3
+ base_dir = current_dir + '/../lib'
4
+ $: << base_dir unless $:.include?(base_dir)
5
+
6
+ require 'rack'
7
+ require 'application'
8
+
9
+ Rack::Handler::WEBrick.run \
10
+ Application,
11
+ :Port => 9292
@@ -0,0 +1,22 @@
1
+ require 'sinatra/base'
2
+ require 'haml'
3
+
4
+ class Application < Sinatra::Base
5
+ set :app_file, __FILE__
6
+ set :haml, { :format => :html5,
7
+ :attr_wrapper => '"' }
8
+ set :inline_templates, true
9
+
10
+ get '/' do
11
+ haml :index
12
+ end
13
+ end
14
+
15
+ __END__
16
+ @@ index
17
+ !!!
18
+ %html
19
+ %head
20
+ %title A test application
21
+ %body
22
+ %h1 Hello, world!
metadata ADDED
@@ -0,0 +1,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-example
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ version: "1.0"
10
+ platform: ruby
11
+ authors:
12
+ - Jon Wood
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-04 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rack
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: sinatra
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: haml
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :runtime
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: shotgun
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ type: :development
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: rspec
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ type: :development
89
+ version_requirements: *id005
90
+ - !ruby/object:Gem::Dependency
91
+ name: cucumber
92
+ prerelease: false
93
+ requirement: &id006 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ type: :development
103
+ version_requirements: *id006
104
+ - !ruby/object:Gem::Dependency
105
+ name: cucumber-sinatra
106
+ prerelease: false
107
+ requirement: &id007 !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ type: :development
117
+ version_requirements: *id007
118
+ - !ruby/object:Gem::Dependency
119
+ name: capybara
120
+ prerelease: false
121
+ requirement: &id008 !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ type: :development
131
+ version_requirements: *id008
132
+ description: This is a simple example I built while playing with Sinatra and Cucumber a bit, so that I could get to a good starting point.
133
+ email:
134
+ - jon@blankpad.net
135
+ executables:
136
+ - sinatra-example
137
+ extensions: []
138
+
139
+ extra_rdoc_files: []
140
+
141
+ files:
142
+ - bin/sinatra-example
143
+ - lib/application.rb
144
+ - LICENSE
145
+ - README.md
146
+ has_rdoc: true
147
+ homepage: http://github.com/jellybob/sinatra-example
148
+ licenses: []
149
+
150
+ post_install_message:
151
+ rdoc_options: []
152
+
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ none: false
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ hash: 3
161
+ segments:
162
+ - 0
163
+ version: "0"
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ none: false
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ hash: 3
170
+ segments:
171
+ - 0
172
+ version: "0"
173
+ requirements: []
174
+
175
+ rubyforge_project:
176
+ rubygems_version: 1.3.7
177
+ signing_key:
178
+ specification_version: 3
179
+ summary: A base Sinatra application, with Cucumber and RSpec
180
+ test_files: []
181
+