teamwork_api 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +11 -1
  4. data/.travis.yml +14 -1
  5. data/CHANGELOG.md +14 -0
  6. data/Gemfile +4 -2
  7. data/Gemfile.lock +65 -10
  8. data/Guardfile +12 -0
  9. data/README.md +5 -5
  10. data/Rakefile +2 -0
  11. data/lib/teamwork_api.rb +3 -0
  12. data/lib/teamwork_api/api/companies.rb +4 -0
  13. data/lib/teamwork_api/api/params.rb +18 -11
  14. data/lib/teamwork_api/api/people.rb +52 -29
  15. data/lib/teamwork_api/api/project_owner.rb +6 -1
  16. data/lib/teamwork_api/api/projects.rb +70 -47
  17. data/lib/teamwork_api/api/task_lists.rb +8 -4
  18. data/lib/teamwork_api/client.rb +24 -20
  19. data/lib/teamwork_api/error.rb +7 -1
  20. data/lib/teamwork_api/version.rb +3 -1
  21. data/spec/fixtures/companies.json +64 -0
  22. data/spec/fixtures/company.json +32 -0
  23. data/spec/fixtures/create_project.json +4 -0
  24. data/spec/fixtures/create_task_list.json +4 -0
  25. data/spec/fixtures/delete.json +3 -0
  26. data/spec/fixtures/people.json +80 -0
  27. data/spec/fixtures/person.json +77 -0
  28. data/spec/fixtures/project.json +68 -0
  29. data/spec/fixtures/project_owner.json +91 -0
  30. data/spec/fixtures/projects.json +135 -0
  31. data/spec/fixtures/task_list.json +18 -0
  32. data/spec/fixtures/task_lists.json +22 -0
  33. data/spec/fixtures/update.json +3 -0
  34. data/spec/spec_helper.rb +60 -7
  35. data/spec/teamwork_api/api/companies_spec.rb +69 -0
  36. data/spec/teamwork_api/api/people_spec.rb +78 -0
  37. data/spec/teamwork_api/api/project_owner_spec.rb +35 -0
  38. data/spec/teamwork_api/api/projects_spec.rb +117 -0
  39. data/spec/teamwork_api/api/task_lists_spec.rb +98 -0
  40. data/spec/teamwork_api/client_spec.rb +202 -0
  41. data/teamwork_api.gemspec +10 -2
  42. metadata +116 -8
  43. data/spec/teamwork_api_spec.rb +0 -9
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TeamworkApi::API::Companies do
6
+ subject { TeamworkApi::Client.new('http://localhost:3000', 'xxx') }
7
+
8
+ describe '#companies' do
9
+ before do
10
+ stub_get(
11
+ 'http://localhost:3000/companies.json'
12
+ ).to_return(
13
+ body: fixture('companies.json'),
14
+ headers: { content_type: 'application/json' }
15
+ )
16
+ end
17
+
18
+ it 'requests the correct resource' do
19
+ subject.companies
20
+ expect(a_get('http://localhost:3000/companies.json')).to have_been_made
21
+ end
22
+
23
+ it 'returns companies' do
24
+ companies = subject.companies
25
+ expect(companies).to be_an Array
26
+ expect(companies.first).to be_a Hash
27
+ end
28
+ end
29
+
30
+ describe '#company' do
31
+ before do
32
+ stub_get(
33
+ 'http://localhost:3000/companies/999.json'
34
+ ).to_return(
35
+ body: fixture('company.json'),
36
+ headers: { content_type: 'application/json' }
37
+ )
38
+ end
39
+
40
+ it 'requests the correct resource' do
41
+ subject.company(999)
42
+ expect(
43
+ a_get('http://localhost:3000/companies/999.json')
44
+ ).to have_been_made
45
+ end
46
+
47
+ it 'returns a single company' do
48
+ company = subject.company(999)
49
+ expect(company).to be_a Hash
50
+ end
51
+ end
52
+
53
+ describe '#company_by_name' do
54
+ before do
55
+ stub_get(
56
+ 'http://localhost:3000/companies.json'
57
+ ).to_return(
58
+ body: fixture('companies.json'),
59
+ headers: { content_type: 'application/json' }
60
+ )
61
+ end
62
+
63
+ it 'returns the named company' do
64
+ company = subject.company_by_name('Company One')
65
+ expect(company).to be_a Hash
66
+ expect(company['name']).to eq 'Company One'
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TeamworkApi::API::People do
6
+ subject { TeamworkApi::Client.new('http://localhost:3000', 'xxx') }
7
+
8
+ describe '#people' do
9
+ before do
10
+ stub_get(
11
+ 'http://localhost:3000/people.json'
12
+ ).to_return(
13
+ body: fixture('people.json'),
14
+ headers: { content_type: 'application/json' }
15
+ )
16
+ end
17
+
18
+ it 'requests the correct resource' do
19
+ subject.people
20
+ expect(a_get('http://localhost:3000/people.json')).to have_been_made
21
+ end
22
+
23
+ it 'returns the requested people' do
24
+ people = subject.people
25
+ expect(people).to be_an Array
26
+ people.each { |g| expect(g).to be_a Hash }
27
+ end
28
+ end
29
+
30
+ describe '#person' do
31
+ before do
32
+ stub_get(
33
+ 'http://localhost:3000/people/1.json'
34
+ ).to_return(
35
+ body: fixture('person.json'),
36
+ headers: { content_type: 'application/json' }
37
+ )
38
+ end
39
+
40
+ it 'requests the correct resource' do
41
+ subject.person(1)
42
+ expect(
43
+ a_get('http://localhost:3000/people/1.json')
44
+ ).to have_been_made
45
+ end
46
+
47
+ it 'returns a single person' do
48
+ person = subject.person(1)
49
+ expect(person).to be_a Hash
50
+ end
51
+ end
52
+
53
+ describe 'add_person_to_project' do
54
+ it 'adds person to project' do
55
+ stub_put('http://localhost:3000/projects/999/people.json')
56
+ subject.add_person_to_project(999, 1)
57
+
58
+ expect(
59
+ a_put(
60
+ 'http://localhost:3000/projects/999/people.json'
61
+ ).with(body: { add: { userIdList: 1 } })
62
+ ).to have_been_made
63
+ end
64
+ end
65
+
66
+ describe '#set_permissions' do
67
+ it 'sets permissions for person on project' do
68
+ stub_put('http://localhost:3000/projects/999/people/1.json')
69
+ subject.set_permissions(999, 1, 'project-administrator': true)
70
+
71
+ expect(
72
+ a_put(
73
+ 'http://localhost:3000/projects/999/people/1.json'
74
+ ).with(body: { permissions: { 'project-administrator': true } })
75
+ ).to have_been_made
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TeamworkApi::API::ProjectOwner do
6
+ subject { TeamworkApi::Client.new('http://localhost:3000', 'xxx') }
7
+
8
+ describe '#project_owner' do
9
+ before do
10
+ stub_get(
11
+ 'http://localhost:3000/projects/1.json?project%5B' \
12
+ 'include_project_owner%5D=true'
13
+ ).to_return(
14
+ body: fixture('project_owner.json'),
15
+ headers: { content_type: 'application/json' }
16
+ )
17
+ end
18
+
19
+ it 'requests the correct resource' do
20
+ subject.project_owner(1)
21
+ expect(
22
+ a_get(
23
+ 'http://localhost:3000/projects/1.json'
24
+ ).with(
25
+ query: { project: { include_project_owner: true } }
26
+ )
27
+ ).to have_been_made
28
+ end
29
+
30
+ it 'returns the project owner' do
31
+ owner = subject.project_owner(1)
32
+ expect(owner).to be_a Hash
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TeamworkApi::API::Projects do
6
+ subject { TeamworkApi::Client.new('http://localhost:3000', 'xxx') }
7
+
8
+ describe '#create_project' do
9
+ before do
10
+ stub_post(
11
+ 'http://localhost:3000/projects.json'
12
+ ).to_return(
13
+ body: fixture('create_project.json'),
14
+ headers: { content_type: 'application/json' }
15
+ )
16
+ end
17
+
18
+ it 'requests the correct resource' do
19
+ subject.create_project(name: 'My Project')
20
+ expect(a_post('http://localhost:3000/projects.json')).to have_been_made
21
+ end
22
+
23
+ it 'returns the project id' do
24
+ id = subject.create_project(name: 'My Project')
25
+ expect(id).to eq 323_605
26
+ end
27
+ end
28
+
29
+ describe '#update_project' do
30
+ before do
31
+ stub_put(
32
+ 'http://localhost:3000/projects/1.json'
33
+ ).to_return(
34
+ body: fixture('update.json'),
35
+ headers: { content_type: 'application/json' }
36
+ )
37
+ end
38
+
39
+ it 'requests the correct resource' do
40
+ subject.update_project(1, name: 'My renamed Project')
41
+ expect(a_put('http://localhost:3000/projects/1.json')).to have_been_made
42
+ end
43
+
44
+ it 'returns ok' do
45
+ response = subject.update_project(1, name: 'My renamed Project')
46
+ expect(response.body).to eq('STATUS' => 'OK')
47
+ end
48
+ end
49
+
50
+ describe '#projects' do
51
+ before do
52
+ stub_get(
53
+ 'http://localhost:3000/projects.json'
54
+ ).to_return(
55
+ body: fixture('projects.json'),
56
+ headers: { content_type: 'application/json' }
57
+ )
58
+ end
59
+
60
+ it 'requests the correct resource' do
61
+ subject.projects
62
+ expect(a_get('http://localhost:3000/projects.json')).to have_been_made
63
+ end
64
+
65
+ it 'returns the requested projects' do
66
+ projects = subject.projects
67
+ expect(projects).to be_an Array
68
+ projects.each { |g| expect(g).to be_a Hash }
69
+ end
70
+ end
71
+
72
+ describe '#project' do
73
+ before do
74
+ stub_get(
75
+ 'http://localhost:3000/projects/1.json'
76
+ ).to_return(
77
+ body: fixture('project.json'),
78
+ headers: { content_type: 'application/json' }
79
+ )
80
+ end
81
+
82
+ it 'requests the correct resource' do
83
+ subject.project(1)
84
+ expect(
85
+ a_get('http://localhost:3000/projects/1.json')
86
+ ).to have_been_made
87
+ end
88
+
89
+ it 'returns a single project' do
90
+ project = subject.project(1)
91
+ expect(project).to be_a Hash
92
+ end
93
+ end
94
+
95
+ describe '#delete_project' do
96
+ before do
97
+ stub_delete(
98
+ 'http://localhost:3000/projects/1.json'
99
+ ).to_return(
100
+ body: fixture('delete.json'),
101
+ headers: { content_type: 'application/json' }
102
+ )
103
+ end
104
+
105
+ it 'requests the correct resource' do
106
+ subject.delete_project(1)
107
+ expect(
108
+ a_delete('http://localhost:3000/projects/1.json')
109
+ ).to have_been_made
110
+ end
111
+
112
+ it 'returns ok' do
113
+ response = subject.delete_project(1)
114
+ expect(response.body).to eq('STATUS' => 'OK')
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TeamworkApi::API::TaskLists do
6
+ subject { TeamworkApi::Client.new('http://localhost:3000', 'xxx') }
7
+
8
+ describe '#create_task_list' do
9
+ before do
10
+ stub_post(
11
+ 'http://localhost:3000/projects/1/tasklists.json'
12
+ ).to_return(
13
+ body: fixture('create_task_list.json'),
14
+ headers: { content_type: 'application/json' }
15
+ )
16
+ end
17
+
18
+ it 'requests the correct resource' do
19
+ subject.create_task_list(1, name: 'My list')
20
+ expect(
21
+ a_post('http://localhost:3000/projects/1/tasklists.json')
22
+ ).to have_been_made
23
+ end
24
+
25
+ it 'returns the task list id' do
26
+ id = subject.create_task_list(1, name: 'My list')
27
+ expect(id).to eq 1_234
28
+ end
29
+ end
30
+
31
+ describe '#task_lists' do
32
+ before do
33
+ stub_get(
34
+ 'http://localhost:3000/tasklists.json'
35
+ ).to_return(
36
+ body: fixture('task_lists.json'),
37
+ headers: { content_type: 'application/json' }
38
+ )
39
+ end
40
+
41
+ it 'requests the correct resource' do
42
+ subject.task_lists
43
+ expect(a_get('http://localhost:3000/tasklists.json')).to have_been_made
44
+ end
45
+
46
+ it 'returns the requested task lists' do
47
+ task_lists = subject.task_lists
48
+ expect(task_lists).to be_an Array
49
+ task_lists.each { |tl| expect(tl).to be_a Hash }
50
+ end
51
+ end
52
+
53
+ describe '#task_list' do
54
+ before do
55
+ stub_get(
56
+ 'http://localhost:3000/tasklists/1.json'
57
+ ).to_return(
58
+ body: fixture('task_list.json'),
59
+ headers: { content_type: 'application/json' }
60
+ )
61
+ end
62
+
63
+ it 'requests the correct resource' do
64
+ subject.task_list(1)
65
+ expect(
66
+ a_get('http://localhost:3000/tasklists/1.json')
67
+ ).to have_been_made
68
+ end
69
+
70
+ it 'returns a single task list' do
71
+ task_list = subject.task_list(1)
72
+ expect(task_list).to be_a Hash
73
+ end
74
+ end
75
+
76
+ describe '#delete_task_list' do
77
+ before do
78
+ stub_delete(
79
+ 'http://localhost:3000/tasklists/1.json'
80
+ ).to_return(
81
+ body: fixture('delete.json'),
82
+ headers: { content_type: 'application/json' }
83
+ )
84
+ end
85
+
86
+ it 'requests the correct resource' do
87
+ subject.delete_task_list(1)
88
+ expect(
89
+ a_delete('http://localhost:3000/tasklists/1.json')
90
+ ).to have_been_made
91
+ end
92
+
93
+ it 'returns ok' do
94
+ response = subject.delete_task_list(1)
95
+ expect(response.body).to eq('STATUS' => 'OK')
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,202 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TeamworkApi::Client do
6
+ subject { TeamworkApi::Client.new('http://localhost:3000') }
7
+
8
+ describe '.new' do
9
+ it 'requires a host argument' do
10
+ expect { TeamworkApi::Client.new }.to raise_error ArgumentError
11
+ end
12
+
13
+ it 'defaults api key to nil' do
14
+ expect(subject.api_key).to be_nil
15
+ end
16
+
17
+ it 'accepts an api key argument' do
18
+ client = TeamworkApi::Client.new('http://localhost:3000', 'test')
19
+ expect(client.api_key).to eq('test')
20
+ end
21
+ end
22
+
23
+ describe '#api_key' do
24
+ it 'is publically accessible' do
25
+ subject.api_key = 'test_d7fd0429940'
26
+ expect(subject.api_key).to eq('test_d7fd0429940')
27
+ end
28
+ end
29
+
30
+ describe '#host' do
31
+ it 'is publically readable' do
32
+ expect(subject.host).to eq('http://localhost:3000')
33
+ end
34
+
35
+ it 'is not publically writeable' do
36
+ expect(subject).not_to respond_to(:host=)
37
+ end
38
+ end
39
+
40
+ describe '#connection' do
41
+ it 'looks like a Faraday connection' do
42
+ expect(subject.send(:connection)).to respond_to :run_request
43
+ end
44
+
45
+ it 'memorizes the connection' do
46
+ c1 = subject.send(:connection)
47
+ c2 = subject.send(:connection)
48
+ expect(c1.object_id).to eq(c2.object_id)
49
+ end
50
+ end
51
+
52
+ describe '#delete' do
53
+ before do
54
+ stub_delete(
55
+ 'http://localhost:3000/test/delete'
56
+ ).with(query: { deleted: 'object' })
57
+ subject.api_key = 'test_d7fd0429940'
58
+ end
59
+
60
+ it 'allows custom delete requests' do
61
+ subject.delete('/test/delete', deleted: 'object')
62
+ expect(
63
+ a_delete(
64
+ 'http://localhost:3000/test/delete'
65
+ ).with(query: { deleted: 'object' })
66
+ ).to have_been_made
67
+ end
68
+
69
+ context 'when using a host with a subdirectory' do
70
+ subject { TeamworkApi::Client.new('http://localhost:3000/forum') }
71
+
72
+ before do
73
+ stub_delete(
74
+ 'http://localhost:3000/forum/test/delete'
75
+ ).with(query: { deleted: 'object' })
76
+ end
77
+
78
+ it 'allows custom delete requests' do
79
+ subject.delete('/test/delete', deleted: 'object')
80
+ expect(
81
+ a_delete(
82
+ 'http://localhost:3000/forum/test/delete'
83
+ ).with(query: { deleted: 'object' })
84
+ ).to have_been_made
85
+ end
86
+ end
87
+ end
88
+
89
+ describe '#post' do
90
+ before do
91
+ stub_post(
92
+ 'http://localhost:3000/test/post'
93
+ ).with(body: { created: 'object' })
94
+ subject.api_key = 'test_d7fd0429940'
95
+ end
96
+
97
+ it 'allows custom post requests' do
98
+ subject.post('/test/post', created: 'object')
99
+ expect(
100
+ a_post(
101
+ 'http://localhost:3000/test/post'
102
+ ).with(body: { created: 'object' })
103
+ ).to have_been_made
104
+ end
105
+
106
+ context 'when using a host with a subdirectory' do
107
+ subject { TeamworkApi::Client.new('http://localhost:3000/forum') }
108
+
109
+ before do
110
+ stub_post(
111
+ 'http://localhost:3000/forum/test/post'
112
+ ).with(body: { created: 'object' })
113
+ end
114
+
115
+ it 'allows custom post requests' do
116
+ subject.post('/test/post', created: 'object')
117
+ expect(
118
+ a_post(
119
+ 'http://localhost:3000/forum/test/post'
120
+ ).with(body: { created: 'object' })
121
+ ).to have_been_made
122
+ end
123
+ end
124
+ end
125
+
126
+ describe '#put' do
127
+ before do
128
+ stub_put(
129
+ 'http://localhost:3000/test/put'
130
+ ).with(body: { updated: 'object' })
131
+ subject.api_key = 'test_d7fd0429940'
132
+ end
133
+
134
+ it 'allows custom put requests' do
135
+ subject.put('/test/put', updated: 'object')
136
+ expect(
137
+ a_put(
138
+ 'http://localhost:3000/test/put'
139
+ ).with(body: { updated: 'object' })
140
+ ).to have_been_made
141
+ end
142
+
143
+ context 'when using a host with a subdirectory' do
144
+ subject { TeamworkApi::Client.new('http://localhost:3000/subd') }
145
+
146
+ before do
147
+ stub_put(
148
+ 'http://localhost:3000/subd/test/put'
149
+ ).with(body: { updated: 'object' })
150
+ end
151
+
152
+ it 'allows custom post requests' do
153
+ subject.put('/test/put', updated: 'object')
154
+ expect(
155
+ a_put(
156
+ 'http://localhost:3000/subd/test/put'
157
+ ).with(body: { updated: 'object' })
158
+ ).to have_been_made
159
+ end
160
+ end
161
+ end
162
+
163
+ describe '#request' do
164
+ it 'catches 500 errors' do
165
+ connection =
166
+ instance_double(Faraday::Connection, headers: {}, basic_auth: '')
167
+ allow(
168
+ connection
169
+ ).to receive(
170
+ :get
171
+ ).and_return(
172
+ OpenStruct.new(env: { body: 'error page html' }, status: 500)
173
+ )
174
+ allow(subject).to receive(:connection).and_return(connection)
175
+ expect {
176
+ subject.send(:request, :get, '/test')
177
+ }.to raise_error TeamworkApi::Error
178
+ end
179
+
180
+ it 'catches Faraday errors' do
181
+ allow(
182
+ subject
183
+ ).to receive(
184
+ :connection
185
+ ).and_raise(Faraday::Error::ClientError.new('BOOM!'))
186
+ expect {
187
+ subject.send(:request, :get, '/test')
188
+ }.to raise_error TeamworkApi::Error
189
+ end
190
+
191
+ it 'catches JSON::ParserError errors' do
192
+ allow(
193
+ subject
194
+ ).to receive(
195
+ :connection
196
+ ).and_raise(JSON::ParserError.new('unexpected token'))
197
+ expect {
198
+ subject.send(:request, :get, '/test')
199
+ }.to raise_error TeamworkApi::Error
200
+ end
201
+ end
202
+ end