class Apisync::Resource

Public Class Methods

new(name, options = {}) click to toggle source
# File lib/apisync/resource.rb, line 3
def initialize(name, options = {})
  @name = name
  @options = options
end

Public Instance Methods

get(conditions) click to toggle source

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' }})
# 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) click to toggle source

Saves a resource.

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

# 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

Private Instance Methods

post(data, headers: {}) click to toggle source
# File lib/apisync/resource.rb, line 49
def post(data, headers: {})
  client = Apisync::HttpClient.new(
    resource_name: @name,
    options: @options
  )
  client.post(
    data: data,
    headers: headers
  )
end
put(data, headers: {}) click to toggle source
# File lib/apisync/resource.rb, line 60
def put(data, headers: {})
  client = Apisync::HttpClient.new(
    resource_name: @name,
    options: @options
  )
  client.put(
    id: data[:id],
    data: data,
    headers: headers
  )
end