Module: Harvest::Behavior::Crud

Instance Method Summary collapse

Instance Method Details

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

Retrieves all items



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

#create(model, user = nil) ⇒ Harvest::BaseModel

Creates an item



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

#delete(model) ⇒ Integer #delete(id) ⇒ Integer #delete(id) ⇒ Integer

Deletes an item



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

#find(id) ⇒ Harvest::BaseModel #find(id) ⇒ Harvest::BaseModel #find(model) ⇒ Harvest::BaseModel

Retrieves an item by id



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

#update(model, user = nil) ⇒ Harvest::BaseModel

Updates an item



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