haveapi 0.14.2 → 0.15.0
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 +4 -4
- data/haveapi.gemspec +1 -1
- 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 +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d8c351c765bd18dc2ff8a33d5ad0e879e0e837e6c959435f24cd07168ac4d94
|
4
|
+
data.tar.gz: 3735e10f7f093a64715220405e4a8a69f769433d0ecd81e55fac048fdeb2e769
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eef44b308fd207bed35e45cba52b2efc63c8f3404804d054fc91f276ddf2d9c648f2cb9f4b8063532e08e9938c28d9592176cec0e16c53c16ef4cbfaffd9e9d9
|
7
|
+
data.tar.gz: 5b8a1c07a524dbfd639eae3748cd8fa5603e72c0830f8330fd9682664152509e0fc32455a627f6a86ffa8e2da04d5046ad1220d3dda53836bd39eb3f46ce3c35
|
data/haveapi.gemspec
CHANGED
@@ -23,6 +23,6 @@ Gem::Specification.new do |s|
|
|
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.15.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.15.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jakub Skokan
|
@@ -142,14 +142,14 @@ dependencies:
|
|
142
142
|
requirements:
|
143
143
|
- - "~>"
|
144
144
|
- !ruby/object:Gem::Version
|
145
|
-
version: 0.
|
145
|
+
version: 0.15.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.15.0
|
153
153
|
- !ruby/object:Gem::Dependency
|
154
154
|
name: mail
|
155
155
|
requirement: !ruby/object:Gem::Requirement
|