fastr 0.0.1 → 0.0.2

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.
data/README.rdoc CHANGED
@@ -1,6 +1,85 @@
1
1
  = fastr
2
2
 
3
- Description goes here.
3
+ Micro web framework for Ruby. Should be used with an EventMachine rack server.
4
+
5
+ == Getting Started
6
+
7
+ $ sudo gem install fastr
8
+ $ fastr init helloworld
9
+ $ cd helloworld
10
+ $ thin -p 5000 start
11
+
12
+ == Directory Structure
13
+
14
+ The directory structure is similar to rails:
15
+
16
+ * app/(config/controller/views/models)
17
+ * lib
18
+ * test
19
+
20
+ == Routes
21
+
22
+ The routes are configured in app/config/routes.rb
23
+
24
+ router.draw do |route|
25
+ route.for '/:controller/:action'
26
+ #route.for '/home/:action', :action => '[A-Za-z]+'
27
+ #route.for '/test', :to => 'home#index'
28
+ end
29
+
30
+ == Controller
31
+
32
+ class HomeController < Fastr::Controller
33
+ def index
34
+ render(:text, "Hello, world!")
35
+ end
36
+ end
37
+
38
+ == Return a view in a controller
39
+
40
+ The return for a controller is just a rack response, i.e [200, {"Content-Type" => "text/plain"}, "Hello, World!"]
41
+
42
+ You can also use the following render methods:
43
+
44
+ render(:text, "My text")
45
+
46
+ With HAML, the template is rendered and any instance variables in your controller are available in the template.
47
+
48
+ render(:haml, :template => "index") # this searches for index.haml in your app/views/ folder
49
+
50
+ == Deferred Responses
51
+
52
+ fastr also lets you return a deferred response. This is useful if you want to chunk the response back to the client, or have a long running operation that you want to perform without blocking EventMachine.
53
+
54
+ The following is an example of a deferred response. It executes a sleep which normally would block EventMachine, but by using response.task, we tell EventMachine to run this code in its internal thread pool and when finished the callback is executed.
55
+
56
+ The following is an example of a controller action.
57
+
58
+ def long_running_task
59
+ defer_response(200, {"Content-Type" => "text/plain"}) do |response|
60
+ puts "in our deferred response...now we can do cool stuff!"
61
+ response.send_data("hey\n")
62
+
63
+ long_task = proc {
64
+ log.debug "Sleeping for 5 seconds...but this won't block other requests"
65
+ sleep(5)
66
+ log.debug "Finished sleeping, returning response to client."
67
+ return "finished"
68
+ }
69
+
70
+ callback = proc { |result|
71
+ log.debug "Callback result: #{result}"
72
+ response.send_data("#{result}\n")
73
+ response.succeed
74
+ }
75
+
76
+ response.task(long_task, callback)
77
+ end
78
+ end
79
+
80
+ == Current Status
81
+
82
+ Right now just the base is done. The controller supports a very basic render method.
4
83
 
5
84
  == Note on Patches/Pull Requests
6
85
 
data/bin/fastr CHANGED
@@ -2,11 +2,6 @@
2
2
 
3
3
  require 'fileutils'
4
4
 
5
- if ARGV.size == 0
6
- Fastr.usage
7
- exit(0)
8
- end
9
-
10
5
  module Fastr
11
6
  def self.init_app(app_name)
12
7
  if File.directory? app_name
@@ -63,6 +58,11 @@ module Fastr
63
58
  end
64
59
  end
65
60
 
61
+ if ARGV.size == 0
62
+ Fastr.usage
63
+ exit(0)
64
+ end
65
+
66
66
  command = ARGV[0]
67
67
 
68
68
  if command == 'init' and ARGV.length == 2
data/lib/fastr.rb CHANGED
@@ -7,4 +7,5 @@ module Fastr
7
7
  autoload :Error, "#{ROOT}/fastr/exception"
8
8
  autoload :Controller, "#{ROOT}/fastr/controller"
9
9
  autoload :Template, "#{ROOT}/fastr/template"
10
+ autoload :Deferrable, "#{ROOT}/fastr/deferrable"
10
11
  end
@@ -43,6 +43,7 @@ module Fastr
43
43
  log.info "Routing to controller: #{klass}, action: #{action}"
44
44
 
45
45
  obj = Module.const_get(klass).new
46
+ obj.env = env
46
47
 
47
48
  ret = obj.send(action)
48
49
 
@@ -1,6 +1,12 @@
1
1
  module Fastr
2
2
  class Controller
3
+ attr_accessor :env
4
+
3
5
  include Fastr::Template
6
+ include Fastr::Deferrable
4
7
 
8
+ def self.inherited(kls)
9
+ kls.instance_eval('include Fastr::Log')
10
+ end
5
11
  end
6
12
  end
@@ -0,0 +1,34 @@
1
+ module Fastr
2
+ module Deferrable
3
+ def defer_response(code, headers, &block)
4
+ response = DeferrableResponse.new
5
+
6
+ EM.next_tick do
7
+ env['async.callback'].call([code, headers, response])
8
+ block.call(response)
9
+ end
10
+
11
+ [-1, {}, []].freeze
12
+ end
13
+ end
14
+
15
+ class DeferrableResponse
16
+ include EventMachine::Deferrable
17
+
18
+ def send_data(data)
19
+ @callback.call(data)
20
+ end
21
+
22
+ def task(operation, callback)
23
+ EM.defer(operation, callback)
24
+ end
25
+
26
+ def finish
27
+ self.succeed
28
+ end
29
+
30
+ def each(&cb)
31
+ @callback = cb
32
+ end
33
+ end
34
+ end
data/lib/fastr/logger.rb CHANGED
@@ -1,16 +1,25 @@
1
1
  require 'logger'
2
2
 
3
3
  module Fastr
4
- module Log
4
+ module Log
5
5
  def self.included(kls)
6
- logger = Logger.new(STDOUT)
7
- logger.level = Logger::DEBUG
8
- logger.formatter = Fastr::Log::Formatter.new(kls)
9
- define_method :log do
10
- logger
6
+ kls.instance_eval do
7
+ @logger = Logger.new(STDOUT)
8
+ @logger.level = Logger::DEBUG
9
+ @logger.formatter = Fastr::Log::Formatter.new(kls)
10
+
11
+ def logger
12
+ @logger
13
+ end
14
+ end
15
+
16
+ kls.class_eval do
17
+ def log
18
+ self.class.logger
19
+ end
11
20
  end
12
21
  end
13
-
22
+
14
23
  class Formatter < Logger::Formatter
15
24
  attr_accessor :progname
16
25
 
@@ -36,9 +36,8 @@ module Fastr
36
36
  haml_engine = Haml::Engine.new(tpl_data)
37
37
  @@tpl_cache[tpl] = haml_engine
38
38
  end
39
-
40
-
41
- resp = haml_engine.render
39
+
40
+ resp = haml_engine.render(self)
42
41
 
43
42
  [200, {"Content-Type" => "text/html"}, resp]
44
43
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastr
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 27
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 0
8
- - 1
9
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
10
11
  platform: ruby
11
12
  authors:
12
13
  - Chris Moos
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-05-19 00:00:00 -07:00
18
+ date: 2010-06-08 00:00:00 -07:00
18
19
  default_executable: fastr
19
20
  dependencies: []
20
21
 
@@ -31,12 +32,16 @@ files:
31
32
  - lib/fastr.rb
32
33
  - lib/fastr/application.rb
33
34
  - lib/fastr/controller.rb
35
+ - lib/fastr/deferrable.rb
34
36
  - lib/fastr/exception.rb
35
37
  - lib/fastr/logger.rb
36
38
  - lib/fastr/router.rb
37
39
  - lib/fastr/template.rb
38
40
  - LICENSE
39
41
  - README.rdoc
42
+ - test/helper.rb
43
+ - test/test_fastr.rb
44
+ - bin/fastr
40
45
  has_rdoc: true
41
46
  homepage: http://github.com/chrismoos/fastr
42
47
  licenses: []
@@ -47,23 +52,27 @@ rdoc_options:
47
52
  require_paths:
48
53
  - lib
49
54
  required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
50
56
  requirements:
51
57
  - - ">="
52
58
  - !ruby/object:Gem::Version
59
+ hash: 3
53
60
  segments:
54
61
  - 0
55
62
  version: "0"
56
63
  required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
57
65
  requirements:
58
66
  - - ">="
59
67
  - !ruby/object:Gem::Version
68
+ hash: 3
60
69
  segments:
61
70
  - 0
62
71
  version: "0"
63
72
  requirements: []
64
73
 
65
74
  rubyforge_project:
66
- rubygems_version: 1.3.6
75
+ rubygems_version: 1.3.7
67
76
  signing_key:
68
77
  specification_version: 3
69
78
  summary: Another rack web framework for Ruby.