apiculture 0.0.15 → 0.0.16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/apiculture.gemspec +4 -4
- data/lib/apiculture.rb +10 -10
- data/lib/apiculture/method_documentation.rb +1 -1
- data/lib/apiculture/version.rb +1 -1
- data/spec/apiculture_spec.rb +27 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fddefec311625b3874fbcf1f2359f17d9393ae03
|
4
|
+
data.tar.gz: 2006215622d1839c0316d1bd1c790b65b08e6fb5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3933f460a2b5b2cd1ea3187d8b4023851efc82ad5b555b55decb0922751f198336998f59d16262ca522f2ef097c26e6978a063be122b842bf1ecd6977b7bacff
|
7
|
+
data.tar.gz: b8f2c0ed089a32b45b5edeb98aea9fe1110b07a92f3df5aa0497c24ccb61e602fcfc547d1b4a0d011a631ca5b871c3ab198569dee3ec04b6b78538392e1ce1fd
|
data/.travis.yml
CHANGED
data/apiculture.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: apiculture 0.0.
|
5
|
+
# stub: apiculture 0.0.16 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "apiculture"
|
9
|
-
s.version = "0.0.
|
9
|
+
s.version = "0.0.16"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Julik Tarkhanov", "WeTransfer"]
|
14
|
-
s.date = "2016-
|
14
|
+
s.date = "2016-09-27"
|
15
15
|
s.description = "A toolkit for building REST APIs on top of Sinatra"
|
16
16
|
s.email = "me@julik.nl"
|
17
17
|
s.extra_rdoc_files = [
|
@@ -43,7 +43,7 @@ Gem::Specification.new do |s|
|
|
43
43
|
]
|
44
44
|
s.homepage = "https://github.com/WeTransfer/apiculture"
|
45
45
|
s.licenses = ["MIT"]
|
46
|
-
s.rubygems_version = "2.
|
46
|
+
s.rubygems_version = "2.5.1"
|
47
47
|
s.summary = "Sweet API sauce on top of Sintra"
|
48
48
|
|
49
49
|
if s.respond_to? :specification_version then
|
data/lib/apiculture.rb
CHANGED
@@ -23,14 +23,14 @@ module Apiculture
|
|
23
23
|
}
|
24
24
|
|
25
25
|
AC_CHECK_TYPE_PROC = ->(param, value) {
|
26
|
-
|
26
|
+
param.matchable === value or raise ParameterTypeMismatch.new(param, value.class)
|
27
27
|
}
|
28
28
|
|
29
29
|
AC_PERMIT_PROC = ->(maybe_strong_params, param_name) {
|
30
30
|
maybe_strong_params.permit(param_name) if maybe_strong_params.respond_to?(:permit)
|
31
31
|
}
|
32
32
|
|
33
|
-
class Parameter < Struct.new(:name, :description, :required, :
|
33
|
+
class Parameter < Struct.new(:name, :description, :required, :matchable, :cast_proc_or_method)
|
34
34
|
# Return Strings since Sinatra prefers string keys for params{}
|
35
35
|
def name_as_string; name.to_s; end
|
36
36
|
end
|
@@ -103,24 +103,24 @@ module Apiculture
|
|
103
103
|
end
|
104
104
|
|
105
105
|
# Add an optional parameter for the API call
|
106
|
-
def param(name, description,
|
106
|
+
def param(name, description, matchable, cast: IDENTITY_PROC)
|
107
107
|
@apiculture_action_definition ||= ActionDefinition.new
|
108
|
-
@apiculture_action_definition.parameters << Parameter.new(name, description, required=false,
|
108
|
+
@apiculture_action_definition.parameters << Parameter.new(name, description, required=false, matchable, cast)
|
109
109
|
end
|
110
110
|
|
111
111
|
# Add a requred parameter for the API call
|
112
|
-
def required_param(name, description,
|
112
|
+
def required_param(name, description, matchable, cast: IDENTITY_PROC)
|
113
113
|
@apiculture_action_definition ||= ActionDefinition.new
|
114
|
-
@apiculture_action_definition.parameters << Parameter.new(name, description, required=true,
|
114
|
+
@apiculture_action_definition.parameters << Parameter.new(name, description, required=true, matchable, cast)
|
115
115
|
end
|
116
116
|
|
117
117
|
# Describe a parameter that has to be included in the URL of the API call.
|
118
118
|
# Route parameters are always required, and all the parameters specified
|
119
119
|
# using +route_param+ should also be included in the path given for the route
|
120
120
|
# definition
|
121
|
-
def route_param(name, description,
|
121
|
+
def route_param(name, description, matchable = String, cast: IDENTITY_PROC)
|
122
122
|
@apiculture_action_definition ||= ActionDefinition.new
|
123
|
-
@apiculture_action_definition.route_parameters << RouteParameter.new(name, description, required=false,
|
123
|
+
@apiculture_action_definition.route_parameters << RouteParameter.new(name, description, required=false, matchable, cast)
|
124
124
|
end
|
125
125
|
|
126
126
|
# Add a possible response, specifying the code and the JSON Response by example.
|
@@ -147,9 +147,9 @@ module Apiculture
|
|
147
147
|
# Gets raised when a parameter is supplied and has a wrong type
|
148
148
|
class ParameterTypeMismatch < ValidationError
|
149
149
|
def initialize(ac_parameter, received_ruby_type)
|
150
|
-
parameter_name, expected_type = ac_parameter.name, ac_parameter.
|
150
|
+
parameter_name, expected_type = ac_parameter.name, ac_parameter.matchable
|
151
151
|
received_type = received_ruby_type
|
152
|
-
super "Received #{received_type}, expected #{expected_type} for :#{parameter_name}"
|
152
|
+
super "Received #{received_type}, expected #{expected_type.inspect} for :#{parameter_name}"
|
153
153
|
end
|
154
154
|
end
|
155
155
|
|
@@ -132,7 +132,7 @@ class Apiculture::MethodDocumentation
|
|
132
132
|
html.tr do
|
133
133
|
html.td { html.tt(param.name.to_s) }
|
134
134
|
html.td(param.required ? 'Yes' : 'No')
|
135
|
-
html.td(param.
|
135
|
+
html.td(param.matchable.inspect)
|
136
136
|
html.td(param.description.to_s)
|
137
137
|
end
|
138
138
|
end
|
data/lib/apiculture/version.rb
CHANGED
data/spec/apiculture_spec.rb
CHANGED
@@ -149,7 +149,33 @@ describe "Apiculture" do
|
|
149
149
|
post '/thing', {number: '123'}
|
150
150
|
}.to raise_error('Received String, expected Integer for :number')
|
151
151
|
end
|
152
|
-
|
152
|
+
|
153
|
+
it 'supports an arbitrary object with === as a type specifier for a parameter' do
|
154
|
+
custom_matcher = Class.new do
|
155
|
+
def ===(value)
|
156
|
+
value == "Magic word"
|
157
|
+
end
|
158
|
+
end.new
|
159
|
+
|
160
|
+
@app_class = Class.new(Sinatra::Base) do
|
161
|
+
settings.show_exceptions = false
|
162
|
+
settings.raise_errors = true
|
163
|
+
extend Apiculture
|
164
|
+
|
165
|
+
required_param :pretty_please, "Only a magic word will do", custom_matcher
|
166
|
+
api_method :post, '/thing' do
|
167
|
+
'Ohai!'
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
post '/thing', {pretty_please: 'Magic word'}
|
172
|
+
expect(last_response).to be_ok
|
173
|
+
|
174
|
+
expect {
|
175
|
+
post '/thing', {pretty_please: 'not the magic word you are looking for'}
|
176
|
+
}.to raise_error(Apiculture::ParameterTypeMismatch)
|
177
|
+
end
|
178
|
+
|
153
179
|
it 'suppresses parameters that are not defined in the action definition' do
|
154
180
|
@app_class = Class.new(Sinatra::Base) do
|
155
181
|
settings.show_exceptions = false
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apiculture
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.16
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julik Tarkhanov
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-09-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sinatra
|
@@ -220,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
220
220
|
version: '0'
|
221
221
|
requirements: []
|
222
222
|
rubyforge_project:
|
223
|
-
rubygems_version: 2.
|
223
|
+
rubygems_version: 2.5.1
|
224
224
|
signing_key:
|
225
225
|
specification_version: 4
|
226
226
|
summary: Sweet API sauce on top of Sintra
|