Sha256: 7567e4ea43d243eeac2e2e9f0aa4120b13c2ba67b110a6c2dfa3607d59c291ec

Contents?: true

Size: 1.22 KB

Versions: 9

Compression:

Stored size: 1.22 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|
              input = {
                name: name,
                params: request_params = params.first || {}
              }.merge(options)
              run_request(Query::Custom.new(self, input))
            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)
            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

9 entries across 9 versions & 1 rubygems

Version Path
json_api_client-0.9.6 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.9.5 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.9.4 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.9.3 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.9.2 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.9.0 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.8.1 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.8.0 lib/json_api_client/helpers/custom_endpoints.rb
json_api_client-0.7.1 lib/json_api_client/helpers/custom_endpoints.rb