class Zype::BaseModel
Constants
- ACCEPTED_KEYS
Attributes
Public Class Methods
# 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
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
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
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
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
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
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
# File lib/zype/base_model.rb, line 69 def client_class Zype::ApiClient end
# 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