Class: HaveAPI::Client::Params

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, data) ⇒ Params

Returns a new instance of Params.



7
8
9
10
11
12
13
# File 'lib/haveapi/client/params.rb', line 7

def initialize(action, data)
  @action = action
  @data = data
  @params = {}
  @errors = {}
  coerce
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/haveapi/client/params.rb', line 5

def errors
  @errors
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/haveapi/client/params.rb', line 5

def params
  @params
end

Instance Method Details

#coerceObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/haveapi/client/params.rb', line 15

def coerce
  @action.input_params.each do |name, p|
    next unless @data.has_key?(name)

    @params[name] = if p[:type] == 'Resource'
                      Parameters::Resource.new(self, p, @data[name])

                    else
                      Parameters::Typed.new(self, p, @data[name])
                    end
  end
end

#to_apiObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/haveapi/client/params.rb', line 47

def to_api
  ret = {}

  @params.each do |name, p|
    ret[name] = p.to_api
  end

  ret[:meta] = @data[:meta] if @data.has_key?(:meta)

  ret
end

#valid?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/haveapi/client/params.rb', line 28

def valid?
  @action.input_params.each do |name, p|
    next if p[:validators].nil?

    if p[:validators][:presence] && @params[name].nil?
      error(name, 'required parameter missing')

    elsif @params[name].nil?
      next
    end

    unless @params[name].valid?
      error(name, @params[name].errors)
    end
  end

  @errors.empty?
end