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.



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

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



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

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

#attach_post_to_project(title, body, project_id) ⇒ Object



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

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



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

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

#create_project(name, client_name) ⇒ Object



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

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

#delete_project(id) ⇒ Object



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

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

#get_comment(id) ⇒ Object



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

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

#get_company_id(name) ⇒ Object



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

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



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

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



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

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



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

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

#update_project(id, name, client_name) ⇒ Object



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

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