Class: MyTargetApi::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/my_target_api/resource.rb

Overview

Reflects api resource

Instance Method Summary collapse

Constructor Details

#initialize(api, path) ⇒ Resource

Returns a new instance of Resource.



7
8
9
10
# File 'lib/my_target_api/resource.rb', line 7

def initialize(api, path)
  @api = api
  @path = path
end

Instance Method Details

#create(params = {}) ⇒ Object



12
13
14
15
16
# File 'lib/my_target_api/resource.rb', line 12

def create(params = {})
  params = prepare_params(params)

  api.post_request("#{path}.json", params)
end

#delete(params = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/my_target_api/resource.rb', line 36

def delete(params = {})
  params = prepare_params(params)
  id = params.delete(:id)

  api.delete_request("#{path}/#{id}.json", params)
end

#read(params = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/my_target_api/resource.rb', line 18

def read(params = {})
  params = prepare_params(params)
  id = params.delete(:id)

  if id
    api.get_request("#{path}/#{id}.json", params)
  else
    api.get_request("#{path}.json", params)
  end
end

#resource(relative_path) ⇒ Object



43
44
45
# File 'lib/my_target_api/resource.rb', line 43

def resource(relative_path)
  MyTargetApi::Resource.new(api, "#{path}/#{relative_path}")
end

#update(params = {}) ⇒ Object



29
30
31
32
33
34
# File 'lib/my_target_api/resource.rb', line 29

def update(params = {})
  params = prepare_params(params)
  id = params.delete(:id)

  api.post_request("#{path}/#{id}.json", params)
end