sliver 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0bdc908dcc8c917965d1acfd2b79b8f290917e7a
4
- data.tar.gz: cb6fcbcbe3f42041052410562e6d9d7cf68edbd9
3
+ metadata.gz: b4cafe27f4c73054d56b959d387c2ad112317a91
4
+ data.tar.gz: 7ef015ae4473548c4191b4d6bea1d7e5813a4f31
5
5
  SHA512:
6
- metadata.gz: bd4f2fb4cf61ee61a845ddeaa5c4e37ab737c152727aafc016a6379601e619a9cf802ba5fcc019987e3989d95f4e03d65ddacdf55a9a3d592ff9ea52dbaf2f80
7
- data.tar.gz: 4b93baa6b44dd3eadb0914bae0e10fba15687933642cbca7b36c5f25ae352c6835bda079667362b0adc409259e2546ca6a057a8bb24eb438e4f3296c014ad11f
6
+ metadata.gz: 6a58eb401484aa99d444765a5b1cf6c66d1b8fbd6bcafeff5dd16ff0b54b26018369467df73096bedccb2cc728e0b22aa5552e4f1a34b7b8962664b6f0debd1a
7
+ data.tar.gz: 9ff0a07cf83cd4e73dcac29a6706d7e9b4881aca903f3ff46ca463cb35136c031ad16f47b9f27f785bae3687d19eb6806a150e381539d3059fd4d2917c1467d5
data/README.md CHANGED
@@ -13,7 +13,7 @@ Early days of development, so things may change dramatically. Or not. Who knows.
13
13
  Add it to your Gemfile like any other gem, or install it manually.
14
14
 
15
15
  ```ruby
16
- gem 'sliver', '~> 0.1.0'
16
+ gem 'sliver', '~> 0.2.0'
17
17
  ```
18
18
 
19
19
  ## Usage
@@ -5,6 +5,7 @@ end
5
5
  require 'sliver/action'
6
6
  require 'sliver/api'
7
7
  require 'sliver/endpoints'
8
+ require 'sliver/hook'
8
9
  require 'sliver/path'
9
10
  require 'sliver/response'
10
11
  require 'sliver/runner'
@@ -11,6 +11,10 @@ module Sliver::Action
11
11
  def guards
12
12
  []
13
13
  end
14
+
15
+ def processors
16
+ []
17
+ end
14
18
  end
15
19
 
16
20
  def initialize(environment, response)
@@ -0,0 +1,9 @@
1
+ class Sliver::Hook
2
+ def initialize(action, response)
3
+ @action, @response = action, response
4
+ end
5
+
6
+ private
7
+
8
+ attr_reader :action, :response
9
+ end
@@ -1,15 +1,14 @@
1
1
  class Sliver::Runner
2
2
  def initialize(klass, environment)
3
3
  @klass, @environment = klass, environment
4
+
5
+ @guarded = false
4
6
  end
5
7
 
6
8
  def call
7
- guard_classes.each do |guard_class|
8
- guard = guard_class.new(action)
9
- return guard.response unless guard.continue?
10
- end
11
-
12
- action.call unless action.skip?
9
+ pass_guards
10
+ action.call unless skip_action?
11
+ post_process
13
12
 
14
13
  response.to_a
15
14
  end
@@ -26,7 +25,37 @@ class Sliver::Runner
26
25
  klass.guards
27
26
  end
28
27
 
28
+ def guarded?
29
+ @guarded
30
+ end
31
+
32
+ def guarded!
33
+ @guarded = true
34
+ end
35
+
36
+ def pass_guards
37
+ guard_classes.each do |guard_class|
38
+ guard = guard_class.new action, response
39
+ next if guard.continue?
40
+
41
+ guard.respond
42
+ guarded!
43
+ end
44
+ end
45
+
46
+ def post_process
47
+ processors.each { |processor| processor.call }
48
+ end
49
+
50
+ def processors
51
+ klass.processors.collect { |klass| klass.new action, response }
52
+ end
53
+
29
54
  def response
30
55
  @response ||= Sliver::Response.new
31
56
  end
57
+
58
+ def skip_action?
59
+ guarded? || action.skip?
60
+ end
32
61
  end
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  Gem::Specification.new do |spec|
3
3
  spec.name = 'sliver'
4
- spec.version = '0.1.0'
4
+ spec.version = '0.2.0'
5
5
  spec.authors = ['Pat Allan']
6
6
  spec.email = ['pat@freelancing-gods.com']
7
7
  spec.summary = %q{Lightweight, simple Rack APIs}
@@ -42,22 +42,15 @@ class SkippedAction
42
42
  end
43
43
  end
44
44
 
45
- class MyParamGuard
46
- def initialize(action)
47
- @action = action
48
- end
49
-
45
+ class MyParamGuard < Sliver::Hook
50
46
  def continue?
51
47
  action.request.params['hello'] == 'world'
52
48
  end
53
49
 
54
- def response
55
- [404, {}, ['Not Found']]
50
+ def respond
51
+ response.status = 404
52
+ response.body = ['Not Found']
56
53
  end
57
-
58
- private
59
-
60
- attr_reader :action
61
54
  end
62
55
 
63
56
  class GuardedAction
@@ -97,6 +90,25 @@ class MultiPathPartAction
97
90
  end
98
91
  end
99
92
 
93
+ class JsonProcessor < Sliver::Hook
94
+ def call
95
+ response.headers['Content-Type'] = 'application/json'
96
+ end
97
+ end
98
+
99
+ class ProcessedAction
100
+ include Sliver::Action
101
+
102
+ def self.processors
103
+ [JsonProcessor]
104
+ end
105
+
106
+ def call
107
+ response.status = 200
108
+ response.body = ['[]']
109
+ end
110
+ end
111
+
100
112
  describe 'Class-based Sliver API' do
101
113
  include Rack::Test::Methods
102
114
 
@@ -109,6 +121,7 @@ describe 'Class-based Sliver API' do
109
121
  api.connect :get, '/unguard', UnguardedAction
110
122
  api.connect :get, '/my/:id', IdAction
111
123
  api.connect :get, '/my/:first/:second', MultiPathPartAction
124
+ api.connect :get, '/processed', ProcessedAction
112
125
  end }
113
126
 
114
127
  it 'constructs responses' do
@@ -172,4 +185,11 @@ describe 'Class-based Sliver API' do
172
185
  expect(last_response.status).to eq(200)
173
186
  expect(last_response.body).to eq('10:foo')
174
187
  end
188
+
189
+ it 'handles processors' do
190
+ get '/processed'
191
+
192
+ expect(last_response.status).to eq(200)
193
+ expect(last_response.headers['Content-Type']).to eq('application/json')
194
+ end
175
195
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sliver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Allan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-16 00:00:00.000000000 Z
11
+ date: 2015-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -69,6 +69,7 @@ files:
69
69
  - lib/sliver/action.rb
70
70
  - lib/sliver/api.rb
71
71
  - lib/sliver/endpoints.rb
72
+ - lib/sliver/hook.rb
72
73
  - lib/sliver/path.rb
73
74
  - lib/sliver/response.rb
74
75
  - lib/sliver/runner.rb