class Zype::BaseModel

Constants

ACCEPTED_KEYS

Attributes

client[R]
path[R]

Public Class Methods

new(auth_method='api_key') click to toggle source
# File lib/zype/base_model.rb, line 7
def initialize(auth_method='api_key')
  @client = client_class.new(auth_method)
  @path = generate_path
end

Public Instance Methods

all(params: {}) click to toggle source

Returns all objects for given class

@param params [Hash] the metadata to filter objects by @return [Array<Hash>] the objects returned from the API

# File lib/zype/base_model.rb, line 16
def all(params: {})
  client.execute(method: :get, path: "/#{path}", params: params)
end
auth=(auth_method) click to toggle source

Sets the authentication method for calling the API

@param auth_method [String] one of 'api_key' or 'app_key'

# File lib/zype/base_model.rb, line 56
def auth=(auth_method)
  raise InvalidKey unless ACCEPTED_KEYS.include?(auth_method.to_sym)

  @client = client_class.new(auth_method)
end
create(params:) click to toggle source

Creates a new object via the API

@param params [Hash] the properties of the object @return [Hash] the newly created object

# File lib/zype/base_model.rb, line 32
def create(params:)
  client.execute(method: :post, path: "/#{path}", params: params)
end
delete(id:) click to toggle source

Deletes an existing object via the API

@param id [String] the ID of the object @return [Hash] the deleted object

# File lib/zype/base_model.rb, line 49
def delete(id:)
  client.execute(method: :delete, path: "/#{path}/#{id}")
end
find(id:) click to toggle source

Returns object matching ID

@param id [String] the ID of the object @return [Hash] the object returned from the API

# File lib/zype/base_model.rb, line 24
def find(id:)
  client.execute(method: :get, path: "/#{path}/#{id}")
end
update(id:, params:) click to toggle source

Updates an existing object via the API

@param id [String] the ID of the object @param params [Hash] the properties to be updated @return [Hash] the updated object

# File lib/zype/base_model.rb, line 41
def update(id:, params:)
  client.execute(method: :put, path: "/#{path}/#{id}", params: params)
end

Private Instance Methods

client_class() click to toggle source
# File lib/zype/base_model.rb, line 69
def client_class
  Zype::ApiClient
end
generate_path() click to toggle source
# File lib/zype/base_model.rb, line 64
def generate_path
  split = self.class.name.split(/(?=[A-Z])/)
  split[1..split.length].join('_').downcase
end