krack 0.0.3 → 0.1.0
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.md +31 -29
- data/lib/krack/endpoint.rb +9 -11
- data/lib/krack/router.rb +1 -6
- data/lib/krack/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -2,34 +2,36 @@
|
|
2
2
|
|
3
3
|
Simple JSON APIs on Rack. Like so:
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
run Krack::Router.new {
|
34
|
+
get "/widgets", Widgets::Index
|
35
|
+
get "/widgets/:id", Widgets::Show
|
36
|
+
}
|
37
|
+
```
|
data/lib/krack/endpoint.rb
CHANGED
@@ -2,25 +2,25 @@ module Krack
|
|
2
2
|
class Endpoint
|
3
3
|
attr_reader :env, :request, :response, :params
|
4
4
|
|
5
|
-
def
|
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
|
-
|
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
|
-
|
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
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
|
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-
|
12
|
+
date: 2013-03-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|