haveapi 0.14.2 → 0.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/haveapi.gemspec +3 -3
- data/lib/haveapi/action.rb +3 -3
- data/lib/haveapi/authorization.rb +2 -2
- data/lib/haveapi/hooks.rb +25 -24
- data/lib/haveapi/params.rb +34 -40
- data/lib/haveapi/server.rb +1 -1
- data/lib/haveapi/version.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0aac56116176f2958372f10f63a776b9f4c1a2f6f13f8ee7b9eaa5493ce87821
|
4
|
+
data.tar.gz: 9f50e9702ab8ee10754e3bc11ffd6822f5a156782489ca1a9aac8f331c0c8000
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01e08a320e77b1e7608d31197714d157b2a602b1dd0f7216bcea91e53c3aa30c3c73b898ac3e20e86a0e5869477e331210d887210a4e9cb08ad8bd78fd8305ad
|
7
|
+
data.tar.gz: dc49f89724e8c05ffa80247b89aeae8d80e649c6e529ed4bae9989c0ff9bb21c0966f9fc037e3d0c94899c75a110b4c93297e26157e270d191ae0e2128d62910
|
data/Gemfile
CHANGED
data/haveapi.gemspec
CHANGED
@@ -16,13 +16,13 @@ Gem::Specification.new do |s|
|
|
16
16
|
|
17
17
|
s.add_runtime_dependency 'require_all', '~> 2.0.0'
|
18
18
|
s.add_runtime_dependency 'json'
|
19
|
-
s.add_runtime_dependency 'activesupport', '>=
|
20
|
-
s.add_runtime_dependency 'sinatra', '~> 2.
|
19
|
+
s.add_runtime_dependency 'activesupport', '>= 6.0'
|
20
|
+
s.add_runtime_dependency 'sinatra', '~> 2.2.0'
|
21
21
|
s.add_runtime_dependency 'tilt', '~> 2.0.10'
|
22
22
|
s.add_runtime_dependency 'redcarpet', '~> 3.5'
|
23
23
|
s.add_runtime_dependency 'rake'
|
24
24
|
s.add_runtime_dependency 'github-markdown'
|
25
25
|
s.add_runtime_dependency 'nesty', '~> 1.0'
|
26
|
-
s.add_runtime_dependency 'haveapi-client', '~> 0.
|
26
|
+
s.add_runtime_dependency 'haveapi-client', '~> 0.16.0'
|
27
27
|
s.add_runtime_dependency 'mail'
|
28
28
|
end
|
data/lib/haveapi/action.rb
CHANGED
@@ -464,11 +464,11 @@ module HaveAPI
|
|
464
464
|
end
|
465
465
|
|
466
466
|
protected
|
467
|
-
def with_restricted(
|
468
|
-
if
|
467
|
+
def with_restricted(**kwargs)
|
468
|
+
if kwargs.empty?
|
469
469
|
@authorization.restrictions
|
470
470
|
else
|
471
|
-
|
471
|
+
kwargs.update(@authorization.restrictions)
|
472
472
|
end
|
473
473
|
end
|
474
474
|
|
@@ -17,8 +17,8 @@ module HaveAPI
|
|
17
17
|
|
18
18
|
# Apply restrictions on query which selects objects from database.
|
19
19
|
# Most common usage is restrict user to access only objects he owns.
|
20
|
-
def restrict(
|
21
|
-
@restrict <<
|
20
|
+
def restrict(**kwargs)
|
21
|
+
@restrict << kwargs
|
22
22
|
end
|
23
23
|
|
24
24
|
# Restrict parameters client can set/change.
|
data/lib/haveapi/hooks.rb
CHANGED
@@ -84,7 +84,8 @@ module HaveAPI
|
|
84
84
|
# +opts+ is a hash and can have following keys:
|
85
85
|
# - desc - why this hook exists, when it's called
|
86
86
|
# - context - the context in which given blocks are called
|
87
|
-
# - args - hash of block arguments
|
87
|
+
# - args - hash of block positional arguments
|
88
|
+
# - kwargs - hash of block keyword arguments
|
88
89
|
# - initial - hash of initial values
|
89
90
|
# - ret - hash of return values
|
90
91
|
def self.register_hook(klass, name, opts = {})
|
@@ -138,6 +139,7 @@ module HaveAPI
|
|
138
139
|
# @param name [Symbol] hook name
|
139
140
|
# @param where [Class instance] class in whose context hooks are executed
|
140
141
|
# @param args [Array] an array of arguments passed to hooks
|
142
|
+
# @param kwargs [Hash] an array of arguments passed to hooks
|
141
143
|
# @param initial [Hash] initial return value
|
142
144
|
# @param instance [Boolean] call instance hooks or not; nil means auto-detect
|
143
145
|
def self.call_for(
|
@@ -145,6 +147,7 @@ module HaveAPI
|
|
145
147
|
name,
|
146
148
|
where = nil,
|
147
149
|
args: [],
|
150
|
+
kwargs: {},
|
148
151
|
initial: {},
|
149
152
|
instance: nil
|
150
153
|
)
|
@@ -165,10 +168,10 @@ module HaveAPI
|
|
165
168
|
|
166
169
|
hooks.each do |hook|
|
167
170
|
if where
|
168
|
-
ret = where.instance_exec(initial, *args, &hook)
|
171
|
+
ret = where.instance_exec(initial, *args, **kwargs, &hook)
|
169
172
|
|
170
173
|
else
|
171
|
-
ret = hook.call(initial, *args)
|
174
|
+
ret = hook.call(initial, *args, **kwargs)
|
172
175
|
end
|
173
176
|
|
174
177
|
initial.update(ret) if ret
|
@@ -201,48 +204,46 @@ module HaveAPI
|
|
201
204
|
end
|
202
205
|
|
203
206
|
# Call all hooks for +name+. see Hooks.call_for.
|
204
|
-
def call_hooks(*args)
|
205
|
-
Hooks.call_for(self.to_s, *args)
|
207
|
+
def call_hooks(*args, **kwargs)
|
208
|
+
Hooks.call_for(self.to_s, *args, **kwargs)
|
206
209
|
end
|
207
210
|
end
|
208
211
|
|
209
212
|
module InstanceMethods
|
210
213
|
# Call all instance and class hooks.
|
211
|
-
def call_hooks_for(*args)
|
212
|
-
ret = call_instance_hooks_for(*args)
|
214
|
+
def call_hooks_for(*args, **kwargs)
|
215
|
+
ret = call_instance_hooks_for(*args, **kwargs)
|
213
216
|
|
214
|
-
|
215
|
-
|
216
|
-
call_class_hooks_for(*args)
|
217
|
-
else
|
218
|
-
call_class_hooks_for(*args, initial: ret)
|
219
|
-
end
|
217
|
+
kwargs[:initial] = ret
|
218
|
+
call_class_hooks_for(*args, **kwargs)
|
220
219
|
end
|
221
220
|
|
222
221
|
# Call only instance hooks.
|
223
|
-
def call_instance_hooks_for(name, where = nil, args: [], initial: {})
|
224
|
-
Hooks.call_for(self, name, where, args: args, initial: initial)
|
222
|
+
def call_instance_hooks_for(name, where = nil, args: [], kwargs: {}, initial: {})
|
223
|
+
Hooks.call_for(self, name, where, args: args, kwargs: kwargs, initial: initial)
|
225
224
|
end
|
226
225
|
|
227
226
|
# Call only class hooks.
|
228
|
-
def call_class_hooks_for(name, where = nil, args: [], initial: {})
|
229
|
-
Hooks.call_for(self.class, name, where, args: args, initial: initial)
|
227
|
+
def call_class_hooks_for(name, where = nil, args: [], kwargs: {}, initial: {})
|
228
|
+
Hooks.call_for(self.class, name, where, args: args, kwargs: kwargs, initial: initial)
|
230
229
|
end
|
231
230
|
|
232
231
|
# Call hooks for different +klass+.
|
233
|
-
def call_hooks_as_for(klass, *args)
|
234
|
-
ret = call_instance_hooks_as_for(klass, *args)
|
235
|
-
|
232
|
+
def call_hooks_as_for(klass, *args, **kwargs)
|
233
|
+
ret = call_instance_hooks_as_for(klass, *args, **kwargs)
|
234
|
+
|
235
|
+
kwargs[:initial] = ret
|
236
|
+
call_class_hooks_as_for(klass.class, *args, **kwargs)
|
236
237
|
end
|
237
238
|
|
238
239
|
# Call only instance hooks for different +klass+.
|
239
|
-
def call_instance_hooks_as_for(klass, *args)
|
240
|
-
Hooks.call_for(klass, *args)
|
240
|
+
def call_instance_hooks_as_for(klass, *args, **kwargs)
|
241
|
+
Hooks.call_for(klass, *args, **kwargs)
|
241
242
|
end
|
242
243
|
|
243
244
|
# Call only class hooks for different +klass+.
|
244
|
-
def call_class_hooks_as_for(klass, *args)
|
245
|
-
Hooks.call_for(klass, *args)
|
245
|
+
def call_class_hooks_as_for(klass, *args, **kwargs)
|
246
|
+
Hooks.call_for(klass, *args, **kwargs)
|
246
247
|
end
|
247
248
|
|
248
249
|
# Connect instance level hook +name+ to +block+.
|
data/lib/haveapi/params.rb
CHANGED
@@ -78,52 +78,47 @@ module HaveAPI
|
|
78
78
|
@namespace = n.to_sym if n
|
79
79
|
end
|
80
80
|
|
81
|
-
def requires(
|
82
|
-
add_param(
|
81
|
+
def requires(name, **kwargs)
|
82
|
+
add_param(name, apply(kwargs, required: true))
|
83
83
|
end
|
84
84
|
|
85
|
-
def optional(
|
86
|
-
add_param(
|
85
|
+
def optional(name, **kwargs)
|
86
|
+
add_param(name, apply(kwargs, required: false))
|
87
87
|
end
|
88
88
|
|
89
|
-
def string(
|
90
|
-
add_param(
|
89
|
+
def string(name, **kwargs)
|
90
|
+
add_param(name, apply(kwargs, type: String))
|
91
91
|
end
|
92
92
|
|
93
|
-
def text(
|
94
|
-
add_param(
|
93
|
+
def text(name, **kwargs)
|
94
|
+
add_param(name, apply(kwargs, type: Text))
|
95
95
|
end
|
96
96
|
|
97
|
-
def password(
|
98
|
-
add_param(
|
97
|
+
def password(name, **kwargs)
|
98
|
+
add_param(name, apply(kwargs, type: String, protected: true))
|
99
99
|
end
|
100
100
|
|
101
|
-
def
|
102
|
-
|
101
|
+
def bool(name, **kwargs)
|
102
|
+
add_param(name, apply(kwargs, type: Boolean))
|
103
103
|
end
|
104
104
|
|
105
|
-
def
|
106
|
-
|
105
|
+
def integer(name, **kwargs)
|
106
|
+
add_param(name, apply(kwargs, type: Integer))
|
107
107
|
end
|
108
108
|
|
109
|
-
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
|
-
def integer(*args)
|
114
|
-
add_param(*apply(args, type: Integer))
|
115
|
-
end
|
109
|
+
alias_method :id, :integer
|
110
|
+
alias_method :foreign_key, :integer
|
116
111
|
|
117
|
-
def float(
|
118
|
-
add_param(
|
112
|
+
def float(name, **kwargs)
|
113
|
+
add_param(name, apply(kwargs, type: Float))
|
119
114
|
end
|
120
115
|
|
121
|
-
def datetime(
|
122
|
-
add_param(
|
116
|
+
def datetime(name, **kwargs)
|
117
|
+
add_param(name, apply(kwargs, type: Datetime))
|
123
118
|
end
|
124
119
|
|
125
|
-
def param(
|
126
|
-
add_param(
|
120
|
+
def param(name, **kwargs)
|
121
|
+
add_param(name, kwargs)
|
127
122
|
end
|
128
123
|
|
129
124
|
def use(name, include: nil, exclude: nil)
|
@@ -153,20 +148,20 @@ module HaveAPI
|
|
153
148
|
end
|
154
149
|
end
|
155
150
|
|
156
|
-
def resource(
|
157
|
-
add_resource(
|
151
|
+
def resource(name, **kwargs)
|
152
|
+
add_resource(name, **kwargs)
|
158
153
|
end
|
159
154
|
|
160
155
|
alias_method :references, :resource
|
161
156
|
alias_method :belongs_to, :resource
|
162
157
|
|
163
|
-
def patch(name, changes
|
158
|
+
def patch(name, **changes)
|
164
159
|
@params.detect { |p| p.name == name }.patch(changes)
|
165
160
|
end
|
166
161
|
|
167
162
|
# Action returns custom data.
|
168
|
-
def custom(
|
169
|
-
add_param(
|
163
|
+
def custom(name, **kwargs, &block)
|
164
|
+
add_param(name, apply(kwargs, type: Custom, clean: block))
|
170
165
|
end
|
171
166
|
|
172
167
|
def describe(context)
|
@@ -274,8 +269,8 @@ module HaveAPI
|
|
274
269
|
end
|
275
270
|
|
276
271
|
private
|
277
|
-
def add_param(
|
278
|
-
p = Parameters::Typed.new(
|
272
|
+
def add_param(name, kwargs)
|
273
|
+
p = Parameters::Typed.new(name, kwargs)
|
279
274
|
|
280
275
|
return if @include && !@include.include?(p.name)
|
281
276
|
return if @exclude && @exclude.include?(p.name)
|
@@ -283,8 +278,8 @@ module HaveAPI
|
|
283
278
|
@params << p unless param_exists?(p.name)
|
284
279
|
end
|
285
280
|
|
286
|
-
def add_resource(
|
287
|
-
r = Parameters::Resource.new(
|
281
|
+
def add_resource(name, kwargs)
|
282
|
+
r = Parameters::Resource.new(name, kwargs)
|
288
283
|
|
289
284
|
return if @include && !@include.include?(r.name)
|
290
285
|
return if @exclude && @exclude.include?(r.name)
|
@@ -296,10 +291,9 @@ module HaveAPI
|
|
296
291
|
!@params.detect { |p| p.name == name }.nil?
|
297
292
|
end
|
298
293
|
|
299
|
-
def apply(
|
300
|
-
|
301
|
-
|
302
|
-
args
|
294
|
+
def apply(kwargs, **default)
|
295
|
+
kwargs.update(default)
|
296
|
+
kwargs
|
303
297
|
end
|
304
298
|
|
305
299
|
def valid_layout?(params)
|
data/lib/haveapi/server.rb
CHANGED
@@ -40,7 +40,7 @@ module HaveAPI
|
|
40
40
|
return @current_user if @current_user
|
41
41
|
|
42
42
|
@current_user = settings.api_server.send(:do_authenticate, v, request)
|
43
|
-
settings.api_server.call_hooks_for(:post_authenticated, args: @current_user)
|
43
|
+
settings.api_server.call_hooks_for(:post_authenticated, args: [@current_user])
|
44
44
|
@current_user
|
45
45
|
end
|
46
46
|
|
data/lib/haveapi/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: haveapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.16.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jakub Skokan
|
@@ -44,28 +44,28 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '6.0'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '6.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: sinatra
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 2.
|
61
|
+
version: 2.2.0
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 2.
|
68
|
+
version: 2.2.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: tilt
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -142,14 +142,14 @@ dependencies:
|
|
142
142
|
requirements:
|
143
143
|
- - "~>"
|
144
144
|
- !ruby/object:Gem::Version
|
145
|
-
version: 0.
|
145
|
+
version: 0.16.0
|
146
146
|
type: :runtime
|
147
147
|
prerelease: false
|
148
148
|
version_requirements: !ruby/object:Gem::Requirement
|
149
149
|
requirements:
|
150
150
|
- - "~>"
|
151
151
|
- !ruby/object:Gem::Version
|
152
|
-
version: 0.
|
152
|
+
version: 0.16.0
|
153
153
|
- !ruby/object:Gem::Dependency
|
154
154
|
name: mail
|
155
155
|
requirement: !ruby/object:Gem::Requirement
|