grape-formats 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +35 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +163 -0
- data/Guardfile +81 -0
- data/LICENSE +22 -0
- data/README.md +60 -0
- data/Rakefile +23 -0
- data/grape-formats.gemspec +32 -0
- data/lib/grape/formats.rb +3 -0
- data/lib/grape/formats/build.rb +24 -0
- data/lib/grape/formats/date_times.rb +47 -0
- data/lib/grape/formats/dates.rb +46 -0
- data/lib/grape/formats/version.rb +11 -0
- data/spec/grape/formats/date_times_spec.rb +55 -0
- data/spec/grape/formats/dates_spec.rb +45 -0
- data/spec/spec_helper.rb +24 -0
- metadata +231 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7dd3d595332ece5d0fb26f07304db15e51501ee7
|
4
|
+
data.tar.gz: 9f3c6397b9e640784fc2f3a573838d7ecba2d8de
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 40bdac08d8639cd8f153b39f50ef0513ab59d43fe2c573032e3eaeb7b874bb7824853fc4bf25c5e4c6cfa501595c9319df5e0a6df28eb5556c0a9e19c1abbb9a
|
7
|
+
data.tar.gz: e71d9ed29d85ec6e6a8194ca578c5551b41589fe154a417886da7190b3618c40e449a245095dc9c6c5147915e8e8b1273542ea0fd4b6e38781308547d5fa614a
|
data/.gitignore
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/vendor/bundle
|
26
|
+
/lib/bundler/man/
|
27
|
+
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
30
|
+
# Gemfile.lock
|
31
|
+
# .ruby-version
|
32
|
+
# .ruby-gemset
|
33
|
+
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
35
|
+
.rvmrc
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
GIT
|
2
|
+
remote: git://github.com/ruby-grape/grape.git
|
3
|
+
revision: b13dcca3e15993e5101b81427a00580c4dc9b6cb
|
4
|
+
specs:
|
5
|
+
grape (0.13.1)
|
6
|
+
activesupport
|
7
|
+
builder
|
8
|
+
hashie (>= 2.1.0)
|
9
|
+
multi_json (>= 1.3.2)
|
10
|
+
multi_xml (>= 0.5.2)
|
11
|
+
rack (>= 1.3.0)
|
12
|
+
rack-accept
|
13
|
+
rack-mount
|
14
|
+
virtus (>= 1.0.0)
|
15
|
+
|
16
|
+
PATH
|
17
|
+
remote: .
|
18
|
+
specs:
|
19
|
+
grape-formats (0.0.1)
|
20
|
+
grape (>= 0.13.1)
|
21
|
+
virtus (>= 1.0.0)
|
22
|
+
|
23
|
+
GEM
|
24
|
+
remote: https://rubygems.org/
|
25
|
+
specs:
|
26
|
+
activesupport (4.2.4)
|
27
|
+
i18n (~> 0.7)
|
28
|
+
json (~> 1.7, >= 1.7.7)
|
29
|
+
minitest (~> 5.1)
|
30
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
31
|
+
tzinfo (~> 1.1)
|
32
|
+
ast (2.1.0)
|
33
|
+
astrolabe (1.3.1)
|
34
|
+
parser (~> 2.2)
|
35
|
+
axiom-types (0.1.1)
|
36
|
+
descendants_tracker (~> 0.0.4)
|
37
|
+
ice_nine (~> 0.11.0)
|
38
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
39
|
+
builder (3.2.2)
|
40
|
+
byebug (6.0.2)
|
41
|
+
coderay (1.1.0)
|
42
|
+
coercible (1.0.0)
|
43
|
+
descendants_tracker (~> 0.0.1)
|
44
|
+
descendants_tracker (0.0.4)
|
45
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
46
|
+
diff-lcs (1.2.5)
|
47
|
+
docile (1.1.5)
|
48
|
+
equalizer (0.0.11)
|
49
|
+
ffi (1.9.10)
|
50
|
+
formatador (0.2.5)
|
51
|
+
guard (2.13.0)
|
52
|
+
formatador (>= 0.2.4)
|
53
|
+
listen (>= 2.7, <= 4.0)
|
54
|
+
lumberjack (~> 1.0)
|
55
|
+
nenv (~> 0.1)
|
56
|
+
notiffany (~> 0.0)
|
57
|
+
pry (>= 0.9.12)
|
58
|
+
shellany (~> 0.0)
|
59
|
+
thor (>= 0.18.1)
|
60
|
+
guard-compat (1.2.1)
|
61
|
+
guard-rspec (4.6.4)
|
62
|
+
guard (~> 2.1)
|
63
|
+
guard-compat (~> 1.1)
|
64
|
+
rspec (>= 2.99.0, < 4.0)
|
65
|
+
guard-rubocop (1.2.0)
|
66
|
+
guard (~> 2.0)
|
67
|
+
rubocop (~> 0.20)
|
68
|
+
guard-yard (2.1.4)
|
69
|
+
guard (>= 1.1.0)
|
70
|
+
yard (>= 0.7.0)
|
71
|
+
hashie (3.4.3)
|
72
|
+
i18n (0.7.0)
|
73
|
+
ice_nine (0.11.1)
|
74
|
+
json (1.8.3)
|
75
|
+
listen (3.0.3)
|
76
|
+
rb-fsevent (>= 0.9.3)
|
77
|
+
rb-inotify (>= 0.9)
|
78
|
+
lumberjack (1.0.9)
|
79
|
+
method_source (0.8.2)
|
80
|
+
minitest (5.8.2)
|
81
|
+
multi_json (1.11.2)
|
82
|
+
multi_xml (0.5.5)
|
83
|
+
nenv (0.2.0)
|
84
|
+
notiffany (0.0.8)
|
85
|
+
nenv (~> 0.1)
|
86
|
+
shellany (~> 0.0)
|
87
|
+
parser (2.2.3.0)
|
88
|
+
ast (>= 1.1, < 3.0)
|
89
|
+
powerpack (0.1.1)
|
90
|
+
pry (0.10.3)
|
91
|
+
coderay (~> 1.1.0)
|
92
|
+
method_source (~> 0.8.1)
|
93
|
+
slop (~> 3.4)
|
94
|
+
rack (1.6.4)
|
95
|
+
rack-accept (0.4.5)
|
96
|
+
rack (>= 0.4)
|
97
|
+
rack-mount (0.8.3)
|
98
|
+
rack (>= 1.0.0)
|
99
|
+
rack-test (0.6.3)
|
100
|
+
rack (>= 1.0)
|
101
|
+
rainbow (2.0.0)
|
102
|
+
rake (10.4.2)
|
103
|
+
rb-fsevent (0.9.6)
|
104
|
+
rb-inotify (0.9.5)
|
105
|
+
ffi (>= 0.5.0)
|
106
|
+
rspec (3.3.0)
|
107
|
+
rspec-core (~> 3.3.0)
|
108
|
+
rspec-expectations (~> 3.3.0)
|
109
|
+
rspec-mocks (~> 3.3.0)
|
110
|
+
rspec-core (3.3.2)
|
111
|
+
rspec-support (~> 3.3.0)
|
112
|
+
rspec-expectations (3.3.1)
|
113
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
114
|
+
rspec-support (~> 3.3.0)
|
115
|
+
rspec-mocks (3.3.2)
|
116
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
117
|
+
rspec-support (~> 3.3.0)
|
118
|
+
rspec-support (3.3.0)
|
119
|
+
rubocop (0.34.2)
|
120
|
+
astrolabe (~> 1.3)
|
121
|
+
parser (>= 2.2.2.5, < 3.0)
|
122
|
+
powerpack (~> 0.1)
|
123
|
+
rainbow (>= 1.99.1, < 3.0)
|
124
|
+
ruby-progressbar (~> 1.4)
|
125
|
+
ruby-progressbar (1.7.5)
|
126
|
+
shellany (0.0.1)
|
127
|
+
simplecov (0.10.0)
|
128
|
+
docile (~> 1.1.0)
|
129
|
+
json (~> 1.8)
|
130
|
+
simplecov-html (~> 0.10.0)
|
131
|
+
simplecov-html (0.10.0)
|
132
|
+
slop (3.6.0)
|
133
|
+
thor (0.19.1)
|
134
|
+
thread_safe (0.3.5)
|
135
|
+
tzinfo (1.2.2)
|
136
|
+
thread_safe (~> 0.1)
|
137
|
+
virtus (1.0.5)
|
138
|
+
axiom-types (~> 0.1)
|
139
|
+
coercible (~> 1.0)
|
140
|
+
descendants_tracker (~> 0.0, >= 0.0.3)
|
141
|
+
equalizer (~> 0.0, >= 0.0.9)
|
142
|
+
yard (0.8.7.6)
|
143
|
+
|
144
|
+
PLATFORMS
|
145
|
+
ruby
|
146
|
+
|
147
|
+
DEPENDENCIES
|
148
|
+
bundler
|
149
|
+
byebug
|
150
|
+
grape!
|
151
|
+
grape-formats!
|
152
|
+
guard
|
153
|
+
guard-rspec
|
154
|
+
guard-rubocop
|
155
|
+
guard-yard
|
156
|
+
rack-test
|
157
|
+
rake
|
158
|
+
rubocop
|
159
|
+
simplecov
|
160
|
+
yard
|
161
|
+
|
162
|
+
BUNDLED WITH
|
163
|
+
1.10.6
|
data/Guardfile
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec features) \
|
6
|
+
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
7
|
+
|
8
|
+
## Note: if you are using the `directories` clause above and you are not
|
9
|
+
## watching the project directory ('.'), then you will want to move
|
10
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
11
|
+
#
|
12
|
+
# $ mkdir config
|
13
|
+
# $ mv Guardfile config/
|
14
|
+
# $ ln -s config/Guardfile .
|
15
|
+
#
|
16
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
17
|
+
|
18
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
19
|
+
# rspec may be run, below are examples of the most common uses.
|
20
|
+
# * bundler: 'bundle exec rspec'
|
21
|
+
# * bundler binstubs: 'bin/rspec'
|
22
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
23
|
+
# installed the spring binstubs per the docs)
|
24
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
25
|
+
# * 'just' rspec: 'rspec'
|
26
|
+
|
27
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
28
|
+
require 'guard/rspec/dsl'
|
29
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
30
|
+
|
31
|
+
# Feel free to open issues for suggestions and improvements
|
32
|
+
|
33
|
+
# RSpec files
|
34
|
+
rspec = dsl.rspec
|
35
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
36
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
37
|
+
watch(rspec.spec_files)
|
38
|
+
|
39
|
+
# Ruby files
|
40
|
+
ruby = dsl.ruby
|
41
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
42
|
+
|
43
|
+
# Rails files
|
44
|
+
rails = dsl.rails(view_extensions: %w(erb haml slim))
|
45
|
+
dsl.watch_spec_files_for(rails.app_files)
|
46
|
+
dsl.watch_spec_files_for(rails.views)
|
47
|
+
|
48
|
+
watch(rails.controllers) do |m|
|
49
|
+
[
|
50
|
+
rspec.spec.call("routing/#{m[1]}_routing"),
|
51
|
+
rspec.spec.call("controllers/#{m[1]}_controller"),
|
52
|
+
rspec.spec.call("acceptance/#{m[1]}")
|
53
|
+
]
|
54
|
+
end
|
55
|
+
|
56
|
+
# Rails config changes
|
57
|
+
watch(rails.spec_helper) { rspec.spec_dir }
|
58
|
+
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
|
59
|
+
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
|
60
|
+
|
61
|
+
# Capybara features specs
|
62
|
+
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
|
63
|
+
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
|
64
|
+
|
65
|
+
# Turnip features and steps
|
66
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
67
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
68
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
guard :rubocop do
|
73
|
+
watch(/.+\.rb$/)
|
74
|
+
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
|
75
|
+
end
|
76
|
+
|
77
|
+
guard 'yard' do
|
78
|
+
watch(%r{app/.+\.rb})
|
79
|
+
watch(%r{lib/.+\.rb})
|
80
|
+
watch(%r{ext/.+\.c})
|
81
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Doug Hammond
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# grape-formats
|
2
|
+
|
3
|
+
`grape-formats` is a very small extension to [grape](https://github.com/ruby-grape/grape)
|
4
|
+
that provides endpoint parameter validation and type coercion for a range of well-known
|
5
|
+
data formats.
|
6
|
+
|
7
|
+
To use these formats, all you need to do is pass the appropriate token as the `type`
|
8
|
+
option for any endpoint parameter declared in the `params` block using `requires` or
|
9
|
+
`optional`. See the usage examples below.
|
10
|
+
|
11
|
+
## `Date`
|
12
|
+
|
13
|
+
Most of the standard string parsing functions available on the `Date` class are
|
14
|
+
available in `Grape::Formats::Dates`. Any parameter value that doesn't match the
|
15
|
+
expected format will cause a `HTTP 400` error to be generated.
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
require 'grape'
|
19
|
+
require 'grape/formats/dates'
|
20
|
+
|
21
|
+
Dates = Grape::Formats::Dates
|
22
|
+
|
23
|
+
class API < Grape::API
|
24
|
+
resource :calendar do
|
25
|
+
params do
|
26
|
+
# Allows '2001-02-03', '20010203' ...
|
27
|
+
requires :start, type: Dates::Iso8601
|
28
|
+
|
29
|
+
# Allows 'H13.02.04' ...
|
30
|
+
requires :finish, type: Dates::Jisx0301
|
31
|
+
end
|
32
|
+
get :agenda do
|
33
|
+
params[:start] # => #<Date: 2001-02-03 ...>
|
34
|
+
params[:finish] # => #<Date: 2001-02-04 ...>
|
35
|
+
# ...
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
See `lib/grape/formats/dates.rb` for a full list of available formats.
|
42
|
+
|
43
|
+
## `DateTime`
|
44
|
+
|
45
|
+
As with `Grape::Formats::Dates`, so too with `Grape::Formats::DateTimes`
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
require 'grape/formats/date_times'
|
49
|
+
|
50
|
+
DateTimes = Grape::Formats::DateTimes
|
51
|
+
|
52
|
+
# ...
|
53
|
+
|
54
|
+
params do
|
55
|
+
requires :start, type: DateTimes::Iso8601
|
56
|
+
requires :finish, type: DateTimes::Jisx0301
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
See `lib/grape/formats/date_times.rb` for available formats.
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.setup :default, :test, :development
|
4
|
+
|
5
|
+
task default: [:rubocop, :spec, :doc]
|
6
|
+
|
7
|
+
Bundler::GemHelper.install_tasks
|
8
|
+
|
9
|
+
require 'rspec/core/rake_task'
|
10
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
11
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'rainbow/ext/string' unless String.respond_to?(:color)
|
15
|
+
require 'rubocop/rake_task'
|
16
|
+
RuboCop::RakeTask.new
|
17
|
+
|
18
|
+
require 'yard'
|
19
|
+
DOC_FILES = ['lib/**/*.rb', 'README.md']
|
20
|
+
|
21
|
+
YARD::Rake::YardocTask.new(:doc) do |t|
|
22
|
+
t.files = DOC_FILES
|
23
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
2
|
+
require 'grape/formats/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'grape-formats'
|
6
|
+
s.version = Grape::Formats::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ['Doug Hammond']
|
9
|
+
s.email = ['d.lakehammond@gmail.com']
|
10
|
+
s.summary = 'Extended format parsing and type validation for Grape.'
|
11
|
+
s.description =
|
12
|
+
'Provides a number of validators for various parameter types and formats.'
|
13
|
+
s.license = 'MIT'
|
14
|
+
|
15
|
+
s.add_runtime_dependency 'grape', '>= 0.13.1'
|
16
|
+
s.add_runtime_dependency 'virtus', '>= 1.0.0'
|
17
|
+
|
18
|
+
s.add_development_dependency 'bundler'
|
19
|
+
s.add_development_dependency 'guard'
|
20
|
+
s.add_development_dependency 'guard-rspec'
|
21
|
+
s.add_development_dependency 'guard-rubocop'
|
22
|
+
s.add_development_dependency 'guard-yard'
|
23
|
+
s.add_development_dependency 'rack-test'
|
24
|
+
s.add_development_dependency 'rake'
|
25
|
+
s.add_development_dependency 'rubocop'
|
26
|
+
s.add_development_dependency 'simplecov'
|
27
|
+
s.add_development_dependency 'yard'
|
28
|
+
|
29
|
+
s.files = `git ls-files`.split("\n")
|
30
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
31
|
+
s.require_paths = ['lib']
|
32
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'virtus'
|
2
|
+
require 'grape/validations/types/custom_type_coercer'
|
3
|
+
|
4
|
+
module Grape
|
5
|
+
# Scaffolding for building types.
|
6
|
+
module Formats
|
7
|
+
# Common function for building 'types' that consist of an
|
8
|
+
# underlying primitive type and an arbitrary coercion method
|
9
|
+
# used to convert strings to that type.
|
10
|
+
#
|
11
|
+
# Makes use of the +CustomTypeCoercer+ class in the base Grape gem.
|
12
|
+
#
|
13
|
+
# @param type [Class] the type to which parameter values will be coerced
|
14
|
+
# @param method [Proc] type coercion method.
|
15
|
+
# Should accept a single +String+ parameter and return an object of
|
16
|
+
# type +type+.
|
17
|
+
# @return [Virtus::Attribute] an object that may be passed as the +type+
|
18
|
+
# option for any Grape endpoint parameter declared with +requires+ or
|
19
|
+
# +optional+.
|
20
|
+
def self.build(type, method)
|
21
|
+
Grape::Validations::Types::CustomTypeCoercer.build(type, method)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'time'
|
2
|
+
require_relative 'build'
|
3
|
+
|
4
|
+
module Grape
|
5
|
+
module Formats
|
6
|
+
# This module provides a set of ready-made +Virtus::Attribute+
|
7
|
+
# constants, suitable for use as the +type+ option for
|
8
|
+
# {Grape::Dsl::Parameters#requires} and {Grape::Dsl::Parameters#optional}.
|
9
|
+
# These definitions will coerce input strings to the standard
|
10
|
+
# ruby +DateTime+ type using the standard parsing methods defined
|
11
|
+
# on that class.
|
12
|
+
#
|
13
|
+
# This module is not required by default.
|
14
|
+
module DateTimes
|
15
|
+
# Parses timestamps using +DateTime.httpdate+
|
16
|
+
HttpDate = Formats.build(::DateTime, ::DateTime.method(:httpdate))
|
17
|
+
|
18
|
+
# Parses timestamps using +DateTime.iso8601+
|
19
|
+
Iso8601 = Formats.build(::DateTime, ::DateTime.method(:iso8601))
|
20
|
+
|
21
|
+
# Parses timestamps using +DateTime.jisx0301+
|
22
|
+
Jisx0301 = Formats.build(::DateTime, ::DateTime.method(:jisx0301))
|
23
|
+
|
24
|
+
# Parses julian dates using +DateTime.jd+.
|
25
|
+
# Time of day is not supported.
|
26
|
+
JulianDay = Formats.build(::DateTime, lambda do |val|
|
27
|
+
unless val =~ /^\d*$/
|
28
|
+
fail Grape::Exceptions::Validations, 'julian date must be an integer'
|
29
|
+
end
|
30
|
+
|
31
|
+
::DateTime.jd val.to_i
|
32
|
+
end)
|
33
|
+
|
34
|
+
# Parses timestamps using +DateTime.rfc2822+
|
35
|
+
Rfc2822 = Formats.build(::DateTime, ::DateTime.method(:rfc2822))
|
36
|
+
|
37
|
+
# Parses timestamps using +DateTime.rfc3339+
|
38
|
+
Rfc3339 = Formats.build(::DateTime, ::DateTime.method(:rfc3339))
|
39
|
+
|
40
|
+
# Parses timestamps using +DateTime.rfc822+
|
41
|
+
Rfc822 = Formats.build(::DateTime, ::DateTime.method(:rfc822))
|
42
|
+
|
43
|
+
# Parses timestamps using +DateTime.xmlschema+
|
44
|
+
XmlSchema = Formats.build(::DateTime, ::DateTime.method(:xmlschema))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'date'
|
2
|
+
require_relative 'build'
|
3
|
+
|
4
|
+
module Grape
|
5
|
+
module Formats
|
6
|
+
# This module provides a set of ready-made +Virtus::Attribute+
|
7
|
+
# constants, suitable for use as the +type+ option for
|
8
|
+
# {Grape::Dsl::Parameters#requires} and {Grape::Dsl::Parameters#optional}.
|
9
|
+
# These definitions will coerce input strings to the standard
|
10
|
+
# ruby +Date+ type using the standard parsing methods defined
|
11
|
+
# on that class.
|
12
|
+
#
|
13
|
+
# This module is not required by default.
|
14
|
+
module Dates
|
15
|
+
# Parses dates using +Date.httpdate+
|
16
|
+
HttpDate = Formats.build(::Date, ::Date.method(:httpdate))
|
17
|
+
|
18
|
+
# Parses dates using +Date.iso8601+
|
19
|
+
Iso8601 = Formats.build(::Date, ::Date.method(:iso8601))
|
20
|
+
|
21
|
+
# Parses dates using +Date.jisx0301+
|
22
|
+
Jisx0301 = Formats.build(::Date, ::Date.method(:jisx0301))
|
23
|
+
|
24
|
+
# Parses dates using +Date.jd+
|
25
|
+
JulianDay = Formats.build(::Date, lambda do |val|
|
26
|
+
unless val =~ /^\d*$/
|
27
|
+
fail Grape::Exceptions::Validations, 'julian date must be an integer'
|
28
|
+
end
|
29
|
+
|
30
|
+
::Date.jd val.to_i
|
31
|
+
end)
|
32
|
+
|
33
|
+
# Parses dates using +Date.rfc2822+
|
34
|
+
Rfc2822 = Formats.build(::Date, ::Date.method(:rfc2822))
|
35
|
+
|
36
|
+
# Parses dates using +Date.rfc3339+
|
37
|
+
Rfc3339 = Formats.build(::Date, ::Date.method(:rfc3339))
|
38
|
+
|
39
|
+
# Parses dates using +Date.rfc822+
|
40
|
+
Rfc822 = Formats.build(::Date, ::Date.method(:rfc822))
|
41
|
+
|
42
|
+
# Parses dates using +Date.xmlschema+
|
43
|
+
XmlSchema = Formats.build(::Date, ::Date.method(:xmlschema))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Grape
|
2
|
+
# Contains collections of constants that may be passed
|
3
|
+
# as the +type+ parameter of {Grape::Dsl::Parameters#requires}
|
4
|
+
# or {Grape::Dsl::Parameters#optional}, providing
|
5
|
+
# parameter coercion from a range of standard formats
|
6
|
+
# to a number of standard types.
|
7
|
+
module Formats
|
8
|
+
# Current version of the grape-formats gem
|
9
|
+
VERSION = '0.0.1'
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'grape/formats/date_times'
|
3
|
+
|
4
|
+
describe Grape::Formats::DateTimes do
|
5
|
+
let(:api) { Class.new(Grape::API) }
|
6
|
+
|
7
|
+
def app
|
8
|
+
api
|
9
|
+
end
|
10
|
+
|
11
|
+
DATE_TIME_INPUTS = {
|
12
|
+
HttpDate: ['Sat, 03 Feb 2001 04:05:06 GMT'],
|
13
|
+
Iso8601: %w(
|
14
|
+
2001-02-03T04:05:06+07:00
|
15
|
+
20010203T040506+0700
|
16
|
+
2001-W05-6T04:05:06+07:00
|
17
|
+
),
|
18
|
+
Jisx0301: %w(H13.02.03T04:05:06+07:00),
|
19
|
+
JulianDay: %w(2451944),
|
20
|
+
Rfc2822: ['Sat, 3 Feb 2001 04:05:06 +0700'],
|
21
|
+
Rfc3339: %w(2001-02-03T04:05:06+07:00),
|
22
|
+
Rfc822: ['Sat, 3 Feb 2001 04:05:06 +0700'],
|
23
|
+
XmlSchema: %w(2001-02-03T04:05:06+07:00)
|
24
|
+
}
|
25
|
+
|
26
|
+
COERCION_EXPECTATION = Hash.new('DateTime.2001-02-03T04:05:06+07:00')
|
27
|
+
# Special expectations for formats that don't support
|
28
|
+
# full timestamp plus timezone.
|
29
|
+
COERCION_EXPECTATION[:HttpDate] = 'DateTime.2001-02-03T04:05:06+00:00'
|
30
|
+
COERCION_EXPECTATION[:JulianDay] = 'DateTime.2001-02-03T00:00:00+00:00'
|
31
|
+
|
32
|
+
context 'timestamp coercion' do
|
33
|
+
described_class.constants.each do |format_name|
|
34
|
+
it "coerces from #{format_name}" do
|
35
|
+
format = described_class.const_get format_name
|
36
|
+
api.params do
|
37
|
+
requires :time, type: format
|
38
|
+
end
|
39
|
+
api.get '/' do
|
40
|
+
"#{params[:time].class}.#{params[:time]}"
|
41
|
+
end
|
42
|
+
|
43
|
+
DATE_TIME_INPUTS[format_name].each do |input|
|
44
|
+
get '/', time: input
|
45
|
+
expect(last_response.status).to eq(200)
|
46
|
+
expect(last_response.body).to eq(COERCION_EXPECTATION[format_name])
|
47
|
+
|
48
|
+
get '/', time: 'definitely not a timestamp'
|
49
|
+
expect(last_response.status).to eq(400)
|
50
|
+
expect(last_response.body).to eq('time is invalid')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'grape/formats/dates'
|
3
|
+
|
4
|
+
describe Grape::Formats::Dates do
|
5
|
+
let(:api) { Class.new(Grape::API) }
|
6
|
+
|
7
|
+
def app
|
8
|
+
api
|
9
|
+
end
|
10
|
+
|
11
|
+
DATE_INPUTS = {
|
12
|
+
HttpDate: ['Sat, 03 Feb 2001 00:00:00 GMT'],
|
13
|
+
Iso8601: %w(2001-02-03 20010203 2001-W05-6),
|
14
|
+
Jisx0301: %w(H13.02.03),
|
15
|
+
JulianDay: %w(2451944),
|
16
|
+
Rfc2822: ['Sat, 3 Feb 2001 00:00:00 +0000'],
|
17
|
+
Rfc3339: %w(2001-02-03T04:05:06+07:00),
|
18
|
+
Rfc822: ['Sat, 3 Feb 2001 00:00:00 +0000'],
|
19
|
+
XmlSchema: %w(2001-02-03)
|
20
|
+
}
|
21
|
+
|
22
|
+
context 'date coercion' do
|
23
|
+
described_class.constants.each do |format_name|
|
24
|
+
it "coerces from #{format_name}" do
|
25
|
+
format = described_class.const_get format_name
|
26
|
+
api.params do
|
27
|
+
requires :date, type: format
|
28
|
+
end
|
29
|
+
api.get '/' do
|
30
|
+
"#{params[:date].class}.#{params[:date]}"
|
31
|
+
end
|
32
|
+
|
33
|
+
DATE_INPUTS[format_name].each do |input|
|
34
|
+
get '/', date: input
|
35
|
+
expect(last_response.status).to eq(200)
|
36
|
+
expect(last_response.body).to eq('Date.2001-02-03')
|
37
|
+
|
38
|
+
get '/', date: 'definitely not a date'
|
39
|
+
expect(last_response.status).to eq(400)
|
40
|
+
expect(last_response.body).to eq('date is invalid')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'support'))
|
4
|
+
|
5
|
+
require 'simplecov'
|
6
|
+
SimpleCov.start
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'bundler'
|
10
|
+
Bundler.setup :default, :test
|
11
|
+
|
12
|
+
require 'grape'
|
13
|
+
require 'rack/test'
|
14
|
+
|
15
|
+
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each do |file|
|
16
|
+
require file
|
17
|
+
end
|
18
|
+
|
19
|
+
RSpec.configure do |config|
|
20
|
+
config.include Rack::Test::Methods
|
21
|
+
config.raise_errors_for_deprecations!
|
22
|
+
|
23
|
+
config.before(:each) { Grape::Util::InheritableSetting.reset_global! }
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,231 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grape-formats
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Doug Hammond
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-01-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: grape
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.13.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.13.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: virtus
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard
|
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: guard-rspec
|
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: guard-rubocop
|
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: guard-yard
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rack-test
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rake
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rubocop
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: simplecov
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: yard
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
181
|
+
description: Provides a number of validators for various parameter types and formats.
|
182
|
+
email:
|
183
|
+
- d.lakehammond@gmail.com
|
184
|
+
executables: []
|
185
|
+
extensions: []
|
186
|
+
extra_rdoc_files: []
|
187
|
+
files:
|
188
|
+
- ".gitignore"
|
189
|
+
- Gemfile
|
190
|
+
- Gemfile.lock
|
191
|
+
- Guardfile
|
192
|
+
- LICENSE
|
193
|
+
- README.md
|
194
|
+
- Rakefile
|
195
|
+
- grape-formats.gemspec
|
196
|
+
- lib/grape/formats.rb
|
197
|
+
- lib/grape/formats/build.rb
|
198
|
+
- lib/grape/formats/date_times.rb
|
199
|
+
- lib/grape/formats/dates.rb
|
200
|
+
- lib/grape/formats/version.rb
|
201
|
+
- spec/grape/formats/date_times_spec.rb
|
202
|
+
- spec/grape/formats/dates_spec.rb
|
203
|
+
- spec/spec_helper.rb
|
204
|
+
homepage:
|
205
|
+
licenses:
|
206
|
+
- MIT
|
207
|
+
metadata: {}
|
208
|
+
post_install_message:
|
209
|
+
rdoc_options: []
|
210
|
+
require_paths:
|
211
|
+
- lib
|
212
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
213
|
+
requirements:
|
214
|
+
- - ">="
|
215
|
+
- !ruby/object:Gem::Version
|
216
|
+
version: '0'
|
217
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
218
|
+
requirements:
|
219
|
+
- - ">="
|
220
|
+
- !ruby/object:Gem::Version
|
221
|
+
version: '0'
|
222
|
+
requirements: []
|
223
|
+
rubyforge_project:
|
224
|
+
rubygems_version: 2.4.8
|
225
|
+
signing_key:
|
226
|
+
specification_version: 4
|
227
|
+
summary: Extended format parsing and type validation for Grape.
|
228
|
+
test_files:
|
229
|
+
- spec/grape/formats/date_times_spec.rb
|
230
|
+
- spec/grape/formats/dates_spec.rb
|
231
|
+
- spec/spec_helper.rb
|