Module: Harvest::Behavior::Crud

Included in:
API::Clients, API::Contacts, API::ExpenseCategories, API::Expenses, API::InvoiceCategories, API::InvoicePayments, API::Invoices, API::Projects, API::Tasks, API::Users
Defined in:
lib/harvest/behavior/crud.rb

Instance Method Summary (collapse)

Instance Method Details

- (Array<Harvest::BaseModel>) all(user = nil, query_options = {})

Retrieves all items

Returns:

  • (Array<Harvest::BaseModel>)

    an array of models depending on where you're calling it from (e.g. [Harvest::Client] from Harvest::Base#clients)



6
7
8
9
10
# File 'lib/harvest/behavior/crud.rb', line 6

def all(user = nil, query_options = {})
  query = query_options.merge!(of_user_query(user))
  response = request(:get, credentials, api_model.api_path, :query => query)
  api_model.parse(response.parsed_response)
end

- (Harvest::BaseModel) create(model, user = nil)

Creates an item

Parameters:

  • model (Harvest::BaseModel)

    the item you want to create

Returns:

  • (Harvest::BaseModel)

    the created model depending on where you're calling it from (e.g. Harvest::Client from Harvest::Base#clients)



30
31
32
33
34
35
36
37
38
39
# File 'lib/harvest/behavior/crud.rb', line 30

def create(model, user = nil)
  model = api_model.wrap(model)
  response = request(:post, credentials, "#{api_model.api_path}", :body => model.to_json, :query => of_user_query(user))
  id = response.headers["location"].match(/\/.*\/(\d+)/)[1]
  if user
    find(id, user)
  else
    find(id)
  end
end

- (Integer) delete(model) - (Integer) delete(id) - (Integer) delete(id)

Deletes an item

Overloads:

  • - (Integer) delete(model)

    Parameters:

    • model (Harvest::BaseModel)

      the item you want to delete

  • - (Integer) delete(id)

    Parameters:

    • id (Integer)

      the id of the item you want to delete

  • - (Integer) delete(id)

    Parameters:

    • id (String)

      the String version of the id of the item you want to delete

Returns:

  • (Integer)

    the id of the item deleted



59
60
61
62
# File 'lib/harvest/behavior/crud.rb', line 59

def delete(model, user = nil)
  request(:delete, credentials, "#{api_model.api_path}/#{model.to_i}", :query => of_user_query(user))
  model.to_i
end

- (Harvest::BaseModel) find(id) - (Harvest::BaseModel) find(id) - (Harvest::BaseModel) find(model)

Retrieves an item by id

Overloads:

  • - (Harvest::BaseModel) find(id)

    Parameters:

    • the (Integer)

      id of the item you want to retreive

  • - (Harvest::BaseModel) find(id)

    Parameters:

    • id (String)

      the String version of the id

  • - (Harvest::BaseModel) find(model)

    Parameters:

    • id (Harvest::BaseModel)

      you can pass a model and it will return a refreshed version

Returns:

  • (Harvest::BaseModel)

    the model depends on where you're calling it from (e.g. Harvest::Client from Harvest::Base#clients)



21
22
23
24
25
# File 'lib/harvest/behavior/crud.rb', line 21

def find(id, user = nil)
  raise "id required" unless id
  response = request(:get, credentials, "#{api_model.api_path}/#{id}", :query => of_user_query(user))
  api_model.parse(response.parsed_response).first
end

- (Harvest::BaseModel) update(model, user = nil)

Updates an item

Parameters:

  • model (Harvest::BaseModel)

    the model you want to update

Returns:

  • (Harvest::BaseModel)

    the created model depending on where you're calling it from (e.g. Harvest::Client from Harvest::Base#clients)



44
45
46
47
48
# File 'lib/harvest/behavior/crud.rb', line 44

def update(model, user = nil)
  model = api_model.wrap(model)
  request(:put, credentials, "#{api_model.api_path}/#{model.to_i}", :body => model.to_json, :query => of_user_query(user))
  find(model.id)
end