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



272
273
274
# File 'lib/haveapi/params.rb', line 272

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(*args) ⇒ Object



109
110
111
# File 'lib/haveapi/params.rb', line 109

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

#check_layout(params) ⇒ Object

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



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

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(*args, &block) ⇒ Object

Action returns custom data.



168
169
170
# File 'lib/haveapi/params.rb', line 168

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

#datetime(*args) ⇒ Object



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

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

#describe(context) ⇒ Object



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

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(*args) ⇒ Object



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

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

#foreign_key(*args) ⇒ Object



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

def foreign_key(*args)
  integer(*args)
end

#id(*args) ⇒ Object



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

def id(*args)
  integer(*args)
end

#integer(*args) ⇒ Object



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

def integer(*args)
  add_param(*apply(args, 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(*args) ⇒ Object



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

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

#param(*args) ⇒ Object



125
126
127
# File 'lib/haveapi/params.rb', line 125

def param(*args)
  add_param(*args)
end

#password(*args) ⇒ Object



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

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

#patch(name, changes = {}) ⇒ Object



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

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

#requires(*args) ⇒ Object



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

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

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



156
157
158
# File 'lib/haveapi/params.rb', line 156

def resource(*args)
  add_resource(*args)
end

#string(*args) ⇒ Object



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

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

#text(*args) ⇒ Object



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

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

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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/haveapi/params.rb', line 129

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.



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

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



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

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

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