Class: Teamwork::API

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

Instance Method Summary collapse

Constructor Details

#initialize(project_name: nil, api_key: nil) ⇒ API

Returns a new instance of API.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/teamwork.rb', line 12

def initialize(project_name: nil, api_key: nil)
  @project_name = project_name
  @api_key = api_key
  @api_conn = Faraday.new(url: "http://#{project_name}.teamworkpm.net/") do |c|
    c.request :multipart
    c.request :json
    c.request :url_encoded
    c.response :json, content_type: /\bjson$/
    c.adapter :net_http
  end

  @api_conn.headers[:cache_control] = 'no-cache'
  @api_conn.basic_auth(api_key, '')
end

Instance Method Details

#account(request_params) ⇒ Object



27
28
29
30
# File 'lib/teamwork.rb', line 27

def (request_params)
  response = @api_conn.get "account.json", request_params
  response.body
end

#attach_post_to_project(title, body, project_id) ⇒ Object



89
90
91
# File 'lib/teamwork.rb', line 89

def attach_post_to_project(title, body, project_id)
  @api_conn.post "projects/#{project_id}/messsages.json", { post: { title: title, body: body } }
end

#create_company(name) ⇒ Object



55
56
57
# File 'lib/teamwork.rb', line 55

def create_company(name)
  @api_conn.post 'companies.json', { company: { name: name } }
end

#create_project(name, client_name) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/teamwork.rb', line 64

def create_project(name, client_name)
  company_id = get_or_create_company(name)
  @api_conn.post('projects.json', {
      project: {
          name: name,
          companyId: company_id,
          "category-id" => '0'
      }
  }).headers['id']
end

#delete_project(id) ⇒ Object



85
86
87
# File 'lib/teamwork.rb', line 85

def delete_project(id)
  @api_conn.delete "projects/#{id}.json"
end

#get_comment(id) ⇒ Object



46
47
48
49
# File 'lib/teamwork.rb', line 46

def get_comment(id)
  response = @api_conn.get "comments/#{id}.json"
  response.body["comment"]
end

#get_company_id(name) ⇒ Object



51
52
53
# File 'lib/teamwork.rb', line 51

def get_company_id(name)
  Hash[@api_conn.get('companies.json').body["companies"].map { |c| [c['name'], c['id']] }][name]
end

#get_or_create_company(name) ⇒ Object



59
60
61
62
# File 'lib/teamwork.rb', line 59

def get_or_create_company(name)
  create_company(name) if !get_company_id(name)
  get_company_id(name)
end

#latestActivity(maxItems: 60, onlyStarred: false) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/teamwork.rb', line 37

def latestActivity(maxItems: 60, onlyStarred: false)
  request_params = {
    maxItems: maxItems,
    onlyStarred: onlyStarred
  }
  response = @api_conn.get "latestActivity.json", request_params
  response.body
end

#people(request_params) ⇒ Object



32
33
34
35
# File 'lib/teamwork.rb', line 32

def people(request_params)
  response = @api_conn.get "people.json", request_params
  response.body
end

#update_project(id, name, client_name) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/teamwork.rb', line 75

def update_project(id, name, client_name)
  company_id = get_or_create_company(name)
  @api_conn.put("projects/#{id}.json", {
      project: {
          name: name,
          companyId: company_id
      }
  }).status
end