Module: JsonApiClient::Helpers::CustomEndpoints::ClassMethods

Defined in:
lib/json_api_client/helpers/custom_endpoints.rb

Instance Method Summary collapse

Instance Method Details

#collection_endpoint(name, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/json_api_client/helpers/custom_endpoints.rb', line 15

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

#custom_endpoint(name, options = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/json_api_client/helpers/custom_endpoints.rb', line 7

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

#member_endpoint(name, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/json_api_client/helpers/custom_endpoints.rb', line 30

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