rack-spec 0.0.4 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 982ed4f72ecd493a91d0c53fb4bf960b2850a303
4
- data.tar.gz: f012454fe35d8abf802a7c32fa138ddfedd21d50
3
+ metadata.gz: bf3a96b091fcf262c279bfd214379e55ed856c7a
4
+ data.tar.gz: 313b5fd8342e4ddd245fced70fad0d348db2092f
5
5
  SHA512:
6
- metadata.gz: 28e45afcf7f5d03cf4bc764f737e846012e8d1d5c63e7936eada092c117385d05e7152533bba5e3044fab6c6d1b3a808fc19802f511ff22aca8b3594bda21d13
7
- data.tar.gz: 5a8239586bf0f19227a2d1606bc4a220c4655864157d1afaa9ed7faa69fc1f4e18aa021380bab99c0f9e84a43ecb09866db484e560348e405d3bb94b5b65642b
6
+ metadata.gz: 2ef09a48686f6e9818fbd4e2c8aef235b8d45981a26f4703e54efe051deb7f057a57dee84ab6d93cb3fb41e327691dd717db083b41835ef50d3e5796170d6368
7
+ data.tar.gz: f625b179cae1770d3877b050492d3e93fa9b05e226a4f84ad359a5acaad62b176277cf0eb8b1afd862bd8c77b69bed2d866a2247b441e4eda909cd779a922f60
@@ -1,3 +1,6 @@
1
+ ## v0.0.5
2
+ * Change RESTful resource API (#get, #post, #put, and #delete)
3
+
1
4
  ## v0.0.4
2
5
  * Add Rack::Spec::Restful, strongly conventional RESTful API Provider
3
6
 
data/README.md CHANGED
@@ -1,28 +1,14 @@
1
1
  # Rack::Spec
2
- Define specifications of your Rack application.
2
+ Spec based web-application middleware for Rack.
3
3
 
4
- ## Installation
5
- ```
6
- gem install rack-spec
7
- ```
8
-
9
- ## Rack::Spec::Validation
10
- Rack::Spec::Validation is a rack-middleware and works as a validation layer for your rack-application.
11
- It loads spec definition (= a pure Hash object in specific format) to validate each request.
12
- If the request is not valid on your definition, it will raise Rack::Spec::Exceptions::ValidationError.
13
- Rack::Spec::ExceptionHandler is a utility rack-middleware to rescue validation error and return 400.
4
+ * Rack::Spec - all-in-one middleware
5
+ * Rack::Spec::Validation - validates requests along given specifications
6
+ * Rack::Spec::ExceptionHandler - rescues exceptions raised from Rack::Spec::Validation
7
+ * Rack::Spec::Restful - provides strongly-conventional RESTful API endpoints
14
8
 
15
9
  ```ruby
16
- require "rack"
17
- require "rack/spec"
18
- require "yaml"
19
-
20
- use Rack::Spec::ExceptionHandler
21
- use Rack::Spec::Validation, spec: YAML.load_file("spec.yml")
22
-
23
- run ->(env) do
24
- [200, {}, ["OK"]]
25
- end
10
+ use Rack::Spec, spec: YAML.load_file("spec.yml")
11
+ run ->(env) { [404, {}, ["Not Found"]] }
26
12
  ```
27
13
 
28
14
  ```yaml
@@ -59,7 +45,18 @@ endpoints:
59
45
  required: true
60
46
  ```
61
47
 
62
- ## Custom Validator
48
+ ## Rack::Spec::Validation
49
+ Rack::Spec::Validation is a rack-middleware and works as a validation layer for your rack-application.
50
+ It loads spec definition (= a pure Hash object in specific format) to validate each request.
51
+ If the request is not valid on your definition, it will raise Rack::Spec::Exceptions::ValidationError.
52
+ Rack::Spec::ExceptionHandler is a utility rack-middleware to rescue validation error and return 400.
53
+
54
+ ```ruby
55
+ use Rack::Spec::ExceptionHandler
56
+ use Rack::Spec::Validation, spec: YAML.load_file("spec.yml")
57
+ ```
58
+
59
+ ### Custom Validator
63
60
  Custom validator can be defined by inheriting Rack::Spec::Validators::Base.
64
61
  The following FwordValidator rejects any parameter starting with "F".
65
62
  See [lib/rack/spec/validators](https://github.com/r7kamura/rack-spec/tree/master/lib/rack/spec/validators) for more examples.
@@ -80,25 +77,30 @@ class FwordValidator < Rack::Spec::Validators::Base
80
77
  end
81
78
  ```
82
79
 
83
- ## Exception Handling
80
+ ### Exception Handling
84
81
  Replace Rack::Spec::ExceptionHandler to customize error behavior.
85
82
 
86
83
  ```ruby
87
- use MyExceptionHandler # Rack::Spec::ValidationError must be rescued
84
+ use MyExceptionHandler # Rack::Spec::Exceptions::ValidationError must be rescued
88
85
  use Rack::Spec::Validation, spec: YAML.load_file("spec.yml")
89
86
  ```
90
87
 
91
88
  ## Rack::Spec::Restful
92
- Rack::Spec::Restful provides strongly conventional RESTful APIs as a rack-middleware.
89
+ Rack::Spec::Restful provides strongly-conventional RESTful API endpoints as a rack-middleware.
90
+
91
+ ### Convention
93
92
  It recognizes a preferred instruction from the request method & path, then tries to call it.
94
93
 
95
- | verb | path | instruction |
96
- | ---- | ---- | ---- |
97
- | GET | /recipes/ | Recipe.index(id) |
98
- | GET | /recipes/{id} | Recipe.show(id, params) |
99
- | POST | /recipes/ | Recipe.create(id) |
100
- | PUT | /recipes/{id} | Recipe.update(id, params) |
101
- | DELETE | /recipes/{id} | Recipe.destroy(id, params) |
94
+ | verb | path | instruction |
95
+ | ---- | ---- | ---- |
96
+ | GET | /recipes | Recipe.get(params) |
97
+ | GET | /recipes/{id} | Recipe.get(params) |
98
+ | POST | /recipes | Recipe.post(params) |
99
+ | PUT | /recipes/{id} | Recipe.put(params) |
100
+ | DELETE | /recipes/{id} | Recipe.delete(params) |
101
+
102
+ ### Example
103
+ You must implement correspondent class & methods for your API.
102
104
 
103
105
  ```ruby
104
106
  class Recipe
@@ -110,6 +112,15 @@ class Recipe
110
112
  find(id)
111
113
  end
112
114
  end
115
+
116
+ require "rack"
117
+ require "rack/spec"
118
+ require "yaml"
119
+
120
+ use Rack::Spec::Restful, spec: YAML.load_file("spec.yml")
121
+ run ->(env) do
122
+ [404, {}, ["Not Found"]]
123
+ end
113
124
  ```
114
125
 
115
126
  ## Development
@@ -28,7 +28,7 @@ module Rack
28
28
 
29
29
  def call
30
30
  if endpoint
31
- response = handler_class.send(handler_method_name, *handler_args)
31
+ response = handler_class.send(handler_method_name, params)
32
32
  [
33
33
  response.try(:status) || default_status,
34
34
  default_header.merge(response.try(:header) || {}),
@@ -45,14 +45,6 @@ module Rack
45
45
  @endpoint ||= @spec.find_endpoint(@env)
46
46
  end
47
47
 
48
- def handler_args
49
- if id
50
- [id, params]
51
- else
52
- [params]
53
- end
54
- end
55
-
56
48
  def handler_class
57
49
  path_segments[1].singularize.camelize.constantize
58
50
  end
@@ -66,20 +58,7 @@ module Rack
66
58
  end
67
59
 
68
60
  def handler_method_name
69
- case request_method
70
- when "GET"
71
- if id
72
- :show
73
- else
74
- :index
75
- end
76
- when "POST"
77
- :create
78
- when "PUT"
79
- :update
80
- when "DELETE"
81
- :destroy
82
- end
61
+ request_method.downcase
83
62
  end
84
63
 
85
64
  def path
@@ -95,7 +74,7 @@ module Rack
95
74
  end
96
75
 
97
76
  def params
98
- request.params
77
+ request.params.merge(@env["rack-spec.uri_parameters"])
99
78
  end
100
79
 
101
80
  def default_status
@@ -11,8 +11,9 @@ module Rack
11
11
 
12
12
  def find_endpoint(env)
13
13
  self["endpoints"].find do |path, source|
14
- if Addressable::Template.new(path).extract(env["PATH_INFO"])
14
+ if parameters = Addressable::Template.new(path).extract(env["PATH_INFO"])
15
15
  if endpoint = source[env["REQUEST_METHOD"]]
16
+ env["rack-spec.uri_parameters"] = parameters
16
17
  break endpoint
17
18
  end
18
19
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class Spec
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Rack::Spec::VERSION
9
9
  spec.authors = ["Ryo Nakamura"]
10
10
  spec.email = ["r7kamura@gmail.com"]
11
- spec.summary = "Define specifications of your Rack application."
11
+ spec.summary = "Spec based web-application middleware for Rack."
12
12
 
13
13
  spec.homepage = "https://github.com/r7kamura/rack-spec"
14
14
  spec.license = "MIT"
@@ -8,24 +8,24 @@ describe Rack::Spec do
8
8
  "Recipe",
9
9
  Class.new do
10
10
  class << self
11
- def index(params)
12
- [
13
- { name: "test" }
14
- ]
11
+ def get(params)
12
+ if params["id"]
13
+ { name: "test#{params["id"]}"}
14
+ else
15
+ [
16
+ { name: "test" }
17
+ ]
18
+ end
15
19
  end
16
20
 
17
- def show(id, params)
18
- { name: "test#{id}" }
19
- end
20
-
21
- def create(params)
21
+ def post(params)
22
22
  { name: "test" }
23
23
  end
24
24
 
25
- def update(id, params)
25
+ def put(params)
26
26
  end
27
27
 
28
- def destroy(id, params)
28
+ def delete(params)
29
29
  end
30
30
  end
31
31
  end
@@ -177,7 +177,7 @@ describe Rack::Spec do
177
177
  end
178
178
 
179
179
  context "with GET /recipes/{id}" do
180
- it "calls Recipe.show(id, params)" do
180
+ it "calls Recipe.get(params)" do
181
181
  get "/recipes/1"
182
182
  response.status.should == 200
183
183
  response.body.should be_json_as(name: "test1")
@@ -189,7 +189,7 @@ describe Rack::Spec do
189
189
  params[:title] = "test"
190
190
  end
191
191
 
192
- it "calls Recipe.create(params)" do
192
+ it "calls Recipe.post(params)" do
193
193
  post "/recipes", params
194
194
  response.status.should == 201
195
195
  response.body.should be_json_as(name: "test")
@@ -197,14 +197,14 @@ describe Rack::Spec do
197
197
  end
198
198
 
199
199
  context "with PUT /recipes/{id}" do
200
- it "calls Recipe.update(id, params)" do
200
+ it "calls Recipe.put(params)" do
201
201
  put "/recipes/1"
202
202
  response.status.should == 204
203
203
  end
204
204
  end
205
205
 
206
206
  context "with DELETE /recipes/{id}" do
207
- it "calls Recipe.update(id, params)" do
207
+ it "calls Recipe.delete(params)" do
208
208
  delete "/recipes/1"
209
209
  response.status.should == 204
210
210
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-spec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -196,7 +196,7 @@ rubyforge_project:
196
196
  rubygems_version: 2.0.3
197
197
  signing_key:
198
198
  specification_version: 4
199
- summary: Define specifications of your Rack application.
199
+ summary: Spec based web-application middleware for Rack.
200
200
  test_files:
201
201
  - spec/fixtures/spec.yml
202
202
  - spec/rack/spec_spec.rb