Class: HaveAPI::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/params.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(direction, action) ⇒ Params

Returns a new instance of Params.



20
21
22
23
24
25
26
# File 'lib/haveapi/params.rb', line 20

def initialize(direction, action)
  @direction = direction
  @params = []
  @action = action
  @cache = {}
  @blocks = []
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



18
19
20
# File 'lib/haveapi/params.rb', line 18

def action
  @action
end

#paramsObject (readonly)

Returns the value of attribute params.



17
18
19
# File 'lib/haveapi/params.rb', line 17

def params
  @params
end

Instance Method Details

#[](name) ⇒ Object



270
271
272
# File 'lib/haveapi/params.rb', line 270

def [](name)
  @params.detect { |p| p.name == name }
end

#add_block(b) ⇒ Object



28
29
30
# File 'lib/haveapi/params.rb', line 28

def add_block(b)
  @blocks << b
end

#bool(name, **kwargs) ⇒ Object



96
97
98
# File 'lib/haveapi/params.rb', line 96

def bool(name, **kwargs)
  add_param(name, apply(kwargs, type: Boolean))
end

#check_layout(params) ⇒ Object

First step of validation. Check if input is in correct namespace and has a correct layout.



206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/haveapi/params.rb', line 206

def check_layout(params)
  if (params[namespace].nil? || !valid_layout?(params)) && any_required_params?
    raise ValidationError.new('invalid input layout', {})
  end

  case layout
  when :object, :hash
    params[namespace] ||= {}

  when :object_list, :hash_list
    params[namespace] ||= []
  end
end

#cloneObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/haveapi/params.rb', line 38

def clone
  obj = super
  params = @params
  blocks = @blocks

  obj.instance_eval do
    @params = params.dup
    @cache = {}
    @blocks = blocks.dup
  end

  obj
end

#custom(name, **kwargs, &block) ⇒ Object

Action returns custom data.



165
166
167
# File 'lib/haveapi/params.rb', line 165

def custom(name, **kwargs, &block)
  add_param(name, apply(kwargs, type: Custom, clean: block))
end

#datetime(name, **kwargs) ⇒ Object



111
112
113
# File 'lib/haveapi/params.rb', line 111

def datetime(name, **kwargs)
  add_param(name, apply(kwargs, type: Datetime))
end

#describe(context) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/haveapi/params.rb', line 169

def describe(context)
  context.layout = layout

  ret = { parameters: {} }
  ret[:layout] = layout
  ret[:namespace] = namespace
  ret[:format] = @structure if @structure

  @params.each do |p|
    ret[:parameters][p.name] = p.describe(context)
  end

  ret[:parameters] = if @direction == :input
                       context.authorization.filter_input(
                         @params,
                         ModelAdapters::Hash.output(context, ret[:parameters])
                       )
                     else
                       context.authorization.filter_output(
                         @params,
                         ModelAdapters::Hash.output(context, ret[:parameters])
                       )
                     end

  ret
end

#execObject



32
33
34
35
36
# File 'lib/haveapi/params.rb', line 32

def exec
  @blocks.each do |b|
    instance_exec(&b)
  end
end

#float(name, **kwargs) ⇒ Object



107
108
109
# File 'lib/haveapi/params.rb', line 107

def float(name, **kwargs)
  add_param(name, apply(kwargs, type: Float))
end

#integer(name, **kwargs) ⇒ Object Also known as: id, foreign_key



100
101
102
# File 'lib/haveapi/params.rb', line 100

def integer(name, **kwargs)
  add_param(name, apply(kwargs, type: Integer))
end

#layoutObject



52
53
54
55
56
# File 'lib/haveapi/params.rb', line 52

def layout
  return @cache[:layout] if @cache[:layout]

  @cache[:layout] = @layout || :object
end

#layout=(l) ⇒ Object



58
59
60
# File 'lib/haveapi/params.rb', line 58

def layout=(l)
  @layout = l if l
end

#namespaceObject



62
63
64
65
66
67
68
69
# File 'lib/haveapi/params.rb', line 62

def namespace
  return @cache[:namespace] unless @cache[:namespace].nil?
  return @cache[:namespace] = @namespace unless @namespace.nil?

  n = @action.resource.resource_name.underscore
  n = n.pluralize if %i[object_list hash_list].include?(layout)
  @cache[:namespace] = n.to_sym
end

#namespace=(n) ⇒ Object



71
72
73
74
# File 'lib/haveapi/params.rb', line 71

def namespace=(n)
  @namespace = false if n === false
  @namespace = n.to_sym if n
end

#optional(name, **kwargs) ⇒ Object



80
81
82
# File 'lib/haveapi/params.rb', line 80

def optional(name, **kwargs)
  add_param(name, apply(kwargs, required: false))
end

#param(name, **kwargs) ⇒ Object



115
116
117
# File 'lib/haveapi/params.rb', line 115

def param(name, **kwargs)
  add_param(name, kwargs)
end

#password(name, **kwargs) ⇒ Object



92
93
94
# File 'lib/haveapi/params.rb', line 92

def password(name, **kwargs)
  add_param(name, apply(kwargs, type: String, protected: true))
end

#patch(name, **changes) ⇒ Object



153
154
155
# File 'lib/haveapi/params.rb', line 153

def patch(name, **changes)
  @params.detect { |p| p.name == name }.patch(changes)
end

#remove(name) ⇒ Object



157
158
159
160
161
162
# File 'lib/haveapi/params.rb', line 157

def remove(name)
  i = @params.index { |p| p.name == name }
  raise "Parameter #{name.inspect} not found" if i.nil?

  @params.delete_at(i)
end

#requires(name, **kwargs) ⇒ Object



76
77
78
# File 'lib/haveapi/params.rb', line 76

def requires(name, **kwargs)
  add_param(name, apply(kwargs, required: true))
end

#resource(name, **kwargs) ⇒ Object Also known as: references, belongs_to



146
147
148
# File 'lib/haveapi/params.rb', line 146

def resource(name, **kwargs)
  add_resource(name, kwargs)
end

#string(name, **kwargs) ⇒ Object



84
85
86
# File 'lib/haveapi/params.rb', line 84

def string(name, **kwargs)
  add_param(name, apply(kwargs, type: String))
end

#text(name, **kwargs) ⇒ Object



88
89
90
# File 'lib/haveapi/params.rb', line 88

def text(name, **kwargs)
  add_param(name, apply(kwargs, type: Text))
end

#use(name, include: nil, exclude: nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/haveapi/params.rb', line 119

def use(name, include: nil, exclude: nil)
  @include_back ||= []
  @exclude_back ||= []

  block = @action.resource.params(name)

  return unless block

  @include_back << @include.clone if @include
  @exclude_back << @exclude.clone if @exclude

  if include
    @include ||= []
    @include.concat(include)
  end

  if exclude
    @exclude ||= []
    @exclude.concat(exclude)
  end

  instance_eval(&block)

  @include = @include_back.pop if @include
  @exclude = @exclude_back.pop if @exclude
end

#validate(params) ⇒ Object

Third step of validation. Check if all required params are present, convert params to correct data types, set default values if necessary.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/haveapi/params.rb', line 222

def validate(params)
  errors = {}

  layout_aware(params) do |input|
    # First run - coerce values to correct types
    @params.each do |p|
      if p.required? && input[p.name].nil?
        errors[p.name] = ['required parameter missing']
        next
      end

      unless input.has_key?(p.name)
        input[p.name] = p.default if p.respond_to?(:fill?) && p.fill?
        next
      end

      begin
        cleaned = p.clean(input[p.name])
      rescue ValidationError => e
        errors[p.name] ||= []
        errors[p.name] << e.message
        next
      end

      input[p.name] = cleaned if cleaned != :_nil
    end

    # Second run - validate parameters
    @params.each do |p|
      next if errors.has_key?(p.name)
      next if input[p.name].nil?

      res = p.validate(input[p.name], input)

      unless res === true
        errors[p.name] ||= []
        errors[p.name].concat(res)
      end
    end
  end

  unless errors.empty?
    raise ValidationError.new('input parameters not valid', errors)
  end

  params
end

#validate_buildObject



196
197
198
199
200
201
202
# File 'lib/haveapi/params.rb', line 196

def validate_build
  m = :"validate_build_#{@direction}"

  @params.each do |p|
    p.send(m) if p.respond_to?(m)
  end
end