bjork 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -6,6 +6,7 @@
6
6
  Gemfile.lock
7
7
  InstalledFiles
8
8
  _yardoc
9
+ build/
9
10
  coverage
10
11
  doc/
11
12
  lib/bundler/man
@@ -0,0 +1,44 @@
1
+ module Bjork
2
+
3
+ # The Bjork::TryStatic middleware delegates requests to Rack::Static middleware
4
+ # trying to match a static file
5
+ #
6
+ # Examples
7
+ #
8
+ # use Bjork::TryStatic,
9
+ # :root => "public", # static files root dir
10
+ # :urls => %w[/], # match all requests
11
+ # :try => ['.html', 'index.html', '/index.html'] # try these postfixes sequentially
12
+ #
13
+ # uses same options as Rack::Static with extra :try option which is an array
14
+ # of postfixes to find desired file
15
+
16
+ class TryStatic
17
+
18
+ def initialize(app, options)
19
+ @app = app
20
+ @try = ['', *options[:try]]
21
+ @static = ::Rack::Static.new(
22
+ lambda { |_| [404, {}, []] },
23
+ options
24
+ )
25
+ end
26
+
27
+ def call(env)
28
+ # TODO: Should we skip for all non-GET methods?
29
+ # Skip for POST
30
+ return @app.call(env) if env["REQUEST_METHOD"] == "POST"
31
+
32
+ orig_path = env['PATH_INFO']
33
+ found = nil
34
+
35
+ @try.each do |path|
36
+ resp = @static.call(env.merge!({'PATH_INFO' => orig_path + path}))
37
+ break if 404 != resp[0] && found = resp
38
+ end
39
+
40
+ found or @app.call(env.merge!('PATH_INFO' => orig_path))
41
+ end
42
+
43
+ end
44
+ end
data/lib/bjork/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Bjork
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/bjork.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "bjork/version"
2
+ require "bjork/try_static"
2
3
 
3
- require "sinatra"
4
+ require "sinatra/base"
4
5
 
5
6
  require 'coffee-script'
6
7
  require "haml"
@@ -15,29 +16,37 @@ Tilt::CoffeeScriptTemplate.default_bare = true
15
16
 
16
17
  module Bjork
17
18
  class Server < Sinatra::Base
18
- local_folder = File.expand_path(File.dirname(__FILE__))
19
+ enable :logging
20
+
21
+ # Serve any assets that exist in our folders
22
+ # Middlewares always take place before anything else,
23
+ # so if the file exists locally in the following folders
24
+ # it will be served.
25
+ # If it is not found the request will continue to the rest of the app
26
+ use Bjork::TryStatic, :urls => %w[/]
19
27
 
20
28
  asset_environment = Sprockets::Environment.new
21
- asset_environment.cache = Sprockets::Cache::FileStore.new("/tmp")
29
+ asset_environment.cache = Sprockets::Cache::FileStore.new("tmp")
22
30
 
23
- set :assets, asset_environment
31
+ server_folder = File.expand_path(File.dirname(__FILE__))
24
32
 
25
- # Configure sprockets
26
- settings.assets.append_path "#{local_folder}/javascripts"
27
- settings.assets.append_path "#{local_folder}/stylesheets"
33
+ # Internal (bjork server) Sprockets asset directories
34
+ %w[
35
+ javascripts
36
+ stylesheets
37
+ ].each do |path|
38
+ asset_environment.append_path File.join(server_folder, path)
39
+ end
28
40
 
29
- # External Sprockets Source directories
41
+ # External (host app which is running bjork) Sprockets asset directories
30
42
  %w[
31
- data
32
- images
33
- music
34
- sounds
43
+ lib
35
44
  source
36
45
  ].each do |path|
37
- settings.assets.append_path path
46
+ asset_environment.append_path path
38
47
  end
39
48
 
40
- set :public_folder, local_folder
49
+ set :assets, asset_environment
41
50
 
42
51
  set :haml, { :format => :html5 }
43
52
 
@@ -45,6 +54,21 @@ module Bjork
45
54
  haml :index
46
55
  end
47
56
 
57
+ # Any post to /save will write data to the path provided.
58
+ post '/save' do
59
+ data = params["data"]
60
+ path = params["path"]
61
+
62
+ # Ensure directory exists
63
+ FileUtils.mkdir_p File.dirname(path)
64
+
65
+ File.open(path, 'w') do |file|
66
+ file.write(data)
67
+ end
68
+
69
+ 200
70
+ end
71
+
48
72
  get '/debug_console' do
49
73
  haml :debug
50
74
  end
@@ -57,15 +81,22 @@ module Bjork
57
81
  end
58
82
  end
59
83
 
84
+ # Asset gems can use any of these four directories to
85
+ # serve things up.
86
+ # Try sprockets last
60
87
  %w[
61
88
  data
62
89
  images
63
90
  javascripts
64
- music
65
- sounds
66
91
  stylesheets
67
92
  ].each do |dir|
68
93
  get "/#{dir}/*.*" do
94
+ # Keep Sprockets environment's paths up to date with
95
+ # any gems that have been required after we've initialized
96
+ (Sprockets.paths - settings.assets.paths).each do |asset_path|
97
+ settings.assets.append_path asset_path
98
+ end
99
+
69
100
  path, extension = params[:splat]
70
101
 
71
102
  if asset = settings.assets["#{path}.#{extension}"]
@@ -76,17 +107,5 @@ module Bjork
76
107
  end
77
108
  end
78
109
  end
79
-
80
- # TODO: handle saving directory
81
- # post '/levels' do
82
- # data = params["data"]
83
- # name = params["name"]
84
-
85
- # File.open("levels/#{name}.json", 'w') do |file|
86
- # file.write(data)
87
- # end
88
-
89
- # 200
90
- # end
91
110
  end
92
111
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bjork
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-26 00:00:00.000000000 Z
12
+ date: 2013-05-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -155,6 +155,7 @@ files:
155
155
  - lib/bjork.rb
156
156
  - lib/bjork/source_maps.rb
157
157
  - lib/bjork/tasks.rb
158
+ - lib/bjork/try_static.rb
158
159
  - lib/bjork/version.rb
159
160
  - lib/stylesheets/all.css
160
161
  - lib/views/debug.haml