Class: Apisync::Resource

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

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Resource

Returns a new instance of Resource.



3
4
5
6
# File 'lib/apisync/resource.rb', line 3

def initialize(name, options = {})
  @name = name
  @options = options
end

Instance Method Details

#get(conditions) ⇒ Object

Returns all resources that match the conditions passed in.

  1. To find a resource by its id:

get(id: 'customer-id')
  1. To find a resource by a column value

get(filters: {column_name: 'customer-id' }})


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/apisync/resource.rb', line 34

def get(conditions)
  client = Apisync::HttpClient.new(
    resource_name: @name,
    options: @options
  )
  client.get(
    id: conditions[:id],
    filters: conditions[:filters],
    headers: conditions[:headers] || {}
  )
end

#save(data) ⇒ Object

Saves a resource.

When the resource has an id in ‘data`, a `PUT` request is done. Otherwise a `POST` takes place.



13
14
15
16
17
18
19
20
21
22
# File 'lib/apisync/resource.rb', line 13

def save(data)
  data[:type] = @name.to_s.gsub("_", "-")
  headers = data.delete(:headers) || {}

  if data[:id].nil?
    post(data, headers: headers)
  else
    put(data, headers: headers)
  end
end