haveapi 0.14.2 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 05bae78d58ccbb9544d69219d3b34f1db107fc16931cbf0714d652ab8efb05f8
4
- data.tar.gz: db18c21a2d73446645d9eba73b8fec50f209a893733c45b69c7d89a33dc96e83
3
+ metadata.gz: 7d8c351c765bd18dc2ff8a33d5ad0e879e0e837e6c959435f24cd07168ac4d94
4
+ data.tar.gz: 3735e10f7f093a64715220405e4a8a69f769433d0ecd81e55fac048fdeb2e769
5
5
  SHA512:
6
- metadata.gz: 69549de1aafe12d346343e1061c5c1513a56cb9d9b7f4f6452afb87d78764febb4a378805c69f55f9bd32db47e73b0b3680a4077bb055b295cc2b64db6935794
7
- data.tar.gz: ed988016903b8d341118c42986e2786bd34609109dcbba31fde6097c9dd42e2370731503ee25498252cfe66b35dc0cd9360b67550e33830f7a33c3bf8d447676
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.14.2'
26
+ s.add_runtime_dependency 'haveapi-client', '~> 0.15.0'
27
27
  s.add_runtime_dependency 'mail'
28
28
  end
@@ -464,11 +464,11 @@ module HaveAPI
464
464
  end
465
465
 
466
466
  protected
467
- def with_restricted(*args)
468
- if args.empty?
467
+ def with_restricted(**kwargs)
468
+ if kwargs.empty?
469
469
  @authorization.restrictions
470
470
  else
471
- args.first.update(@authorization.restrictions)
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(*args)
21
- @restrict << args.first
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
- if args.last.is_a?(::Hash)
215
- args.last.update(initial: ret)
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
- call_class_hooks_as_for(klass.class, *args, initial: ret)
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+.
@@ -78,52 +78,47 @@ module HaveAPI
78
78
  @namespace = n.to_sym if n
79
79
  end
80
80
 
81
- def requires(*args)
82
- add_param(*apply(args, required: true))
81
+ def requires(name, **kwargs)
82
+ add_param(name, apply(kwargs, required: true))
83
83
  end
84
84
 
85
- def optional(*args)
86
- add_param(*apply(args, required: false))
85
+ def optional(name, **kwargs)
86
+ add_param(name, apply(kwargs, required: false))
87
87
  end
88
88
 
89
- def string(*args)
90
- add_param(*apply(args, type: String))
89
+ def string(name, **kwargs)
90
+ add_param(name, apply(kwargs, type: String))
91
91
  end
92
92
 
93
- def text(*args)
94
- add_param(*apply(args, type: Text))
93
+ def text(name, **kwargs)
94
+ add_param(name, apply(kwargs, type: Text))
95
95
  end
96
96
 
97
- def password(*args)
98
- add_param(*apply(args, type: String, protected: true))
97
+ def password(name, **kwargs)
98
+ add_param(name, apply(kwargs, type: String, protected: true))
99
99
  end
100
100
 
101
- def id(*args)
102
- integer(*args)
101
+ def bool(name, **kwargs)
102
+ add_param(name, apply(kwargs, type: Boolean))
103
103
  end
104
104
 
105
- def foreign_key(*args)
106
- integer(*args)
105
+ def integer(name, **kwargs)
106
+ add_param(name, apply(kwargs, type: Integer))
107
107
  end
108
108
 
109
- def bool(*args)
110
- add_param(*apply(args, type: Boolean))
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(*args)
118
- add_param(*apply(args, type: Float))
112
+ def float(name, **kwargs)
113
+ add_param(name, apply(kwargs, type: Float))
119
114
  end
120
115
 
121
- def datetime(*args)
122
- add_param(*apply(args, type: Datetime))
116
+ def datetime(name, **kwargs)
117
+ add_param(name, apply(kwargs, type: Datetime))
123
118
  end
124
119
 
125
- def param(*args)
126
- add_param(*args)
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(*args)
157
- add_resource(*args)
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(*args, &block)
169
- add_param(*apply(args, type: Custom, clean: block))
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(*args)
278
- p = Parameters::Typed.new(*args)
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(*args)
287
- r = Parameters::Resource.new(*args)
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(args, default)
300
- args << {} unless args.last.is_a?(Hash)
301
- args.last.update(default)
302
- args
294
+ def apply(kwargs, **default)
295
+ kwargs.update(default)
296
+ kwargs
303
297
  end
304
298
 
305
299
  def valid_layout?(params)
@@ -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
 
@@ -1,4 +1,4 @@
1
1
  module HaveAPI
2
2
  PROTOCOL_VERSION = '2.0'
3
- VERSION = '0.14.2'
3
+ VERSION = '0.15.0'
4
4
  end
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.14.2
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.14.2
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.14.2
152
+ version: 0.15.0
153
153
  - !ruby/object:Gem::Dependency
154
154
  name: mail
155
155
  requirement: !ruby/object:Gem::Requirement