rack-spec 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 09c2e44eccf5df03af4ba733f212863feddc17f2
4
+ data.tar.gz: c20330aae4f67a16a78157d68c5ef7f7eb9b5067
5
+ SHA512:
6
+ metadata.gz: a1818d010e69c5bfac6dbea17970d6a5f9f117322de69d888695e24fdcf767c029320333d7c5203cdf758e2a088c1384e936c3e7cc98897dff9182356c9f08b2
7
+ data.tar.gz: 77571272b0bfb76540da3507d88f8dbc89ff13d49f4d8562faa61e4d37e829e3fd30421101d8ad4b9cf1e414861af2702532a0b185c8d009ec4e14f32cf1cec3
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-spec.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ryo Nakamura
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,52 @@
1
+ # Rack::Spec
2
+ Define specifications of your Rack application.
3
+
4
+ ## Installation
5
+ ```
6
+ gem install rack-spec
7
+ ```
8
+
9
+ ## Usage
10
+ ```ruby
11
+ require "rack"
12
+ require "rack/spec"
13
+ require "yaml"
14
+
15
+ use Rack::Spec, spec: YAML.load("spec.yml")
16
+
17
+ run ->(env) do
18
+ [200, {}, ["OK"]]
19
+ end
20
+ ```
21
+
22
+ ```yaml
23
+ # spec.yml
24
+ meta:
25
+ baseUri: http://api.example.com/
26
+
27
+ endpoints:
28
+ /recipes:
29
+ GET:
30
+ queryParameters:
31
+ page:
32
+ type: integer
33
+ minimum: 1
34
+ maximum: 10
35
+ private:
36
+ type: boolean
37
+ rank:
38
+ type: float
39
+ time:
40
+ type: iso8601
41
+ ```
42
+
43
+ ## Development
44
+ ```sh
45
+ # setup
46
+ git clone git@github.com:r7kamura/rack-spec.git
47
+ cd rack-spec
48
+ bundle install
49
+
50
+ # testing
51
+ bundle exec rspec
52
+ ```
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ require "rack/spec"
@@ -0,0 +1,29 @@
1
+ require "rack/builder"
2
+ require "rack/spec/exception_handler"
3
+ require "rack/spec/spec"
4
+ require "rack/spec/validation"
5
+ require "rack/spec/validation_error"
6
+ require "rack/spec/validators/base"
7
+ require "rack/spec/validators/maximum_validator"
8
+ require "rack/spec/validators/minimum_validator"
9
+ require "rack/spec/validators/null_validator"
10
+ require "rack/spec/validators/query_parameters_validator"
11
+ require "rack/spec/validators/type_validator"
12
+ require "rack/spec/validator_factory"
13
+ require "rack/spec/version"
14
+
15
+ module Rack
16
+ class Spec
17
+ def initialize(app, options)
18
+ @app = Rack::Builder.app do
19
+ use Rack::Spec::ExceptionHandler
20
+ use Rack::Spec::Validation, options
21
+ run app
22
+ end
23
+ end
24
+
25
+ def call(env)
26
+ @app.call(env)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ module Rack
2
+ class Spec
3
+ class ExceptionHandler
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ @app.call(env)
10
+ rescue ValidationError => exception
11
+ exception.to_rack_response
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ module Rack
2
+ class Spec
3
+ class Spec < Hash
4
+ def initialize(hash)
5
+ hash.each do |key, value|
6
+ self[key] = value
7
+ end
8
+ end
9
+
10
+ def reach(*keys)
11
+ keys.inject(self) do |hash, key|
12
+ hash[key] if hash.respond_to?(:[])
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ module Rack
2
+ class Spec
3
+ class Validation
4
+ def initialize(app, options = {})
5
+ @app = app
6
+ @options = options
7
+ end
8
+
9
+ def call(env)
10
+ query_parameters_validator.validate!(env)
11
+ @app.call(env)
12
+ end
13
+
14
+ private
15
+
16
+ def spec
17
+ Spec.new(@options[:spec])
18
+ end
19
+
20
+ def query_parameters_validator
21
+ @query_parameters_validator ||= Validators::QueryParametersValidator.new(spec)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,33 @@
1
+ require "json"
2
+
3
+ module Rack
4
+ class Spec
5
+ class ValidationError < StandardError
6
+ def initialize(message)
7
+ @message = message
8
+ end
9
+
10
+ def message
11
+ @message
12
+ end
13
+
14
+ def to_rack_response
15
+ [status, header, body]
16
+ end
17
+
18
+ private
19
+
20
+ def status
21
+ 400
22
+ end
23
+
24
+ def header
25
+ { "Content-Type" => "application/json" }
26
+ end
27
+
28
+ def body
29
+ { message: @message }.to_json
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ module Rack
2
+ class Spec
3
+ class ValidatorFactory
4
+ class << self
5
+ def validator_classes
6
+ @validator_classes ||= Hash.new(Validators::NullValidator)
7
+ end
8
+
9
+ def register(name, klass)
10
+ validator_classes[name] = klass
11
+ end
12
+
13
+ def build(key, type, constraint)
14
+ validator_classes[type].new(key, constraint)
15
+ end
16
+ end
17
+
18
+ register "maximum", Validators::MaximumValidator
19
+ register "minimum", Validators::MinimumValidator
20
+ register "type", Validators::TypeValidator
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module Rack
2
+ class Spec
3
+ module Validators
4
+ class Base
5
+ def initialize(key, constraint)
6
+ @key = key
7
+ @constraint = constraint
8
+ end
9
+
10
+ def validate!(env)
11
+ raise NotImplementedError
12
+ end
13
+
14
+ private
15
+
16
+ def extract_value(env)
17
+ env["rack-spec.request"] ||= Rack::Request.new(env)
18
+ env["rack-spec.request"].params[@key]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ module Rack
2
+ class Spec
3
+ module Validators
4
+ class MaximumValidator < Base
5
+ def validate!(env)
6
+ value = extract_value(env)
7
+ if value && value.to_f > maximum
8
+ raise ValidationError, "Expected #@key to be equal or less than #@maximum, but in fact #{value.inspect}"
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def maximum
15
+ @constraint
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module Rack
2
+ class Spec
3
+ module Validators
4
+ class MinimumValidator < Base
5
+ def validate!(env)
6
+ value = extract_value(env)
7
+ if value && value.to_f < minimum
8
+ raise ValidationError, "Expected #@key to be equal or higher than #@minimum, but in fact #{value.inspect}"
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def minimum
15
+ @constraint
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ module Rack
2
+ class Spec
3
+ module Validators
4
+ class NullValidator < Base
5
+ def validate!(env)
6
+ end
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ module Rack
2
+ class Spec
3
+ module Validators
4
+ class QueryParametersValidator
5
+ def initialize(spec)
6
+ @spec = spec
7
+ end
8
+
9
+ def validate!(env)
10
+ parameters = @spec.reach("endpoints", env["PATH_INFO"], env["REQUEST_METHOD"], "queryParameters") || {}
11
+ parameters.each do |key, hash|
12
+ hash.each do |type, constraint|
13
+ ValidatorFactory.build(key, type, constraint).validate!(env)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,37 @@
1
+ require "time"
2
+
3
+ module Rack
4
+ class Spec
5
+ module Validators
6
+ class TypeValidator < Base
7
+ class << self
8
+ def patterns
9
+ @patterns ||= Hash.new(//)
10
+ end
11
+
12
+ def register(name, pattern)
13
+ patterns[name] = pattern
14
+ end
15
+ end
16
+
17
+ register "boolean", /\A(?:true|false)\z/
18
+ register "float", /\A-?\d+(?:\.\d+)*\z/
19
+ register "integer", /\A-?\d+\z/
20
+ register "iso8601", ->(value) { Time.iso8601(value) rescue false }
21
+
22
+ def validate!(env)
23
+ value = extract_value(env) or return
24
+ unless pattern === value
25
+ raise ValidationError, "Expected #@key to be #@constraint, but in fact #{value.inspect}"
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def pattern
32
+ self.class.patterns[@constraint]
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Spec
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/spec/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-spec"
8
+ spec.version = Rack::Spec::VERSION
9
+ spec.authors = ["Ryo Nakamura"]
10
+ spec.email = ["r7kamura@gmail.com"]
11
+ spec.summary = "Define specifications of your Rack application."
12
+
13
+ spec.homepage = "https://github.com/r7kamura/rack-spec"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rack"
22
+ spec.add_development_dependency "activesupport", "4.0.2"
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "rack-test"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec", "2.14.1"
28
+ spec.add_development_dependency "rspec-json_matcher", "0.1.3"
29
+ end
@@ -0,0 +1,116 @@
1
+ require "spec_helper"
2
+ require "active_support/core_ext/string/strip"
3
+ require "rack/test"
4
+ require "yaml"
5
+
6
+ describe Rack::Spec do
7
+ include Rack::Test::Methods
8
+
9
+ let(:app) do
10
+ described_class.new(original_app, spec: spec)
11
+ end
12
+
13
+ let(:original_app) do
14
+ ->(env) do
15
+ [200, {}, ["OK"]]
16
+ end
17
+ end
18
+
19
+ let(:spec) do
20
+ YAML.load(yaml)
21
+ end
22
+
23
+ let(:yaml) do
24
+ <<-EOS.strip_heredoc
25
+ ---
26
+ meta:
27
+ baseUri: http://api.example.com/
28
+
29
+ endpoints:
30
+ /recipes:
31
+ GET:
32
+ queryParameters:
33
+ page:
34
+ type: integer
35
+ minimum: 1
36
+ maximum: 10
37
+ private:
38
+ type: boolean
39
+ rank:
40
+ type: float
41
+ time:
42
+ type: iso8601
43
+ EOS
44
+ end
45
+
46
+ let(:path) do
47
+ "/recipes"
48
+ end
49
+
50
+ let(:params) do
51
+ {}
52
+ end
53
+
54
+ let(:env) do
55
+ {}
56
+ end
57
+
58
+ subject do
59
+ get path, params, env
60
+ last_response.status
61
+ end
62
+
63
+ describe "#call" do
64
+ context "with valid request" do
65
+ before do
66
+ params[:page] = 5
67
+ params[:private] = "false"
68
+ params[:rank] = 2.0
69
+ params[:time] = "2000-01-01T00:00:00+00:00"
70
+ end
71
+ it { should == 200 }
72
+ end
73
+
74
+ context "with query parameter invalid on integer" do
75
+ before do
76
+ params[:page] = "1.0"
77
+ end
78
+ it { should == 400 }
79
+ end
80
+
81
+ context "with query parameter invalid on float" do
82
+ before do
83
+ params[:rank] = "x"
84
+ end
85
+ it { should == 400 }
86
+ end
87
+
88
+ context "with query parameter invalid on boolean" do
89
+ before do
90
+ params[:private] = 1
91
+ end
92
+ it { should == 400 }
93
+ end
94
+
95
+ context "with query parameter invalid on iso8601" do
96
+ before do
97
+ params[:time] = "2000-01-01 00:00:00 +0000"
98
+ end
99
+ it { should == 400 }
100
+ end
101
+
102
+ context "with query parameter invalid on minimum" do
103
+ before do
104
+ params[:page] = 0
105
+ end
106
+ it { should == 400 }
107
+ end
108
+
109
+ context "with query parameter invalid on maximum" do
110
+ before do
111
+ params[:page] = 11
112
+ end
113
+ it { should == 400 }
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+ require "rack/spec"
3
+ require "rspec/json_matcher"
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+ config.include RSpec::JsonMatcher
10
+ end
metadata ADDED
@@ -0,0 +1,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-spec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryo Nakamura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 4.0.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 2.14.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 2.14.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-json_matcher
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '='
116
+ - !ruby/object:Gem::Version
117
+ version: 0.1.3
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '='
123
+ - !ruby/object:Gem::Version
124
+ version: 0.1.3
125
+ description:
126
+ email:
127
+ - r7kamura@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .gitignore
133
+ - Gemfile
134
+ - LICENSE.txt
135
+ - README.md
136
+ - Rakefile
137
+ - lib/rack-spec.rb
138
+ - lib/rack/spec.rb
139
+ - lib/rack/spec/exception_handler.rb
140
+ - lib/rack/spec/spec.rb
141
+ - lib/rack/spec/validation.rb
142
+ - lib/rack/spec/validation_error.rb
143
+ - lib/rack/spec/validator_factory.rb
144
+ - lib/rack/spec/validators/base.rb
145
+ - lib/rack/spec/validators/maximum_validator.rb
146
+ - lib/rack/spec/validators/minimum_validator.rb
147
+ - lib/rack/spec/validators/null_validator.rb
148
+ - lib/rack/spec/validators/query_parameters_validator.rb
149
+ - lib/rack/spec/validators/type_validator.rb
150
+ - lib/rack/spec/version.rb
151
+ - rack-spec.gemspec
152
+ - spec/rack/spec_spec.rb
153
+ - spec/spec_helper.rb
154
+ homepage: https://github.com/r7kamura/rack-spec
155
+ licenses:
156
+ - MIT
157
+ metadata: {}
158
+ post_install_message:
159
+ rdoc_options: []
160
+ require_paths:
161
+ - lib
162
+ required_ruby_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - '>='
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ requirements: []
173
+ rubyforge_project:
174
+ rubygems_version: 2.0.3
175
+ signing_key:
176
+ specification_version: 4
177
+ summary: Define specifications of your Rack application.
178
+ test_files:
179
+ - spec/rack/spec_spec.rb
180
+ - spec/spec_helper.rb
181
+ has_rdoc: