Sha256: b4e1b2f76fd1da851b3f4170325fa40b42bc574da6682f134fafb7848aa483d1

Contents?: true

Size: 1.77 KB

Versions: 8

Compression:

Stored size: 1.77 KB

Contents

module ApipieBindings

  class Action

    attr_reader :name

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

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

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

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

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

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

    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

    def validate!(params)
      # return unless params.is_a?(Hash)

      # invalid_keys = params.keys.map(&:to_s) - (rules.is_a?(Hash) ? rules.keys : rules)
      # raise ArgumentError, "Invalid keys: #{invalid_keys.join(", ")}" unless invalid_keys.empty?

      # if rules.is_a? Hash
      #   rules.each do |key, sub_keys|
      #     validate_params!(params[key], sub_keys) if params[key]
      #   end
      # end
    end

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

    def inspect
      to_s
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
apipie-bindings-0.0.9 lib/apipie_bindings/action.rb
apipie-bindings-0.0.8 lib/apipie_bindings/action.rb
apipie-bindings-0.0.7 lib/apipie_bindings/action.rb
apipie-bindings-0.0.6 lib/apipie_bindings/action.rb
apipie-bindings-0.0.5 lib/apipie_bindings/action.rb
apipie-bindings-0.0.4 lib/apipie_bindings/action.rb
apipie-bindings-0.0.3 lib/apipie_bindings/action.rb
apipie-bindings-0.0.2 lib/apipie_bindings/action.rb