Sha256: 56897a15e6ea972a5f7e21bd15432cc1aa166b31d27a6db88f4317301a5655cd

Contents?: true

Size: 1.11 KB

Versions: 12

Compression:

Stored size: 1.11 KB

Contents

module JsonApiClient
  module Helpers
    module CustomEndpoints
      extend ActiveSupport::Concern

      module ClassMethods
        def custom_endpoint(name, options = {})
          if :collection == options.delete(:on)
            collection_endpoint(name, options)
          else
            member_endpoint(name, options)
          end
        end

        def collection_endpoint(name, options = {})
          self.class.send(:define_method, name) do |*params|
            input = {
              name: name,
              params: request_params = params.first || {}
            }.merge(options)
            run_request(Query::Custom.new(self, input))
          end
        end

        def member_endpoint(name, options = {})
          define_method name do |*params|
            request_params = params.first || {}
            request_params[self.class.primary_key] = attributes.fetch(primary_key)
            input = {
              name: name, 
              params: request_params
            }.merge(options)
            run_request(Query::Custom.new(self.class, input))
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
json_api_client-0.7.0 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.6.0 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.5.1 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.5.0 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.4.0 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.3.1 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.3.0 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.2.4 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.2.3 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.2.2 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.2.1 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.2.0 lib/json_api_client/helpers/custom_endpoints.rb