desk_api 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +26 -8
  4. data/lib/desk.rb +1 -1
  5. data/lib/desk_api.rb +2 -3
  6. data/lib/desk_api/client.rb +6 -6
  7. data/lib/desk_api/configuration.rb +5 -2
  8. data/lib/desk_api/default.rb +2 -2
  9. data/lib/desk_api/error.rb +2 -2
  10. data/lib/desk_api/error/bad_gateway.rb +1 -1
  11. data/lib/desk_api/error/bad_request.rb +1 -1
  12. data/lib/desk_api/error/client_error.rb +1 -1
  13. data/lib/desk_api/error/configuration_error.rb +1 -1
  14. data/lib/desk_api/error/conflict.rb +1 -1
  15. data/lib/desk_api/error/follow_redirect_error.rb +1 -1
  16. data/lib/desk_api/error/forbidden.rb +1 -1
  17. data/lib/desk_api/error/gateway_timeout.rb +1 -1
  18. data/lib/desk_api/error/internal_server_error.rb +1 -1
  19. data/lib/desk_api/error/method_not_allowed.rb +1 -1
  20. data/lib/desk_api/error/not_acceptable.rb +1 -1
  21. data/lib/desk_api/error/not_found.rb +1 -1
  22. data/lib/desk_api/error/parser_error.rb +1 -1
  23. data/lib/desk_api/error/server_error.rb +1 -1
  24. data/lib/desk_api/error/service_unavailable.rb +1 -1
  25. data/lib/desk_api/error/too_many_requests.rb +1 -1
  26. data/lib/desk_api/error/unauthorized.rb +1 -1
  27. data/lib/desk_api/error/unprocessable_entity.rb +1 -1
  28. data/lib/desk_api/error/unsupported_media_type.rb +1 -1
  29. data/lib/desk_api/rate_limit.rb +2 -2
  30. data/lib/desk_api/request/encode_dates.rb +73 -0
  31. data/lib/desk_api/request/encode_json.rb +2 -2
  32. data/lib/desk_api/request/oauth.rb +2 -2
  33. data/lib/desk_api/request/retry.rb +4 -3
  34. data/lib/desk_api/resource.rb +22 -194
  35. data/lib/desk_api/resource/download.rb +57 -0
  36. data/lib/desk_api/resource/pagination.rb +77 -0
  37. data/lib/desk_api/resource/query_params.rb +106 -0
  38. data/lib/desk_api/resource/scrud.rb +145 -0
  39. data/lib/desk_api/response/follow_redirects.rb +2 -2
  40. data/lib/desk_api/response/parse_dates.rb +2 -2
  41. data/lib/desk_api/response/parse_json.rb +2 -2
  42. data/lib/desk_api/response/raise_error.rb +2 -2
  43. data/lib/desk_api/version.rb +2 -2
  44. data/spec/cassettes/DeskApi_Resource/_next_/returns_nil_on_non-page_resources.yml +207 -0
  45. data/spec/cassettes/DeskApi_Resource/_next_/throws_an_error_on_non-page_resources.yml +207 -0
  46. data/spec/cassettes/DeskApi_Resource_Download/downloads_the_attachment.yml +238 -0
  47. data/spec/cassettes/DeskApi_Resource_Download/throws_an_error_on_non_attachment_resources.yml +47 -0
  48. data/spec/cassettes/DeskApi_Resource_Pagination/_all/iterates_over_each_resource_on_each_page.yml +13538 -0
  49. data/spec/cassettes/DeskApi_Resource_Pagination/_each_page/iterates_over_each_page.yml +13538 -0
  50. data/spec/cassettes/DeskApi_Resource_Pagination/_each_page/raises_NoMethodError_is_called_on_non-page_resources.yml +2093 -0
  51. data/spec/cassettes/DeskApi_Resource_Pagination/_each_page/uses_a_default_per_page_of_1000.yml +13538 -0
  52. data/spec/cassettes/DeskApi_Resource_QueryParams/_page/keeps_the_resource_as_loaded.yml +2093 -0
  53. data/spec/cassettes/DeskApi_Resource_QueryParams/_page/returns_the_current_page_and_loads_if_page_not_defined.yml +1207 -0
  54. data/spec/cassettes/DeskApi_Resource_QueryParams/_page/sets_the_resource_to_not_loaded.yml +2093 -0
  55. data/spec/cassettes/{DeskApi_Resource → DeskApi_Resource_SCRUD}/_create/creates_a_new_topic.yml +0 -0
  56. data/spec/cassettes/{DeskApi_Resource → DeskApi_Resource_SCRUD}/_create/throws_an_error_creating_a_user.yml +0 -0
  57. data/spec/cassettes/{DeskApi_Resource → DeskApi_Resource_SCRUD}/_delete/deletes_a_resource.yml +0 -0
  58. data/spec/cassettes/{DeskApi_Resource → DeskApi_Resource_SCRUD}/_delete/throws_an_error_deleting_a_non_deletalbe_resource.yml +0 -0
  59. data/spec/cassettes/{DeskApi_Resource → DeskApi_Resource_SCRUD}/_find/has_an_alias_by_id.yml +0 -0
  60. data/spec/cassettes/{DeskApi_Resource → DeskApi_Resource_SCRUD}/_find/loads_the_requested_resource.yml +0 -0
  61. data/spec/cassettes/{DeskApi_Resource → DeskApi_Resource_SCRUD}/_search/allows_searching_on_search_enabled_resources.yml +0 -0
  62. data/spec/cassettes/{DeskApi_Resource → DeskApi_Resource_SCRUD}/_search/throws_an_error_if_search_is_not_enabled.yml +0 -0
  63. data/spec/cassettes/{DeskApi_Resource → DeskApi_Resource_SCRUD}/_update/can_handle_action_params.yml +0 -0
  64. data/spec/cassettes/{DeskApi_Resource → DeskApi_Resource_SCRUD}/_update/can_handle_links.yml +0 -0
  65. data/spec/cassettes/{DeskApi_Resource → DeskApi_Resource_SCRUD}/_update/can_handle_update_action_params.yml +0 -0
  66. data/spec/cassettes/{DeskApi_Resource → DeskApi_Resource_SCRUD}/_update/can_replace_instead_of_append.yml +0 -0
  67. data/spec/cassettes/{DeskApi_Resource → DeskApi_Resource_SCRUD}/_update/can_update_without_a_hash.yml +0 -0
  68. data/spec/cassettes/{DeskApi_Resource → DeskApi_Resource_SCRUD}/_update/throws_an_error_updating_a_user.yml +0 -0
  69. data/spec/cassettes/{DeskApi_Resource → DeskApi_Resource_SCRUD}/_update/updates_a_topic.yml +0 -0
  70. data/spec/desk_api/client_spec.rb +33 -7
  71. data/spec/desk_api/configuration_spec.rb +4 -4
  72. data/spec/desk_api/default_spec.rb +1 -1
  73. data/spec/desk_api/error_spec.rb +1 -1
  74. data/spec/desk_api/rate_limit_spec.rb +1 -1
  75. data/spec/desk_api/request/encode_dates_spec.rb +63 -0
  76. data/spec/desk_api/request/encode_json_spec.rb +4 -4
  77. data/spec/desk_api/request/oauth_spec.rb +1 -1
  78. data/spec/desk_api/request/retry_spec.rb +1 -1
  79. data/spec/desk_api/resource/download_spec.rb +45 -0
  80. data/spec/desk_api/resource/pagination_spec.rb +77 -0
  81. data/spec/desk_api/resource/query_params_spec.rb +145 -0
  82. data/spec/desk_api/resource/scrud_spec.rb +216 -0
  83. data/spec/desk_api/resource_spec.rb +14 -321
  84. data/spec/desk_api/response/follow_redirects_spec.rb +1 -1
  85. data/spec/desk_api/response/parse_dates_spec.rb +1 -1
  86. data/spec/desk_api/response/parse_json_spec.rb +1 -1
  87. data/spec/desk_api/response/raise_error_spec.rb +1 -1
  88. data/spec/desk_api_spec.rb +1 -1
  89. data/spec/spec_helper.rb +1 -2
  90. data/spec/stubs/file.jpg +0 -0
  91. metadata +128 -104
  92. data/spec/cassettes/DeskApi_Resource/_all/iterates_over_each_resource_on_each_page.yml +0 -1953
  93. data/spec/cassettes/DeskApi_Resource/_each_page/iterates_over_each_page.yml +0 -1953
  94. data/spec/cassettes/DeskApi_Resource/_each_page/raises_NoMethodError_is_called_on_non-page_resources.yml +0 -207
  95. data/spec/cassettes/DeskApi_Resource/_each_page/uses_a_default_per_page_of_1000.yml +0 -1953
  96. data/spec/cassettes/DeskApi_Resource/_page/keeps_the_resource_as_loaded.yml +0 -113
  97. data/spec/cassettes/DeskApi_Resource/_page/returns_the_current_page_and_loads_if_page_not_defined.yml +0 -62
  98. data/spec/cassettes/DeskApi_Resource/_page/sets_the_resource_to_not_loaded.yml +0 -113
@@ -0,0 +1,216 @@
1
+ # Copyright (c) 2013-2016, Salesforce.com, Inc.
2
+ # All rights reserved.
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without modification,
5
+ # are permitted provided that the following conditions are met:
6
+ #
7
+ # * Redistributions of source code must retain the above copyright notice, this
8
+ # list of conditions and the following disclaimer.
9
+ #
10
+ # * Redistributions in binary form must reproduce the above copyright notice,
11
+ # this list of conditions and the following disclaimer in the documentation
12
+ # and/or other materials provided with the distribution.
13
+ #
14
+ # * Neither the name of Salesforce.com nor the names of its contributors may be
15
+ # used to endorse or promote products derived from this software without
16
+ # specific prior written permission.
17
+ #
18
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22
+ # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
+ # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
+ # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
+ # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+
29
+ require 'spec_helper'
30
+
31
+ describe DeskApi::Resource::SCRUD do
32
+ subject { @client ||= DeskApi::Client.new DeskApi::CONFIG }
33
+
34
+ context '#create' do
35
+ it 'creates a new topic', :vcr do
36
+ expect(
37
+ topic = subject.topics.create({
38
+ name: 'My new topic'
39
+ }).name
40
+ ).to eq('My new topic')
41
+ end
42
+
43
+ it 'throws an error creating a user', :vcr do
44
+ expect(lambda { subject.users.create(name: 'Some User') }).to raise_error(DeskApi::Error::MethodNotAllowed)
45
+ end
46
+ end
47
+
48
+ context '#update' do
49
+ it 'updates a topic', :vcr do
50
+ topic = subject.topics.entries.first
51
+
52
+ topic.description = 'Some new description'
53
+ topic.update({
54
+ name: 'Updated topic name'
55
+ })
56
+
57
+ expect(topic.name).to eq('Updated topic name')
58
+ expect(topic.description).to eq('Some new description')
59
+ end
60
+
61
+ it 'throws an error updating a user', :vcr do
62
+ user = subject.users.entries.first
63
+ expect(lambda { user.update(name: 'Some User') }).to raise_error(DeskApi::Error::MethodNotAllowed)
64
+ end
65
+
66
+ it 'can update without a hash', :vcr do
67
+ topic = subject.topics.entries.first
68
+ topic.description = 'Another description update.'
69
+ topic.update
70
+ expect(subject.topics.entries.first.description).to eq('Another description update.')
71
+ end
72
+
73
+ it 'can handle update action params', :vcr do
74
+ customer = subject.customers.entries.first
75
+ num_count = customer.phone_numbers.count
76
+ phone = { type: 'home', value: '(415) 555-1234' }
77
+
78
+ customer.update({
79
+ phone_numbers: [phone],
80
+ phone_numbers_update_action: 'append'
81
+ })
82
+
83
+ expect(customer.reload!.phone_numbers.size).to eq(num_count + 1)
84
+
85
+ customer.update({
86
+ phone_numbers: [phone],
87
+ phone_numbers_update_action: 'append'
88
+ })
89
+
90
+ expect(customer.reload!.phone_numbers.size).to eq(num_count + 2)
91
+ end
92
+
93
+ it 'can handle action params', :vcr do
94
+ ticket = subject.cases.entries.first
95
+ num_count = ticket.to_hash['labels'].count
96
+ labels = ['client_spam', 'client_test']
97
+
98
+ ticket.update({
99
+ labels: labels,
100
+ label_action: 'append'
101
+ })
102
+
103
+ expect(ticket.labels.reload!.total_entries).to eq(num_count + 2)
104
+
105
+ ticket.update({
106
+ labels: labels,
107
+ label_action: 'replace'
108
+ })
109
+
110
+ expect(ticket.labels.reload!.total_entries).to eq(2)
111
+ end
112
+
113
+ it 'can handle suppress_rules param', skip: 'does not work with VCR' do
114
+ # This test requires a Case Updated rule which always sets case status
115
+ # to open and stops processing if case labels contains
116
+ # 'suppress_rules_test'
117
+ #
118
+ # The case is updated to add the suppress_rules label,
119
+ # the rule will append 'test_failed' if it is run
120
+
121
+ VCR.turn_off! ignore_cassettes: true
122
+
123
+ ticket = subject.cases.entries.first
124
+ labels = ticket.to_hash['labels']
125
+
126
+ ticket.update({
127
+ labels: ['suppress_rules_test'],
128
+ label_action: 'append',
129
+ suppress_rules: true
130
+ })
131
+
132
+ expect(ticket.labels.reload!.entries.map(&:name).include?('test_failed')).to be false
133
+
134
+ ticket.update({
135
+ labels: labels,
136
+ label_action: 'replace'
137
+ })
138
+
139
+ VCR.turn_on!
140
+ end
141
+
142
+ it 'can replace instead of append', :vcr do
143
+ customer = subject.customers.entries.first
144
+ phone = { type: 'home', value: '(415) 555-1234' }
145
+
146
+ customer.update({
147
+ phone_numbers: [phone, phone, phone],
148
+ phone_numbers_update_action: 'append'
149
+ })
150
+
151
+ num_count = customer.reload!.phone_numbers.size
152
+ customer.update({
153
+ phone_numbers: [{ type: 'other', value: '(415) 555-4321' }],
154
+ phone_numbers_update_action: 'replace'
155
+ })
156
+
157
+ expect(customer.reload!.phone_numbers.size).to eq(1)
158
+ expect(num_count).not_to eq(customer.phone_numbers.size)
159
+ end
160
+
161
+ it 'can handle links', :vcr do
162
+ thomas = { "href"=>"/api/v2/users/16096734", "class"=>"user" }
163
+ andy = { "href"=>"/api/v2/users/21923785", "class"=>"user" }
164
+ ticket = subject.cases.find(3186)
165
+
166
+ ticket.update({
167
+ _links: { assigned_user: thomas }
168
+ })
169
+
170
+ expect(ticket.assigned_user.public_name).to eq('Thomas Stachl')
171
+ expect(ticket.load!.assigned_user.public_name).to eq('Thomas Stachl')
172
+
173
+ ticket.update({
174
+ _links: { assigned_user: andy }
175
+ })
176
+
177
+ expect(ticket.assigned_user.public_name).to eq('Andrew Frauen')
178
+ expect(ticket.load!.assigned_user.public_name).to eq('Andrew Frauen')
179
+ end
180
+ end
181
+
182
+ context '#delete' do
183
+ it 'deletes a resource', :vcr do
184
+ expect(
185
+ subject.articles.create({
186
+ subject: 'My subject',
187
+ body: 'Some text for this new article',
188
+ _links: {
189
+ topic: subject.topics.entries.first.get_self
190
+ }
191
+ }).delete
192
+ ).to eq(true)
193
+ end
194
+
195
+ it 'throws an error deleting a non deletalbe resource', :vcr do
196
+ user = subject.users.entries.first
197
+ expect(lambda { user.delete }).to raise_error(DeskApi::Error::MethodNotAllowed)
198
+ end
199
+ end
200
+
201
+ context '#search' do
202
+ it 'allows searching on search enabled resources', :vcr do
203
+ expect(subject.articles.search(text: 'Lorem Ipsum').total_entries).to eq(0)
204
+ end
205
+ end
206
+
207
+ context '#find' do
208
+ it 'loads the requested resource', :vcr do
209
+ expect(subject.cases.find(3065).subject).to eq('Testing the Tank again')
210
+ end
211
+
212
+ it 'has an alias by_id', :vcr do
213
+ expect(subject.cases.find(3065).subject).to eq('Testing the Tank again')
214
+ end
215
+ end
216
+ end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2013-2014, Salesforce.com, Inc.
1
+ # Copyright (c) 2013-2016, Salesforce.com, Inc.
2
2
  # All rights reserved.
3
3
  #
4
4
  # Redistribution and use in source and binary forms, with or without modification,
@@ -39,11 +39,11 @@ describe DeskApi::Resource do
39
39
  end
40
40
 
41
41
  it 'is not loaded initially' do
42
- expect(subject.articles.instance_variable_get(:@_loaded)).to be_false
42
+ expect(subject.articles.instance_variable_get(:@_loaded)).to eq(false)
43
43
  end
44
44
 
45
45
  it 'sets up the link to self' do
46
- expect(subject.articles.href).not_to be_nil
46
+ expect(subject.articles.href).not_to eq(nil)
47
47
  end
48
48
 
49
49
  context 'additional options' do
@@ -64,19 +64,19 @@ describe DeskApi::Resource do
64
64
  end
65
65
 
66
66
  it 'does not automatically load the resource' do
67
- expect(subject.cases(company_id: 1).instance_variable_get(:@_loaded)).to be_false
67
+ expect(subject.cases(company_id: 1).instance_variable_get(:@_loaded)).to eq(false)
68
68
  end
69
69
  end
70
70
  end
71
71
 
72
72
  context '#exec!', :vcr do
73
73
  it 'loads the current resource' do
74
- expect(subject.articles.send(:exec!).instance_variable_get(:@_loaded)).to be_true
74
+ expect(subject.articles.send(:exec!).instance_variable_get(:@_loaded)).to eq(true)
75
75
  end
76
76
 
77
77
  it 'can be forced to reload' do
78
78
  subject.articles.instance_variable_set(:@_loaded, true)
79
- subject.should_receive(:get).and_call_original
79
+ expect(subject).to receive(:get).and_call_original
80
80
  subject.articles.send(:exec!, true)
81
81
  end
82
82
  end
@@ -85,7 +85,7 @@ describe DeskApi::Resource do
85
85
  it 'loads the resource to find a suitable method' do
86
86
  articles = subject.articles
87
87
  articles.instance_variable_set(:@_loaded, false)
88
- articles.should_receive(:exec!).and_call_original
88
+ expect(articles).to receive(:exec!).and_call_original
89
89
  articles.entries
90
90
  end
91
91
 
@@ -104,16 +104,16 @@ describe DeskApi::Resource do
104
104
 
105
105
  it 'loads the resource to find a suitable method' do
106
106
  @company.instance_variable_set(:@_loaded, false)
107
- @company.should_receive(:exec!)
107
+ expect(@company).to receive(:exec!)
108
108
  @company.respond_to?(:name)
109
109
  end
110
110
 
111
111
  it 'returns true if method found in definition' do
112
- expect(@company.respond_to?(:name)).to be_true
112
+ expect(@company.respond_to?(:name)).to eq(true)
113
113
  end
114
114
 
115
115
  it 'returns false if method does not exist' do
116
- expect(@company.respond_to?(:no_method_here)).to be_false
116
+ expect(@company.respond_to?(:no_method_here)).to eq(false)
117
117
  end
118
118
  end
119
119
 
@@ -156,249 +156,13 @@ describe DeskApi::Resource do
156
156
  end
157
157
  end
158
158
 
159
- context '#search' do
160
- it 'allows searching on search enabled resources', :vcr do
161
- expect(subject.articles.search(text: 'Lorem Ipsum').total_entries).to eq(0)
162
- end
163
- end
164
-
165
- context '#create' do
166
- it 'creates a new topic', :vcr do
167
- expect(
168
- topic = subject.topics.create({
169
- name: 'My new topic'
170
- }).name
171
- ).to eq('My new topic')
172
- end
173
-
174
- it 'throws an error creating a user', :vcr do
175
- expect(lambda { subject.users.create(name: 'Some User') }).to raise_error(DeskApi::Error::MethodNotAllowed)
176
- end
177
- end
178
-
179
- context '#update' do
180
- it 'updates a topic', :vcr do
181
- topic = subject.topics.entries.first
182
-
183
- topic.description = 'Some new description'
184
- topic.update({
185
- name: 'Updated topic name'
186
- })
187
-
188
- expect(topic.name).to eq('Updated topic name')
189
- expect(topic.description).to eq('Some new description')
190
- end
191
-
192
- it 'throws an error updating a user', :vcr do
193
- user = subject.users.entries.first
194
- expect(lambda { user.update(name: 'Some User') }).to raise_error(DeskApi::Error::MethodNotAllowed)
195
- end
196
-
197
- it 'can update without a hash', :vcr do
198
- topic = subject.topics.entries.first
199
- topic.description = 'Another description update.'
200
- topic.update
201
- expect(subject.topics.entries.first.description).to eq('Another description update.')
202
- end
203
-
204
- it 'can handle update action params', :vcr do
205
- customer = subject.customers.entries.first
206
- num_count = customer.phone_numbers.count
207
- phone = { type: 'home', value: '(415) 555-1234' }
208
-
209
- customer.update({
210
- phone_numbers: [phone],
211
- phone_numbers_update_action: 'append'
212
- })
213
-
214
- expect(customer.reload!.phone_numbers.size).to eq(num_count + 1)
215
-
216
- customer.update({
217
- phone_numbers: [phone],
218
- phone_numbers_update_action: 'append'
219
- })
220
-
221
- expect(customer.reload!.phone_numbers.size).to eq(num_count + 2)
222
- end
223
-
224
- it 'can handle action params', :vcr do
225
- ticket = subject.cases.entries.first
226
- num_count = ticket.to_hash['labels'].count
227
- labels = ['client_spam', 'client_test']
228
-
229
- ticket.update({
230
- labels: labels,
231
- label_action: 'append'
232
- })
233
-
234
- expect(ticket.labels.reload!.total_entries).to eq(num_count + 2)
235
-
236
- ticket.update({
237
- labels: labels,
238
- label_action: 'replace'
239
- })
240
-
241
- expect(ticket.labels.reload!.total_entries).to eq(2)
242
- end
243
-
244
- it 'can replace instead of append', :vcr do
245
- customer = subject.customers.entries.first
246
- phone = { type: 'home', value: '(415) 555-1234' }
247
-
248
- customer.update({
249
- phone_numbers: [phone, phone, phone],
250
- phone_numbers_update_action: 'append'
251
- })
252
-
253
- num_count = customer.reload!.phone_numbers.size
254
- customer.update({
255
- phone_numbers: [{ type: 'other', value: '(415) 555-4321' }],
256
- phone_numbers_update_action: 'replace'
257
- })
258
-
259
- expect(customer.reload!.phone_numbers.size).to eq(1)
260
- expect(num_count).not_to eq(customer.phone_numbers.size)
261
- end
262
-
263
- it 'can handle links', :vcr do
264
- thomas = { "href"=>"/api/v2/users/16096734", "class"=>"user" }
265
- andy = { "href"=>"/api/v2/users/21923785", "class"=>"user" }
266
- ticket = subject.cases.find(3186)
267
-
268
- ticket.update({
269
- _links: { assigned_user: thomas }
270
- })
271
-
272
- expect(ticket.assigned_user.public_name).to eq('Thomas Stachl')
273
- expect(ticket.load!.assigned_user.public_name).to eq('Thomas Stachl')
274
-
275
- ticket.update({
276
- _links: { assigned_user: andy }
277
- })
278
-
279
- expect(ticket.assigned_user.public_name).to eq('Andrew Frauen')
280
- expect(ticket.load!.assigned_user.public_name).to eq('Andrew Frauen')
281
- end
282
- end
283
-
284
- context '#delete' do
285
- it 'deletes a resource', :vcr do
286
- expect {
287
- subject.articles.create({
288
- subject: 'My subject',
289
- body: 'Some text for this new article',
290
- _links: {
291
- topic: subject.topics.entries.first.get_self
292
- }
293
- }).delete
294
- }.to be_true
295
- end
296
-
297
- it 'throws an error deleting a non deletalbe resource', :vcr do
298
- user = subject.users.entries.first
299
- expect(lambda { user.delete }).to raise_error(DeskApi::Error::MethodNotAllowed)
300
- end
301
- end
302
-
303
- describe 'embeddable' do
304
- it 'allows to declare embedds' do
305
- expect(lambda { subject.cases.embed(:assigned_user) }).not_to raise_error
306
- end
307
-
308
- it 'changes the url' do
309
- expect(subject.cases.embed(:assigned_user).href).to eq('/api/v2/cases?embed=assigned_user')
310
- end
311
-
312
- context 'if you use embed' do
313
- before do
314
- VCR.turn_off! ignore_cassettes: true
315
-
316
- @stubs ||= Faraday::Adapter::Test::Stubs.new
317
- @client ||= DeskApi::Client.new(DeskApi::CONFIG).tap do |client|
318
- client.middleware = Proc.new do |builder|
319
- builder.response :desk_parse_dates
320
- builder.response :desk_parse_json
321
-
322
- builder.adapter :test, @stubs
323
- end
324
- end
325
- end
326
-
327
- after do
328
- VCR.turn_on!
329
- end
330
-
331
- it 'does not load the resource again' do
332
- times_called = 0
333
- @stubs.get('/api/v2/cases?embed=assigned_user') do
334
- times_called += 1
335
- [
336
- 200,
337
- { 'content-type' => 'application/json' },
338
- File.open(File.join(RSpec.configuration.root_path, 'stubs', 'cases_embed_assigned_user.json')).read
339
- ]
340
- end
341
-
342
- first_case = @client.cases.embed(:assigned_user).entries.first
343
- expect(first_case.assigned_user.name).to eq('Thomas Stachl')
344
- expect(first_case.assigned_user.instance_variable_get(:@_loaded)).to be_true
345
- expect(times_called).to eq(1)
346
- end
347
-
348
- it 'can be used in finder' do
349
- @stubs.get('/api/v2/cases/3011?embed=customer') do
350
- [
351
- 200,
352
- { 'content-type' => 'application/json' },
353
- File.open(File.join(RSpec.configuration.root_path, 'stubs', 'case_embed_customer.json')).read
354
- ]
355
- end
356
-
357
- customer = @client.cases.find(3011, embed: :customer).customer
358
- expect(customer.first_name).to eq('Thomas')
359
- customer = @client.cases.find(3011, embed: [:customer]).customer
360
- expect(customer.first_name).to eq('Thomas')
361
- end
362
- end
363
- end
364
-
365
- context '#query_params' do
366
- before do
367
- @page = DeskApi::Resource.new(subject, {
368
- '_links'=>{'self'=>{'href'=>'/api/v2/cases?page=2&per_page=50'}}
369
- }, true)
370
- end
371
-
372
- it 'allows to get query params from the current resource' do
373
- expect(@page.send(:query_params_include?, 'page')).to eq('2')
374
- expect(@page.send(:query_params_include?, 'per_page')).to eq('50')
375
- end
376
-
377
- it 'returns nil if param not found' do
378
- expect(@page.send(:query_params_include?, 'blup')).to be_nil
379
- end
380
- end
381
-
382
- context '#query_params=' do
383
- before do
384
- @page = DeskApi::Resource.new(subject, {
385
- '_links'=>{'self'=>{'href'=>'/api/v2/cases'}}
386
- }, true)
387
- end
388
-
389
- it 'sets query params on the current url' do
390
- @page.send(:query_params=, { page: 5, per_page: 50 })
391
- expect(@page.instance_variable_get(:@_definition)['_links']['self']['href']).to eq('/api/v2/cases?page=5&per_page=50')
392
- end
393
- end
394
-
395
159
  context '#get_linked_resource' do
396
160
  it 'returns linked resources', :vcr do
397
161
  expect(subject.cases.entries.first.customer).to be_an_instance_of(DeskApi::Resource)
398
162
  end
399
163
 
400
164
  it 'returns nil if link is nil', :vcr do
401
- expect(subject.articles.next).to be_nil
165
+ expect(subject.articles.next).to eq(nil)
402
166
  end
403
167
 
404
168
  it 'saves the linked resource instead of the url', :vcr do
@@ -408,36 +172,6 @@ describe DeskApi::Resource do
408
172
  end
409
173
  end
410
174
 
411
- context '#page' do
412
- it 'returns the current page and loads if page not defined', :vcr do
413
- expect(subject.articles.page).to eq(1)
414
- end
415
-
416
- it 'sets the page' do
417
- expect(subject.cases.page(5).page).to eq(5)
418
- end
419
-
420
- it 'sets the resource to not loaded', :vcr do
421
- cases = subject.cases.send(:exec!)
422
- expect(cases.page(5).instance_variable_get(:@_loaded)).to be_false
423
- end
424
-
425
- it 'keeps the resource as loaded', :vcr do
426
- cases = subject.cases.send(:exec!)
427
- expect(cases.page(1).instance_variable_get(:@_loaded)).to be_true
428
- end
429
- end
430
-
431
- context '#find' do
432
- it 'loads the requested resource', :vcr do
433
- expect(subject.cases.find(3065).subject).to eq('Testing the Tank again')
434
- end
435
-
436
- it 'has an alias by_id', :vcr do
437
- expect(subject.cases.find(3065).subject).to eq('Testing the Tank again')
438
- end
439
- end
440
-
441
175
  context '#to_hash' do
442
176
  it 'returns a hash for a desk resource', :vcr do
443
177
  expect(subject.topics.entries.first.to_hash).to eq({
@@ -491,52 +225,11 @@ describe DeskApi::Resource do
491
225
  end
492
226
 
493
227
  it 'returns nil on the last page', :vcr do
494
- expect(subject.cases.last.next!).to eq(nil)
495
- end
496
-
497
- end
498
-
499
- context '#each_page' do
500
- it 'iterates over each page', :vcr do
501
- subject.cases.each_page do |page, page_number|
502
- expect(page).to be_an_instance_of(DeskApi::Resource)
503
- expect(page.resource_type).to eq('page')
504
- expect(page_number).to be_an_instance_of(Fixnum)
505
- end
506
- end
507
-
508
- it 'uses a default per_page of 1000', :vcr do
509
- subject.cases.each_page do |page, page_number|
510
- expect((page.query_params['per_page'].to_i % 10)).to eq(0)
511
- end
512
- end
513
-
514
- it 'uses per_page from query_params if present' do
515
- subject.cases.per_page(25) do |page, page_number|
516
- expect(page.query_params['per_page']).to eq(25)
517
- end
518
- end
519
-
520
- it 'raises ArgumentError if no block is given' do
521
- expect { subject.cases.each_page }.to raise_error(ArgumentError)
522
- end
523
-
524
- it 'raises NoMethodError is called on non-page resources', :vcr do
525
- expect { subject.cases.entries.first.each_page { |x| x } }.to raise_error(NoMethodError)
526
- end
527
- end
528
-
529
- context '#all' do
530
- it 'iterates over each resource on each page', :vcr do
531
- subject.cases.all do |resource, page_num|
532
- expect(resource).to be_an_instance_of(DeskApi::Resource)
533
- expect(resource.resource_type).to eq('case')
534
- expect(page_num).to be_an_instance_of(Fixnum)
535
- end
228
+ expect(subject.cases.last.next!).to be_nil
536
229
  end
537
230
 
538
- it 'raises an argument error if no block is given' do
539
- expect { subject.cases.all }.to raise_error(ArgumentError)
231
+ it 'returns nil on non-page resources', :vcr do
232
+ expect(subject.cases.entries.first.next!).to be_nil
540
233
  end
541
234
  end
542
235