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



275
276
277
# File 'lib/haveapi/params.rb', line 275

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



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

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.



211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/haveapi/params.rb', line 211

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.



170
171
172
# File 'lib/haveapi/params.rb', line 170

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

#datetime(name, **kwargs) ⇒ Object



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

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

#describe(context) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/haveapi/params.rb', line 174

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



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

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

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



105
106
107
# File 'lib/haveapi/params.rb', line 105

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
70
71
72
73
74
# 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 = if %i[object_list hash_list].include?(layout)
        n.pluralize
      else
        n.singularize
      end

  @cache[:namespace] = n.to_sym
end

#namespace=(n) ⇒ Object



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

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

#optional(name, **kwargs) ⇒ Object



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

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

#param(name, **kwargs) ⇒ Object



120
121
122
# File 'lib/haveapi/params.rb', line 120

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

#password(name, **kwargs) ⇒ Object



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

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

#patch(name, **changes) ⇒ Object



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

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

#remove(name) ⇒ Object



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

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



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

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

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



151
152
153
# File 'lib/haveapi/params.rb', line 151

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

#string(name, **kwargs) ⇒ Object



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

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

#text(name, **kwargs) ⇒ Object



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

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

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/haveapi/params.rb', line 124

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.



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
269
270
271
272
273
# File 'lib/haveapi/params.rb', line 227

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



201
202
203
204
205
206
207
# File 'lib/haveapi/params.rb', line 201

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

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