sliver 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.
- checksums.yaml +4 -4
- data/README.md +6 -2
- data/lib/sliver/action.rb +4 -0
- data/lib/sliver/api.rb +6 -5
- data/lib/sliver/endpoints.rb +21 -0
- data/lib/sliver.rb +1 -0
- data/sliver.gemspec +1 -1
- data/spec/acceptance/class_api_spec.rb +18 -2
- data/spec/acceptance/lambda_api_spec.rb +11 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 20b809608f86609cc795b28a114f3c812dec65e8
|
4
|
+
data.tar.gz: 4bf292bffbc5d19991d0fa7e29d2ced1005b6732
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f33a1f81c5222b877ef334d98f386abc7d1c64bc47d8e97587e533ebfcaefaf7ae0ebb774434ac305b8049d624522121a08c154eb893cb8de13432b3fd7cc96
|
7
|
+
data.tar.gz: c2496802b022d43a351ef992c38188f61f2f170e8ad52abe1c4f3e562a6012c332f2521fd76c29085ea418f884ce2f449cd4d1b22827b262d1c877cc521cad53
|
data/README.md
CHANGED
@@ -2,12 +2,16 @@
|
|
2
2
|
|
3
3
|
A super simple, extendable Rack API.
|
4
4
|
|
5
|
+
[](https://travis-ci.org/pat/sliver)
|
6
|
+
|
7
|
+
Early days of development, so things may change dramatically. Or not. Who knows.
|
8
|
+
|
5
9
|
## Installation
|
6
10
|
|
7
11
|
Add it to your Gemfile like any other gem, or install it manually.
|
8
12
|
|
9
13
|
```ruby
|
10
|
-
gem 'sliver', '~> 0.0.
|
14
|
+
gem 'sliver', '~> 0.0.2'
|
11
15
|
```
|
12
16
|
|
13
17
|
## Usage
|
@@ -52,7 +56,7 @@ class ChangeAction
|
|
52
56
|
response.body = [
|
53
57
|
"How dare the Premier ignore my invitations?",
|
54
58
|
"He'll have to go",
|
55
|
-
"So
|
59
|
+
"So too the bunch he luncheons with",
|
56
60
|
"It's second on my list of things to do"
|
57
61
|
]
|
58
62
|
|
data/lib/sliver/action.rb
CHANGED
data/lib/sliver/api.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
class Sliver::API
|
2
|
+
NOT_FOUND = lambda { |environment| [404, {}, ['Not Found']] }
|
3
|
+
|
2
4
|
attr_accessor :path
|
3
5
|
|
4
6
|
def initialize(&block)
|
5
|
-
@endpoints = {}
|
7
|
+
@endpoints = Hash.new { |hash, key| hash[key] = Sliver::Endpoints.new }
|
6
8
|
@path = ''
|
7
9
|
|
8
10
|
block.call self
|
@@ -11,16 +13,15 @@ class Sliver::API
|
|
11
13
|
def call(environment)
|
12
14
|
method = environment['REQUEST_METHOD']
|
13
15
|
path_info = environment['PATH_INFO'].gsub(/\A#{path}/, '')
|
14
|
-
endpoint = endpoints[method]
|
16
|
+
endpoint = endpoints[method].find(path_info) || NOT_FOUND
|
15
17
|
|
16
|
-
endpoint
|
18
|
+
endpoint.call environment
|
17
19
|
end
|
18
20
|
|
19
21
|
def connect(method, path, action)
|
20
22
|
method = method.to_s.upcase
|
21
23
|
|
22
|
-
endpoints[method]
|
23
|
-
endpoints[method][path] = action
|
24
|
+
endpoints[method].append path, action
|
24
25
|
end
|
25
26
|
|
26
27
|
private
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Sliver::Endpoints
|
2
|
+
def initialize
|
3
|
+
@paths = {}
|
4
|
+
end
|
5
|
+
|
6
|
+
def append(path, action)
|
7
|
+
paths[path] = action
|
8
|
+
end
|
9
|
+
|
10
|
+
def find(path)
|
11
|
+
key = paths.keys.detect { |key|
|
12
|
+
key.is_a?(String) ? (key == path) : path[/\A#{key}\z/]
|
13
|
+
}
|
14
|
+
|
15
|
+
key && paths[key]
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
attr_reader :paths
|
21
|
+
end
|
data/lib/sliver.rb
CHANGED
data/sliver.gemspec
CHANGED
@@ -19,12 +19,22 @@ class EchoAction
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
+
class AdditionAction
|
23
|
+
include Sliver::Action
|
24
|
+
|
25
|
+
def call
|
26
|
+
response.status = 200
|
27
|
+
response.body = [(request.params['a'].to_i + request.params['b'].to_i)]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
22
31
|
describe 'Class-based Sliver API' do
|
23
32
|
include Rack::Test::Methods
|
24
33
|
|
25
34
|
let(:app) { Sliver::API.new do |api|
|
26
|
-
api.connect :get, '/',
|
27
|
-
api.connect :put, '/echo',
|
35
|
+
api.connect :get, '/', GetAction
|
36
|
+
api.connect :put, '/echo', EchoAction
|
37
|
+
api.connect :get, '/addition', AdditionAction
|
28
38
|
end }
|
29
39
|
|
30
40
|
it 'constructs responses' do
|
@@ -40,4 +50,10 @@ describe 'Class-based Sliver API' do
|
|
40
50
|
|
41
51
|
expect(last_response.body).to eq('baz')
|
42
52
|
end
|
53
|
+
|
54
|
+
it 'allows use of request' do
|
55
|
+
get '/addition', 'a' => '5', 'b' => '3'
|
56
|
+
|
57
|
+
expect(last_response.body).to eq('8')
|
58
|
+
end
|
43
59
|
end
|
@@ -15,6 +15,10 @@ describe 'Basic Sliver API' do
|
|
15
15
|
api.connect :post, '/', lambda { |environment|
|
16
16
|
[200, {'Content-Type' => 'text/plain'}, ['qux']]
|
17
17
|
}
|
18
|
+
|
19
|
+
api.connect :delete, %r{/remove/\d+}, lambda { |environment|
|
20
|
+
[200, {}, ['removed']]
|
21
|
+
}
|
18
22
|
end }
|
19
23
|
|
20
24
|
it 'responds to GET requests' do
|
@@ -43,6 +47,13 @@ describe 'Basic Sliver API' do
|
|
43
47
|
expect(last_response.status).to eq(404)
|
44
48
|
expect(last_response.body).to eq('Not Found')
|
45
49
|
end
|
50
|
+
|
51
|
+
it 'matches against regular expressions' do
|
52
|
+
delete '/remove/141'
|
53
|
+
|
54
|
+
expect(last_response.status).to eq(200)
|
55
|
+
expect(last_response.body).to eq('removed')
|
56
|
+
end
|
46
57
|
end
|
47
58
|
|
48
59
|
describe 'Basic lambda API with a path prefix' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sliver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pat Allan
|
@@ -68,6 +68,7 @@ files:
|
|
68
68
|
- lib/sliver.rb
|
69
69
|
- lib/sliver/action.rb
|
70
70
|
- lib/sliver/api.rb
|
71
|
+
- lib/sliver/endpoints.rb
|
71
72
|
- lib/sliver/response.rb
|
72
73
|
- sliver.gemspec
|
73
74
|
- spec/acceptance/class_api_spec.rb
|