Class: JSONAPI::Consumer::Query::Requestor

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

Instance Method Summary collapse

Methods included from Helpers::URI

#encode_part

Constructor Details

#initialize(klass, path = nil) ⇒ Requestor

Returns a new instance of Requestor.



7
8
9
10
# File 'lib/jsonapi/consumer/query/requestor.rb', line 7

def initialize(klass, path = nil)
  @klass = klass
  @path = path
end

Instance Method Details

#create(record) ⇒ Object

expects a record



13
14
15
16
17
# File 'lib/jsonapi/consumer/query/requestor.rb', line 13

def create(record)
  request(:post, klass.path(record.attributes), {
    data: record.as_json_api
  })
end

#custom(method_name, options, params) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/jsonapi/consumer/query/requestor.rb', line 39

def custom(method_name, options, params)
  path = resource_path(params)
  params.delete(klass.primary_key)
  path = File.join(path, method_name.to_s)

  request(options.fetch(:request_method, :get), path, params)
end

#destroy(record) ⇒ Object



31
32
33
# File 'lib/jsonapi/consumer/query/requestor.rb', line 31

def destroy(record)
  request(:delete, resource_path(record.attributes), {})
end

#get(params = {}) ⇒ Object



25
26
27
28
29
# File 'lib/jsonapi/consumer/query/requestor.rb', line 25

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

#linked(path) ⇒ Object



35
36
37
# File 'lib/jsonapi/consumer/query/requestor.rb', line 35

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

#update(record) ⇒ Object



19
20
21
22
23
# File 'lib/jsonapi/consumer/query/requestor.rb', line 19

def update(record)
  request(:patch, resource_path(record.attributes), {
    data: record.as_json_api
  })
end