Class: ApipieBindings::Action
- Inherits:
-
Object
- Object
- ApipieBindings::Action
- Defined in:
- lib/apipie_bindings/action.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #add_to_path(path, *additions) ⇒ Object
- #apidoc ⇒ Object
- #call(params = {}, headers = {}, options = {}) ⇒ Object
- #examples ⇒ Object
- #find_route(params = {}) ⇒ Object
-
#initialize(resource, name, api) ⇒ Action
constructor
A new instance of Action.
- #inspect ⇒ Object
- #params ⇒ Object
- #routes ⇒ Object
- #to_s ⇒ Object
- #validate(params, values, path = nil) ⇒ Object
- #validate!(parameters) ⇒ Object
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/apipie_bindings/action.rb', line 5 def name @name 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 |
#apidoc ⇒ Object
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={}, ={}) @api.call(@resource, @name, params, headers, ) end |
#examples ⇒ Object
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 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.keys.map(&:to_s).include?(path_param) } end suitable_route ||= sorted_routes.last return suitable_route end |
#inspect ⇒ Object
114 115 116 |
# File 'lib/apipie_bindings/action.rb', line 114 def inspect to_s end |
#params ⇒ Object
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 |
#routes ⇒ Object
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_s ⇒ Object
110 111 112 |
# File 'lib/apipie_bindings/action.rb', line 110 def to_s "<Action :#{@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 |