Class: Harvest::API::Projects

Inherits:
Base
  • Object
show all
Includes:
Behavior::Crud
Defined in:
lib/harvest/api/projects.rb

Instance Attribute Summary

Attributes inherited from Base

#credentials

Instance Method Summary (collapse)

Methods included from Behavior::Crud

#create, #delete, #find, #update

Methods inherited from Base

api_model, #initialize

Constructor Details

This class inherits a constructor from Harvest::API::Base

Instance Method Details

- (Harvest::Project) activate(project)

Activates the project. Does nothing if the project is already activated



45
46
47
48
49
50
51
# File 'lib/harvest/api/projects.rb', line 45

def activate(project)
  if !project.active?
    request(:put, credentials, "#{api_model.api_path}/#{project.to_i}/toggle", :headers => {'Content-Length' => '0'})
    project.active = true
  end
  project
end

- (Harvest::Project) all

Retrieves all projects. Note: this requires project manager or administrator authorization.



10
11
12
13
14
# File 'lib/harvest/api/projects.rb', line 10

def all(*)
  super
rescue NotFound => e
  raise NotFound.new(e.response, e.params, "Do you have sufficient privileges? If not, consider using time.trackable_projects instead.")
end

- (Harvest::Project) create_task(project, task_name)

Creates and Assigns a task to the project

Examples

project = harvest.projects.find(401)
harvest.projects.create_task(project, 'Bottling Glue') # creates and assigns a task to the project


23
24
25
26
27
# File 'lib/harvest/api/projects.rb', line 23

def create_task(project, task_name)
  response = request(:post, credentials, "/projects/#{project.to_i}/task_assignments/add_with_create_new_task", :body => {"task" => {"name" => task_name}}.to_json)
  id = response.headers["location"].match(/\/.*\/(\d+)\/.*\/(\d+)/)[1]
  find(id)
end

- (Harvest::Project) deactivate(project)

Deactivates the project. Does nothing if the project is already deactivated



33
34
35
36
37
38
39
# File 'lib/harvest/api/projects.rb', line 33

def deactivate(project)
  if project.active?
    request(:put, credentials, "#{api_model.api_path}/#{project.to_i}/toggle", :headers => {'Content-Length' => '0'})
    project.active = false
  end
  project
end