camping 3.2.4 → 3.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +13 -16
- data/Rakefile +33 -15
- data/book/01_introduction.md +2 -3
- data/book/02_getting_started.md +39 -77
- data/lib/camping/commands.rb +1 -1
- data/lib/camping/loader.rb +9 -6
- data/lib/camping/loads.rb +5 -5
- data/lib/camping/sequel.rb +31 -0
- data/lib/camping/server.rb +5 -1
- data/lib/camping/version.rb +1 -1
- data/lib/camping-unabridged.rb +1 -0
- data/lib/camping.rb +1 -0
- data/test/app_loader.rb +14 -1
- data/test/config_reloader.rb +3 -3
- data/test/integration/Gemfile +1 -1
- data/test/integration/Rakefile +0 -27
- data/test/integration/kindling/starty.rb +7 -0
- data/test/reload_reloader.rb +7 -10
- data/test/server/helper.rb +13 -0
- data/test/server/spec_server.rb +103 -0
- data/test/test_helper.rb +8 -24
- metadata +67 -8
- data/lib/campingtrip.md +0 -341
- /data/lib/{camping/gear → gear}/filters.rb +0 -0
- /data/lib/{camping/gear → gear}/firewatch.rb +0 -0
- /data/lib/{camping/gear → gear}/inspection.rb +0 -0
- /data/lib/{camping/gear → gear}/kuddly.rb +0 -0
- /data/lib/{camping/gear → gear}/nancy.rb +0 -0
data/lib/campingtrip.md
DELETED
|
@@ -1,341 +0,0 @@
|
|
|
1
|
-
# How does Camping work?
|
|
2
|
-
This is an academic document written to help people, but mostly me, understand what Camping is doing and in what sequence. Why? Because I want to make camping better, but how do you make it better unless you understand what you've got?
|
|
3
|
-
|
|
4
|
-
# Start
|
|
5
|
-
Camping starts off with some simple code you type: `camping nuts.rb` into your terminal and the camping gem is loaded and executed. This assumes that you've installed camping via ruby gems: `gem install camping`. Gems are then given a binary command you can use on the command line, in our case **camping**. The camping command accepts a file as an argument and maybe some options. This command calls a *binary* that's found the camping gem, this is what it looks like:
|
|
6
|
-
```ruby
|
|
7
|
-
#!/usr/bin/env ruby
|
|
8
|
-
|
|
9
|
-
$:.unshift File.dirname(__FILE__) + "/../lib"
|
|
10
|
-
|
|
11
|
-
require 'camping'
|
|
12
|
-
require 'camping/server'
|
|
13
|
-
|
|
14
|
-
begin
|
|
15
|
-
Camping::Server.start
|
|
16
|
-
rescue OptionParser::ParseError => ex
|
|
17
|
-
STDERR.puts "!! #{ex.message}"
|
|
18
|
-
puts "** use `#{File.basename($0)} --help` for more details..."
|
|
19
|
-
exit 1
|
|
20
|
-
end
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
First wee see `$:.unshift File.dirname(__FILE__) + "/../lib"`, `$:`, is a global variable, that contains the loadpath for scripts. Every ruby file is a script, you may be more familiar with `$LOAD_PATH` which is an alias for the `$:` global. Next `unshift` is an array method that prepends an item to the beginning of an array. `File` is a builtin ruby class that lets you work with files. `File.dirname(file)` returns a string of the complete file path of the file given, except for the file's name. In our case we're using `__FILE__` to return the current file name, which is the binary file in the camping gem. the last portion: `+ "/../lib"` appends the string to the directory path that we just got. All of this is done to ensure that the `lib` folder where all of camping's code resides is added to the script load path.
|
|
24
|
-
|
|
25
|
-
Next we require camping:
|
|
26
|
-
```ruby
|
|
27
|
-
require 'camping'
|
|
28
|
-
require 'camping/server'
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
Which loads camping and it's server code in to the current script context.
|
|
32
|
-
|
|
33
|
-
```ruby
|
|
34
|
-
Camping::Server.start
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
The above code finally Starts the camping server. So let's take a look at the server:
|
|
38
|
-
|
|
39
|
-
```ruby
|
|
40
|
-
require 'irb'
|
|
41
|
-
require 'erb'
|
|
42
|
-
require 'rack'
|
|
43
|
-
require 'camping/reloader'
|
|
44
|
-
require 'camping/commands'
|
|
45
|
-
|
|
46
|
-
# == The Camping Server (for development)
|
|
47
|
-
#
|
|
48
|
-
# Camping includes a pretty nifty server which is built for development.
|
|
49
|
-
# It follows these rules:
|
|
50
|
-
#
|
|
51
|
-
# * Load all Camping apps in a file.
|
|
52
|
-
# * Mount those apps according to their name. (e.g. Blog is mounted at /blog.)
|
|
53
|
-
# * Run each app's <tt>create</tt> method upon startup.
|
|
54
|
-
# * Reload the app if its modification time changes.
|
|
55
|
-
# * Reload the app if it requires any files under the same directory and one
|
|
56
|
-
# of their modification times changes.
|
|
57
|
-
# * Support the X-Sendfile header.
|
|
58
|
-
#
|
|
59
|
-
# Run it like this:
|
|
60
|
-
#
|
|
61
|
-
# camping blog.rb # Mounts Blog at /
|
|
62
|
-
#
|
|
63
|
-
# And visit http://localhost:3301/ in your browser.
|
|
64
|
-
module Camping
|
|
65
|
-
class Server < Rack::Server
|
|
66
|
-
class Options
|
|
67
|
-
if home = ENV['HOME'] # POSIX
|
|
68
|
-
DB = File.join(home, '.camping.db')
|
|
69
|
-
RC = File.join(home, '.campingrc')
|
|
70
|
-
elsif home = ENV['APPDATA'] # MSWIN
|
|
71
|
-
DB = File.join(home, 'Camping.db')
|
|
72
|
-
RC = File.join(home, 'Campingrc')
|
|
73
|
-
else
|
|
74
|
-
DB = nil
|
|
75
|
-
RC = nil
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
HOME = File.expand_path(home) + '/'
|
|
79
|
-
|
|
80
|
-
def parse!(args)
|
|
81
|
-
args = args.dup
|
|
82
|
-
|
|
83
|
-
options = {}
|
|
84
|
-
|
|
85
|
-
opt_parser = OptionParser.new("", 24, ' ') do |opts|
|
|
86
|
-
opts.banner = "Usage: camping my-camping-app.rb"
|
|
87
|
-
opts.define_head "#{File.basename($0)}, the microframework ON-button for ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
|
|
88
|
-
opts.separator ""
|
|
89
|
-
opts.separator "Specific options:"
|
|
90
|
-
|
|
91
|
-
opts.on("-h", "--host HOSTNAME",
|
|
92
|
-
"Host for web server to bind to (default is all IPs)") { |v| options[:Host] = v }
|
|
93
|
-
|
|
94
|
-
opts.on("-p", "--port NUM",
|
|
95
|
-
"Port for web server (defaults to 3301)") { |v| options[:Port] = v }
|
|
96
|
-
|
|
97
|
-
db = DB.sub(HOME, '~/') if DB
|
|
98
|
-
opts.on("-d", "--database FILE",
|
|
99
|
-
"SQLite3 database path (defaults to #{db ? db : '<none>'})") { |db_path| options[:database] = db_path }
|
|
100
|
-
|
|
101
|
-
opts.on("-C", "--console",
|
|
102
|
-
"Run in console mode with IRB") { options[:server] = "console" }
|
|
103
|
-
|
|
104
|
-
server_list = ["thin", "webrick", "console"]
|
|
105
|
-
opts.on("-s", "--server NAME",
|
|
106
|
-
"Server to force (#{server_list.join(', ')})") { |v| options[:server] = v }
|
|
107
|
-
|
|
108
|
-
opts.separator ""
|
|
109
|
-
opts.separator "Common options:"
|
|
110
|
-
|
|
111
|
-
# No argument, shows at tail. This will print an options summary.
|
|
112
|
-
# Try it and see!
|
|
113
|
-
opts.on("-?", "--help", "Show this message") do
|
|
114
|
-
puts opts
|
|
115
|
-
exit
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
# Another typical switch to print the version.
|
|
119
|
-
opts.on("-m", "--mounting", "Shows Mounting Guide") do
|
|
120
|
-
puts "Mounting Guide"
|
|
121
|
-
puts ""
|
|
122
|
-
puts "To mount your horse, hop up on the side and put it."
|
|
123
|
-
exit
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
# Another typical switch to print the version.
|
|
127
|
-
opts.on("-v", "--version", "Show version") do
|
|
128
|
-
puts Gem.loaded_specs['camping'].version
|
|
129
|
-
exit
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
opt_parser.parse!(args)
|
|
135
|
-
|
|
136
|
-
# If no Arguments were called.
|
|
137
|
-
if args.empty?
|
|
138
|
-
args << "cabin.rb" # adds cabin.rb as a default camping entrance file
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
# Parses the first argument as the script to load into the server.
|
|
142
|
-
options[:script] = args.shift
|
|
143
|
-
options
|
|
144
|
-
end
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
def initialize(*)
|
|
148
|
-
super
|
|
149
|
-
@reloader = Camping::Reloader.new(options[:script]) do |app|
|
|
150
|
-
if !app.options.has_key?(:dynamic_templates)
|
|
151
|
-
app.options[:dynamic_templates] = true
|
|
152
|
-
end
|
|
153
|
-
|
|
154
|
-
if !Camping::Models.autoload?(:Base) && options[:database]
|
|
155
|
-
Camping::Models::Base.establish_connection(
|
|
156
|
-
:adapter => 'sqlite3',
|
|
157
|
-
:database => options[:database]
|
|
158
|
-
)
|
|
159
|
-
end
|
|
160
|
-
end
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
def opt_parser
|
|
164
|
-
Options.new
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
def default_options
|
|
168
|
-
super.merge({
|
|
169
|
-
:Port => 3301,
|
|
170
|
-
:database => Options::DB
|
|
171
|
-
})
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
def middleware
|
|
175
|
-
h = super
|
|
176
|
-
h["development"] << [XSendfile]
|
|
177
|
-
h
|
|
178
|
-
end
|
|
179
|
-
|
|
180
|
-
def start
|
|
181
|
-
if options[:server] == "console"
|
|
182
|
-
puts "** Starting console"
|
|
183
|
-
@reloader.reload!
|
|
184
|
-
r = @reloader
|
|
185
|
-
eval("self", TOPLEVEL_BINDING).meta_def(:reload!) { r.reload!; nil }
|
|
186
|
-
ARGV.clear
|
|
187
|
-
IRB.start
|
|
188
|
-
exit
|
|
189
|
-
else
|
|
190
|
-
name = server.name[/\w+$/]
|
|
191
|
-
puts "** Starting #{name} on #{options[:Host]}:#{options[:Port]}"
|
|
192
|
-
super
|
|
193
|
-
end
|
|
194
|
-
end
|
|
195
|
-
|
|
196
|
-
# defines the public directory to be /public
|
|
197
|
-
def public_dir
|
|
198
|
-
File.expand_path('../public', @reloader.file)
|
|
199
|
-
end
|
|
200
|
-
|
|
201
|
-
# add the public directory as a Rack app serving files first, then the
|
|
202
|
-
# current value of self, which is our camping apps, as an app.
|
|
203
|
-
def app
|
|
204
|
-
Rack::Cascade.new([Rack::Files.new(public_dir), self], [405, 404, 403])
|
|
205
|
-
end
|
|
206
|
-
|
|
207
|
-
# path_matches?
|
|
208
|
-
# accepts a regular expression string
|
|
209
|
-
# in our case our apps and controllers
|
|
210
|
-
def path_matches?(path, *reg)
|
|
211
|
-
reg.each do |r|
|
|
212
|
-
return true if Regexp.new(r).match? path
|
|
213
|
-
end
|
|
214
|
-
false
|
|
215
|
-
end
|
|
216
|
-
|
|
217
|
-
# call(env) res
|
|
218
|
-
# == How routing works
|
|
219
|
-
#
|
|
220
|
-
# The first app added using Camping.goes is set at the root, we walk through
|
|
221
|
-
# the defined routes of the first app to see if there is a match.
|
|
222
|
-
# With no match we then walk through every other defined app.
|
|
223
|
-
# Each subsequent app defined is loaded at a directory named after them:
|
|
224
|
-
#
|
|
225
|
-
# camping.goes :Nuts # Mounts Nuts at /
|
|
226
|
-
# camping.goes :Auth # Mounts Auth at /auth/
|
|
227
|
-
# camping.goes :Blog # Mounts Blog at /blog/
|
|
228
|
-
#
|
|
229
|
-
def call(env)
|
|
230
|
-
@reloader.reload
|
|
231
|
-
apps = @reloader.apps
|
|
232
|
-
|
|
233
|
-
# our switch statement iterates through possible app outcomes, no apps
|
|
234
|
-
# loaded, one app loaded, or multiple apps loaded.
|
|
235
|
-
case apps.length
|
|
236
|
-
when 0
|
|
237
|
-
[200, {'Content-Type' => 'text/html'}, ["I'm sorry but no apps were found."]]
|
|
238
|
-
when 1
|
|
239
|
-
apps.values.first.call(env) # When we have one
|
|
240
|
-
else
|
|
241
|
-
# 2 and up get special treatment
|
|
242
|
-
count = 0
|
|
243
|
-
apps.each do |name, app|
|
|
244
|
-
if count == 0
|
|
245
|
-
app.routes.each do |r|
|
|
246
|
-
if (path_matches?(env['PATH_INFO'], r))
|
|
247
|
-
next
|
|
248
|
-
end
|
|
249
|
-
return app.call(env) unless !(path_matches?(env['PATH_INFO'], r))
|
|
250
|
-
end
|
|
251
|
-
else
|
|
252
|
-
mount = name.to_s.downcase
|
|
253
|
-
case env["PATH_INFO"]
|
|
254
|
-
when %r{^/#{mount}}
|
|
255
|
-
env["SCRIPT_NAME"] = env["SCRIPT_NAME"] + $&
|
|
256
|
-
env["PATH_INFO"] = $'
|
|
257
|
-
return app.call(env)
|
|
258
|
-
when %r{^/code/#{mount}}
|
|
259
|
-
return [200, {'Content-Type' => 'text/plain', 'X-Sendfile' => @reloader.file}, []]
|
|
260
|
-
end
|
|
261
|
-
end
|
|
262
|
-
count += 1
|
|
263
|
-
end
|
|
264
|
-
|
|
265
|
-
# Just return the first app if we didn't find a match.
|
|
266
|
-
return apps.values.first.call(env)
|
|
267
|
-
end
|
|
268
|
-
end
|
|
269
|
-
|
|
270
|
-
class XSendfile
|
|
271
|
-
def initialize(app)
|
|
272
|
-
@app = app
|
|
273
|
-
end
|
|
274
|
-
|
|
275
|
-
def call(env)
|
|
276
|
-
status, headers, body = @app.call(env)
|
|
277
|
-
|
|
278
|
-
if key = headers.keys.grep(/c-sendfile/i).first
|
|
279
|
-
filename = headers[key]
|
|
280
|
-
content = open(filename,'rb') { | io | io.read}
|
|
281
|
-
headers['content-length'] = size(content).to_s
|
|
282
|
-
body = [content]
|
|
283
|
-
end
|
|
284
|
-
|
|
285
|
-
return status, headers, body
|
|
286
|
-
end
|
|
287
|
-
|
|
288
|
-
if "".respond_to?(:bytesize)
|
|
289
|
-
def size(str)
|
|
290
|
-
str.bytesize
|
|
291
|
-
end
|
|
292
|
-
else
|
|
293
|
-
def size(str)
|
|
294
|
-
str.size
|
|
295
|
-
end
|
|
296
|
-
end
|
|
297
|
-
end
|
|
298
|
-
end
|
|
299
|
-
end
|
|
300
|
-
```
|
|
301
|
-
|
|
302
|
-
The beginning of Server loads the required code to get Camping started, and then opens the `Camping` module:
|
|
303
|
-
|
|
304
|
-
```ruby
|
|
305
|
-
module Camping
|
|
306
|
-
class Server < Rack::Server
|
|
307
|
-
end
|
|
308
|
-
end
|
|
309
|
-
```
|
|
310
|
-
|
|
311
|
-
`Server` inherits from `Rack::Server`. Camping is Rack based to give ourselves a predictable interface for our web server code. Consequentally a lot of utilities useful for webservers are just baked into Rack, It gives Camping the chance to do what it does best, magic!
|
|
312
|
-
|
|
313
|
-
The first class declared in `Server` is called `Options`. It's in charge of parsing the command line options supplied to camping, and then supplying those options as a hash for the program further down the line. This class is declared inside of the `Server` class so that we can encapsulate the behaviour of options within the server. Which is pretty nice. Next is initialize:
|
|
314
|
-
|
|
315
|
-
```ruby
|
|
316
|
-
def initialize(*)
|
|
317
|
-
super
|
|
318
|
-
@reloader = Camping::Reloader.new(options[:script]) do |app|
|
|
319
|
-
if !app.options.has_key?(:dynamic_templates)
|
|
320
|
-
app.options[:dynamic_templates] = true
|
|
321
|
-
end
|
|
322
|
-
|
|
323
|
-
if !Camping::Models.autoload?(:Base) && options[:database]
|
|
324
|
-
Camping::Models::Base.establish_connection(
|
|
325
|
-
:adapter => 'sqlite3',
|
|
326
|
-
:database => options[:database]
|
|
327
|
-
)
|
|
328
|
-
end
|
|
329
|
-
end
|
|
330
|
-
end
|
|
331
|
-
```
|
|
332
|
-
|
|
333
|
-
`initialize` is the method called whenever you instantiate a new object of a class. You'll notice that the line of code in the method is a call to `super`. Because `Camping::Server` is a subclass of `Rack::Server`, and to get things rolling we first call `Rack::Server`'s initialize. Afterwards we setup the reloader, and optionally include the database.
|
|
334
|
-
|
|
335
|
-
When you call super naked like that it passes along whatever arguments were sent to the method that called super. In our case It's a splat: `initialize(*)`, so everything is sent along.
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
Remember earlier from the Camping binary where we start the server? : `Camping::Server.start`, You may notice that this is a call to a class method `start`, but we don't declare any class methods in `Camping::Server` only instance methods.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|