Class: ApipieBindings::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/apipie_bindings/action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, name, api) ⇒ Action

Returns a new instance of Action.



7
8
9
10
11
# File 'lib/apipie_bindings/action.rb', line 7

def initialize(resource, name, api)
  @resource = resource
  @name = name.to_sym
  @api = api
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/apipie_bindings/action.rb', line 5

def name
  @name
end

#resourceObject (readonly)

Returns the value of attribute resource.



5
6
7
# File 'lib/apipie_bindings/action.rb', line 5

def resource
  @resource
end

Instance Method Details

#add_to_path(path, *additions) ⇒ Object



105
106
107
108
# File 'lib/apipie_bindings/action.rb', line 105

def add_to_path(path, *additions)
  path ||= ''
  additions.inject(path) { |new_path, add| new_path.empty? ? "#{add}" : "#{new_path}[#{add}]" }
end

#apidocObject



17
18
19
20
21
22
# File 'lib/apipie_bindings/action.rb', line 17

def apidoc
  methods = @api.apidoc[:docs][:resources][@resource][:methods].select do |action|
    action[:name].to_sym == @name
  end
  methods.first
end

#call(params = {}, headers = {}, options = {}) ⇒ Object



13
14
15
# File 'lib/apipie_bindings/action.rb', line 13

def call(params={}, headers={}, options={})
  @api.call(@resource, @name, params, headers, options)
end

#examplesObject



41
42
43
44
45
# File 'lib/apipie_bindings/action.rb', line 41

def examples
  apidoc[:examples].map do |example|
    ApipieBindings::Example.parse(example)
  end.compact
end

#find_route(params = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/apipie_bindings/action.rb', line 47

def find_route(params={})
  sorted_routes = routes.sort_by { |r| [-1 * r.params_in_path.count, r.path] }

  suitable_route = sorted_routes.find do |route|
    route.params_in_path.all? { |path_param| !params[path_param.to_sym].nil? || !params[path_param].nil? }
  end

  suitable_route ||= sorted_routes.last
  return suitable_route
end

#inspectObject



114
115
116
# File 'lib/apipie_bindings/action.rb', line 114

def inspect
  to_s
end

#paramsObject



31
32
33
34
35
36
37
38
39
# File 'lib/apipie_bindings/action.rb', line 31

def params
  if apidoc
    apidoc[:params].map do |param|
      ApipieBindings::Param.new(param)
    end
  else
    []
  end
end

#routesObject



24
25
26
27
28
29
# File 'lib/apipie_bindings/action.rb', line 24

def routes
  apidoc[:apis].map do |api|
    ApipieBindings::Route.new(
      api[:api_url], api[:http_method], api[:short_description])
  end
end

#to_sObject



110
111
112
# File 'lib/apipie_bindings/action.rb', line 110

def to_s
  "<Action #{@resource}:#{@name}>"
end

#validate(params, values, path = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/apipie_bindings/action.rb', line 73

def validate(params, values, path=nil)
  return [ErrorData.new(:invalid_type, path, 'Hash')] unless values.respond_to?(:keys)
  # check required
  required_keys = params.select(&:required?).map(&:name)
  given_keys = values.keys.select { |par| !values[par].nil? }.map(&:to_s)
  missing_params = required_keys - given_keys
  errors = missing_params.map { |p| ErrorData.new(:missing_argument, add_to_path(path, p)) }

  # check individuals one by one
  values.each do |param, value|
    param_description = params.find { |p| p.name == param.to_s }
    if param_description

      # nested?
      if !param_description.params.empty? && !value.nil?
        # array
        if param_description.expected_type == :array
          value.each.with_index do |item, i|
            errors += validate(param_description.params, item, add_to_path(path, param_description.name, i))
          end
        end
        # hash
        if param_description.expected_type == :hash
          errors += validate(param_description.params, value, add_to_path(path, param_description.name))
        end
      end
    end
  end

  errors
end

#validate!(parameters) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/apipie_bindings/action.rb', line 58

def validate!(parameters)
  errors = validate(params, parameters)

  missing_arguments, errors = errors.partition { |e| e.kind == :missing_argument }
  missing_arguments.map! { |e| e.argument }
  raise ApipieBindings::MissingArgumentsError.new(missing_arguments) unless missing_arguments.empty?

  invalid_types, errors = errors.partition { |e| e.kind == :invalid_type }
  invalid_types.map! { |e| [e.argument, e.details] }
  raise ApipieBindings::InvalidArgumentTypesError.new(invalid_types) unless invalid_types.empty?

  errors.map! { |e| e.argument }
  raise ApipieBindings::ValidationError.new(errors) unless errors.empty?
end