Sha256: 72c6816b5905d9f9dcb2e34d3f02bbd7f2d91f37575e5675d450e20711df9c0c

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 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 = {})
          metaclass = class << self
            self
          end
          metaclass.instance_eval do
            define_method(name) do |*params|
              request_params = params.first || {}
              requestor.custom(name, options, request_params)
            end
          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)
            self.class.requestor.custom(name, options, request_params)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
json_api_client-1.0.0.beta5 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-1.0.0.beta4 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-1.0.0.beta3 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-1.0.0.beta2 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-1.0.0.beta lib/json_api_client/helpers/custom_endpoints.rb