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.



25
26
27
28
29
30
31
# File 'lib/haveapi/params.rb', line 25

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

Instance Attribute Details

#actionObject

Returns the value of attribute action.



23
24
25
# File 'lib/haveapi/params.rb', line 23

def action
  @action
end

#paramsObject (readonly)

Returns the value of attribute params.



22
23
24
# File 'lib/haveapi/params.rb', line 22

def params
  @params
end

Instance Method Details

#[](name) ⇒ Object



267
268
269
# File 'lib/haveapi/params.rb', line 267

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

#add_block(b) ⇒ Object



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

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.



202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/haveapi/params.rb', line 202

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



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/haveapi/params.rb', line 43

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.



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

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



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

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

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

  ret
end

#execObject



37
38
39
40
41
# File 'lib/haveapi/params.rb', line 37

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



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

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

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

#layout=(l) ⇒ Object



63
64
65
# File 'lib/haveapi/params.rb', line 63

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

#namespaceObject



67
68
69
70
71
72
73
74
# File 'lib/haveapi/params.rb', line 67

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



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

#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)

  if 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
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.



218
219
220
221
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
# File 'lib/haveapi/params.rb', line 218

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



192
193
194
195
196
197
198
# File 'lib/haveapi/params.rb', line 192

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

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