Class: JsonApiClient::Query::Requestor

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Helpers::URI
Defined in:
lib/json_api_client/query/requestor.rb

Instance Method Summary collapse

Methods included from Helpers::URI

#encode_part

Constructor Details

#initialize(klass) ⇒ Requestor

Returns a new instance of Requestor.



7
8
9
# File 'lib/json_api_client/query/requestor.rb', line 7

def initialize(klass)
  @klass = klass
end

Instance Method Details

#create(record) ⇒ Object

expects a record



12
13
14
15
16
17
18
19
# File 'lib/json_api_client/query/requestor.rb', line 12

def create(record)
  request(
    :post,
    klass.path(record.path_attributes),
    body: { data: record.as_json_api },
    params: record.request_params.to_params
  )
end

#custom(method_name, options, params) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/json_api_client/query/requestor.rb', line 44

def custom(method_name, options, params)
  path = resource_path(params)
  params.delete(klass.primary_key)
  path = File.join(path, method_name.to_s)
  request_method = options.fetch(:request_method, :get).to_sym
  query_params, body_params = [:get, :delete].include?(request_method) ? [params, nil] : [nil, params]
  request(request_method, path, params: query_params, body: body_params)
end

#destroy(record) ⇒ Object



36
37
38
# File 'lib/json_api_client/query/requestor.rb', line 36

def destroy(record)
  request(:delete, resource_path(record.path_attributes))
end

#get(params = {}) ⇒ Object



30
31
32
33
34
# File 'lib/json_api_client/query/requestor.rb', line 30

def get(params = {})
  path = resource_path(params)
  params.delete(klass.primary_key)
  request(:get, path, params: params)
end

#linked(path) ⇒ Object



40
41
42
# File 'lib/json_api_client/query/requestor.rb', line 40

def linked(path)
  request(:get, path)
end

#update(record) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/json_api_client/query/requestor.rb', line 21

def update(record)
  request(
    :patch,
    resource_path(record.path_attributes),
    body: { data: record.as_json_api },
    params: record.request_params.to_params
  )
end