krack 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,34 +2,36 @@
2
2
 
3
3
  Simple JSON APIs on Rack. Like so:
4
4
 
5
- # config.ru
6
- require 'krack'
7
-
8
- class Widget # < "ORM"
9
- DB = {
10
- "1" => {name: "Foo", color: "Black"},
11
- "2" => {name: "Bar", color: "White"}
12
- }
13
- def self.all; DB end
14
- def self.find(id); DB[id] end
5
+ ```ruby
6
+ # config.ru
7
+ require 'krack'
8
+
9
+ class Widget # < "ORM"
10
+ DB = {
11
+ "1" => {name: "Foo", color: "Black"},
12
+ "2" => {name: "Bar", color: "White"}
13
+ }
14
+ def self.all; DB end
15
+ def self.find(id); DB[id] end
16
+ end
17
+
18
+ module Widgets
19
+ class Index < Krack::Endpoint
20
+ def respond
21
+ {widgets: Widget.all}
15
22
  end
16
-
17
- module Widgets
18
- class Index < Krack::Endpoint
19
- def respond
20
- {widgets: Widget.all}
21
- end
22
- end
23
-
24
- class Show < Krack::Endpoint
25
- def respond
26
- widget = Widget.find(params["id"]) or throw :halt, 404
27
- {widget: widget}
28
- end
29
- end
23
+ end
24
+
25
+ class Show < Krack::Endpoint
26
+ def respond
27
+ widget = Widget.find(params["id"]) or throw :halt, 404
28
+ {widget: widget}
30
29
  end
31
-
32
- run Krack::Router.new {
33
- get "/widgets", Widgets::Index
34
- get "/widgets/:id", Widgets::Show
35
- }
30
+ end
31
+ end
32
+
33
+ run Krack::Router.new {
34
+ get "/widgets", Widgets::Index
35
+ get "/widgets/:id", Widgets::Show
36
+ }
37
+ ```
@@ -2,25 +2,25 @@ module Krack
2
2
  class Endpoint
3
3
  attr_reader :env, :request, :response, :params
4
4
 
5
- def call(env)
6
- dup.call!(env)
7
- end
8
-
9
- def call!(env)
5
+ def initialize(env)
10
6
  @env = env
11
7
  @request = Rack::Request.new(env)
12
8
  @response = Rack::Response.new([], 200, {"Content-Type" => "application/json"})
13
9
  @params = env["krack.params"].merge(@request.params)
10
+ end
14
11
 
12
+ def self.call(env)
13
+ new(env).call!
14
+ end
15
+
16
+ def call!
15
17
  data = begin
16
- on_call
17
18
  respond_or_halt
18
19
  rescue
19
20
  on_error($!)
20
21
  end
21
-
22
- @response.write(output(data))
23
- @response.finish
22
+ response.write(output(data))
23
+ response.finish
24
24
  end
25
25
 
26
26
  def respond_or_halt
@@ -28,8 +28,6 @@ module Krack
28
28
  on_halt(halt)
29
29
  end
30
30
 
31
- def on_call; end
32
-
33
31
  def on_error(error)
34
32
  on_halt(500)
35
33
  end
data/lib/krack/router.rb CHANGED
@@ -36,12 +36,7 @@ module Krack
36
36
  # Allow optional trailing slash, add start/end tokens
37
37
  route = "\\A#{route}\\/?\\z"
38
38
 
39
- # App can be either a Rack class or an instance of such, e.g.
40
- # Endpoints::Deals::Near or lambda { |env| ... }
41
- # What goes into @routes is something that responds to #call
42
- app = to.respond_to?(:call) ? to : to.new
43
-
44
- @routes << [verb, Regexp.new(route), app]
39
+ @routes << [verb, Regexp.new(route), to]
45
40
  end
46
41
  end
47
42
  end
data/lib/krack/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Krack
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: krack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
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-02-26 00:00:00.000000000 Z
12
+ date: 2013-03-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack