Class: Zype::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/zype/base_model.rb

Defined Under Namespace

Classes: InvalidKey

Constant Summary collapse

ACCEPTED_KEYS =
%i(api_key app_key)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_method = 'api_key') ⇒ BaseModel

Returns a new instance of BaseModel.



7
8
9
10
# File 'lib/zype/base_model.rb', line 7

def initialize(auth_method='api_key')
  @client = client_class.new(auth_method)
  @path = generate_path
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/zype/base_model.rb', line 3

def client
  @client
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/zype/base_model.rb', line 3

def path
  @path
end

Instance Method Details

#all(params: {}) ⇒ Array<Hash>

Returns all objects for given class

Parameters:

  • params (Hash) (defaults to: {})

    the metadata to filter objects by

Returns:

  • (Array<Hash>)

    the objects returned from the API



16
17
18
# File 'lib/zype/base_model.rb', line 16

def all(params: {})
  client.execute(method: :get, path: "/#{path}", params: params)
end

#auth=(auth_method) ⇒ Object

Sets the authentication method for calling the API

Parameters:

  • auth_method (String)

    one of ‘api_key’ or ‘app_key’

Raises:



56
57
58
59
60
# 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:) ⇒ Hash

Creates a new object via the API

Parameters:

  • params (Hash)

    the properties of the object

Returns:

  • (Hash)

    the newly created object



32
33
34
# File 'lib/zype/base_model.rb', line 32

def create(params:)
  client.execute(method: :post, path: "/#{path}", params: params)
end

#delete(id:) ⇒ Hash

Deletes an existing object via the API

Parameters:

  • id (String)

    the ID of the object

Returns:

  • (Hash)

    the deleted object



49
50
51
# File 'lib/zype/base_model.rb', line 49

def delete(id:)
  client.execute(method: :delete, path: "/#{path}/#{id}")
end

#find(id:) ⇒ Hash

Returns object matching ID

Parameters:

  • id (String)

    the ID of the object

Returns:

  • (Hash)

    the object returned from the API



24
25
26
# File 'lib/zype/base_model.rb', line 24

def find(id:)
  client.execute(method: :get, path: "/#{path}/#{id}")
end

#update(id:, params:) ⇒ Hash

Updates an existing object via the API

Parameters:

  • id (String)

    the ID of the object

  • params (Hash)

    the properties to be updated

Returns:

  • (Hash)

    the updated object



41
42
43
# File 'lib/zype/base_model.rb', line 41

def update(id:, params:)
  client.execute(method: :put, path: "/#{path}/#{id}", params: params)
end