module Forecast::Behavior::Crud
Public Instance Methods
def all(user = nil, query_options = {})
# File lib/forecast/behavior/crud.rb, line 11 def all(query_options = {}) # query = query_options.merge!(of_user_query(user)) query = query_options response = request(:get, credentials, api_model.api_path, :query => query) api_model.parse(response.parsed_response) end
Creates an item @param [Harvest::BaseModel] model the item you want to create @return [Harvest::BaseModel] the created model depending on where you're calling it from (e.g. Harvest::Client from Harvest::Base#clients)
# File lib/forecast/behavior/crud.rb, line 40 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
Deletes an item @overload delete(model)
@param [Harvest::BaseModel] model the item you want to delete
@overload delete(id)
@param [Integer] id the id of the item you want to delete
@overload delete(id)
@param [String] id the String version of the id of the item you want to delete
@return [Integer] the id of the item deleted
# File lib/forecast/behavior/crud.rb, line 69 def delete(model, user = nil) request(:delete, credentials, "#{api_model.api_path}/#{model.to_i}", :query => of_user_query(user)) model.to_i end
Retrieves an item by id @overload find(id)
@param [Integer] the id of the item you want to retreive
@overload find(id)
@param [String] id the String version of the id
@overload find(model)
@param [Harvest::BaseModel] id you can pass a model and it will return a refreshed version
@return [Harvest::BaseModel] the model depends on where you're calling it from (e.g. Harvest::Client from Harvest::Base#clients)
# File lib/forecast/behavior/crud.rb, line 30 def find(id, user = nil) raise "id required" unless id # response = request(:get, credentials, "#{api_model.api_path}/#{id}", :query => of_user_query(user)) response = request(:get, credentials, "#{api_model.api_path}/#{id}") api_model.parse(response.parsed_response) end
Updates an item @param [Harvest::BaseModel] model the model you want to update @return [Harvest::BaseModel] the created model depending on where you're calling it from (e.g. Harvest::Client from Harvest::Base#clients)
# File lib/forecast/behavior/crud.rb, line 54 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