desk_api 0.6.1 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -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,
@@ -183,14 +183,40 @@ describe DeskApi::Client do
183
183
  end
184
184
 
185
185
  describe '#request' do
186
- it 'catches Faraday errors' do
187
- allow(subject).to receive(:connection).and_raise(Faraday::Error::ClientError.new('Oops'))
188
- expect(lambda { subject.send(:request, :get, '/path') }).to raise_error(DeskApi::Error::ClientError)
186
+ context 'Faraday::Error::ClientError' do
187
+ before do
188
+ allow(subject).to receive(:connection).and_raise(Faraday::Error::ClientError.new('Oops'))
189
+ end
190
+
191
+ it 'catches the error' do
192
+ expect(lambda { subject.send(:request, :get, '/path') }).to raise_error(DeskApi::Error::ClientError)
193
+ end
194
+
195
+ it 'wraps the original error' do
196
+ begin
197
+ subject.send(:request, :get, '/path')
198
+ rescue DeskApi::Error::ClientError => err
199
+ expect(err.instance_variable_get(:@wrapped_err)).to be_an_instance_of(Faraday::Error::ClientError)
200
+ end
201
+ end
189
202
  end
190
203
 
191
- it 'catches JSON::ParserError errors' do
192
- allow(subject).to receive(:connection).and_raise(JSON::ParserError.new('unexpected token'))
193
- expect(lambda { subject.send(:request, :get, '/path') }).to raise_error(DeskApi::Error::ParserError)
204
+ context 'JSON::ParserError' do
205
+ before do
206
+ allow(subject).to receive(:connection).and_raise(JSON::ParserError.new('unexpected token'))
207
+ end
208
+
209
+ it 'catches the error' do
210
+ expect(lambda { subject.send(:request, :get, '/path') }).to raise_error(DeskApi::Error::ParserError)
211
+ end
212
+
213
+ it 'wraps the original error' do
214
+ begin
215
+ subject.send(:request, :get, '/path')
216
+ rescue DeskApi::Error::ParserError => err
217
+ expect(err.instance_variable_get(:@wrapped_err)).to be_an_instance_of(JSON::ParserError)
218
+ end
219
+ end
194
220
  end
195
221
  end
196
222
  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,
@@ -199,13 +199,13 @@ describe DeskApi::Configuration do
199
199
  end
200
200
 
201
201
  it 'returns false if no authentication credentials are set' do
202
- expect(@client.credentials?).to be_false
202
+ expect(@client.credentials?).to eq(false)
203
203
  end
204
204
 
205
205
  it 'returns true if basic auth credentials are set' do
206
206
  @client.username = 'UN'
207
207
  @client.password = 'PW'
208
- expect(@client.credentials?).to be_true
208
+ expect(@client.credentials?).to eq(true)
209
209
  end
210
210
 
211
211
  it 'returns true if oauth credentials are set' do
@@ -213,7 +213,7 @@ describe DeskApi::Configuration do
213
213
  @client.consumer_secret = 'CS'
214
214
  @client.token = 'TOK'
215
215
  @client.token_secret = 'TOKS'
216
- expect(@client.credentials?).to be_true
216
+ expect(@client.credentials?).to eq(true)
217
217
  end
218
218
  end
219
219
  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,
@@ -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,
@@ -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,
@@ -0,0 +1,63 @@
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
+ require 'desk_api/request/encode_dates'
31
+
32
+ describe DeskApi::Request::EncodeDates do
33
+ before(:all) do
34
+ VCR.turn_off!
35
+
36
+ @time = Time.now
37
+ @stubs = Faraday::Adapter::Test::Stubs.new
38
+ @conn = Faraday.new do |builder|
39
+ builder.request :desk_encode_dates
40
+ builder.request :desk_encode_json
41
+ builder.adapter :test, @stubs
42
+ end
43
+ end
44
+
45
+ after(:all) do
46
+ VCR.turn_on!
47
+ end
48
+
49
+ it 'encodes the date, datetime and time to iso8601' do
50
+ @stubs.post('/echo') do |env|
51
+ body = JSON.parse(env[:body], symbolize_names: true)
52
+ expect(body[:date]).to eq(@time.to_date.to_time.utc.iso8601)
53
+ expect(body[:datetime]).to eq(@time.utc.iso8601)
54
+ expect(body[:time]).to eq(@time.utc.iso8601)
55
+ end
56
+
57
+ @conn.post('http://localhost/echo', {
58
+ date: @time.to_date,
59
+ datetime: @time.to_datetime,
60
+ time: @time
61
+ })
62
+ end
63
+ 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,
@@ -30,7 +30,7 @@ require 'spec_helper'
30
30
  require 'desk_api/request/encode_json'
31
31
 
32
32
  describe DeskApi::Request::EncodeJson do
33
- before do
33
+ before(:all) do
34
34
  VCR.turn_off!
35
35
 
36
36
  @stubs = Faraday::Adapter::Test::Stubs.new
@@ -40,7 +40,7 @@ describe DeskApi::Request::EncodeJson do
40
40
  end
41
41
  end
42
42
 
43
- after do
43
+ after(:all) do
44
44
  VCR.turn_on!
45
45
  end
46
46
 
@@ -54,7 +54,7 @@ describe DeskApi::Request::EncodeJson do
54
54
 
55
55
  it 'encodes the body into json' do
56
56
  @stubs.post('/echo') do |env|
57
- expect(!!JSON.parse(env[:body])).to be_true
57
+ expect(!!JSON.parse(env[:body])).to eq(true)
58
58
  end
59
59
  @conn.post('http://localhost/echo', test: 'test')
60
60
  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,
@@ -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,
@@ -0,0 +1,45 @@
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
+ require 'fileutils'
31
+
32
+ describe DeskApi::Resource::Download do
33
+ subject { @client ||= DeskApi::Client.new DeskApi::CONFIG }
34
+
35
+ it 'throws an error on non attachment resources', :vcr do
36
+ expect(lambda { subject.cases.find(3065).download })
37
+ .to raise_error(NoMethodError)
38
+ end
39
+
40
+ it 'downloads the attachment', :vcr do
41
+ a = subject.cases.find(3065).attachments.entries.first.download
42
+ b = File.open(File.join(RSpec.configuration.root_path, 'stubs', 'file.jpg'))
43
+ expect(a.read).to eq(b.read)
44
+ end
45
+ end
@@ -0,0 +1,77 @@
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::Pagination do
32
+ subject { @client ||= DeskApi::Client.new DeskApi::CONFIG }
33
+
34
+ context '#all' do
35
+ it 'iterates over each resource on each page', :vcr do
36
+ subject.cases.all do |resource, page_num|
37
+ expect(resource).to be_an_instance_of(DeskApi::Resource)
38
+ expect(resource.resource_type).to eq('case')
39
+ expect(page_num).to be_an_instance_of(Fixnum)
40
+ end
41
+ end
42
+
43
+ it 'raises an argument error if no block is given' do
44
+ expect { subject.cases.all }.to raise_error(ArgumentError)
45
+ end
46
+ end
47
+
48
+ context '#each_page' do
49
+ it 'iterates over each page', :vcr do
50
+ subject.cases.each_page do |page, page_number|
51
+ expect(page).to be_an_instance_of(DeskApi::Resource)
52
+ expect(page.resource_type).to eq('page')
53
+ expect(page_number).to be_an_instance_of(Fixnum)
54
+ end
55
+ end
56
+
57
+ it 'uses a default per_page of 1000', :vcr do
58
+ subject.cases.each_page do |page, page_number|
59
+ expect((page.query_params['per_page'].to_i % 10)).to eq(0)
60
+ end
61
+ end
62
+
63
+ it 'uses per_page from query_params if present' do
64
+ subject.cases.per_page(25) do |page, page_number|
65
+ expect(page.query_params['per_page']).to eq(25)
66
+ end
67
+ end
68
+
69
+ it 'raises ArgumentError if no block is given' do
70
+ expect { subject.cases.each_page }.to raise_error(ArgumentError)
71
+ end
72
+
73
+ it 'raises NoMethodError is called on non-page resources', :vcr do
74
+ expect { subject.cases.entries.first.each_page { |x| x } }.to raise_error(NoMethodError)
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,145 @@
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::QueryParams do
32
+ subject { @client ||= DeskApi::Client.new DeskApi::CONFIG }
33
+
34
+ describe 'embeddable' do
35
+ it 'allows to declare embedds' do
36
+ expect(lambda { subject.cases.embed(:assigned_user) }).not_to raise_error
37
+ end
38
+
39
+ it 'changes the url' do
40
+ expect(subject.cases.embed(:assigned_user).href).to eq('/api/v2/cases?embed=assigned_user')
41
+ end
42
+
43
+ context 'if you use embed' do
44
+ before do
45
+ VCR.turn_off! ignore_cassettes: true
46
+
47
+ @stubs ||= Faraday::Adapter::Test::Stubs.new
48
+ @client ||= DeskApi::Client.new(DeskApi::CONFIG).tap do |client|
49
+ client.middleware = Proc.new do |builder|
50
+ builder.response :desk_parse_dates
51
+ builder.response :desk_parse_json
52
+
53
+ builder.adapter :test, @stubs
54
+ end
55
+ end
56
+ end
57
+
58
+ after do
59
+ VCR.turn_on!
60
+ end
61
+
62
+ it 'does not load the resource again' do
63
+ times_called = 0
64
+ @stubs.get('/api/v2/cases?embed=assigned_user') do
65
+ times_called += 1
66
+ [
67
+ 200,
68
+ { 'content-type' => 'application/json' },
69
+ File.open(File.join(RSpec.configuration.root_path, 'stubs', 'cases_embed_assigned_user.json')).read
70
+ ]
71
+ end
72
+
73
+ first_case = @client.cases.embed(:assigned_user).entries.first
74
+ expect(first_case.assigned_user.name).to eq('Thomas Stachl')
75
+ expect(first_case.assigned_user.instance_variable_get(:@_loaded)).to eq(true)
76
+ expect(times_called).to eq(1)
77
+ end
78
+
79
+ it 'can be used in finder' do
80
+ @stubs.get('/api/v2/cases/3011?embed=customer') do
81
+ [
82
+ 200,
83
+ { 'content-type' => 'application/json' },
84
+ File.open(File.join(RSpec.configuration.root_path, 'stubs', 'case_embed_customer.json')).read
85
+ ]
86
+ end
87
+
88
+ customer = @client.cases.find(3011, embed: :customer).customer
89
+ expect(customer.first_name).to eq('Thomas')
90
+ customer = @client.cases.find(3011, embed: [:customer]).customer
91
+ expect(customer.first_name).to eq('Thomas')
92
+ end
93
+ end
94
+ end
95
+
96
+ context '#query_params' do
97
+ before do
98
+ @page = DeskApi::Resource.new(subject, {
99
+ '_links'=>{'self'=>{'href'=>'/api/v2/cases?page=2&per_page=50'}}
100
+ }, true)
101
+ end
102
+
103
+ it 'allows to get query params from the current resource' do
104
+ expect(@page.send(:query_params_include?, 'page')).to eq('2')
105
+ expect(@page.send(:query_params_include?, 'per_page')).to eq('50')
106
+ end
107
+
108
+ it 'returns nil if param not found' do
109
+ expect(@page.send(:query_params_include?, 'blup')).to eq(nil)
110
+ end
111
+ end
112
+
113
+ context '#query_params=' do
114
+ before do
115
+ @page = DeskApi::Resource.new(subject, {
116
+ '_links'=>{'self'=>{'href'=>'/api/v2/cases'}}
117
+ }, true)
118
+ end
119
+
120
+ it 'sets query params on the current url' do
121
+ @page.send(:query_params=, { page: 5, per_page: 50 })
122
+ expect(@page.instance_variable_get(:@_definition)['_links']['self']['href']).to eq('/api/v2/cases?page=5&per_page=50')
123
+ end
124
+ end
125
+
126
+ context '#page' do
127
+ it 'returns the current page and loads if page not defined', :vcr do
128
+ expect(subject.articles.page).to eq(1)
129
+ end
130
+
131
+ it 'sets the page' do
132
+ expect(subject.cases.page(5).page).to eq(5)
133
+ end
134
+
135
+ it 'sets the resource to not loaded', :vcr do
136
+ cases = subject.cases.send(:exec!)
137
+ expect(cases.page(5).instance_variable_get(:@_loaded)).to eq(false)
138
+ end
139
+
140
+ it 'keeps the resource as loaded', :vcr do
141
+ cases = subject.cases.send(:exec!)
142
+ expect(cases.page(1).instance_variable_get(:@_loaded)).to eq(true)
143
+ end
144
+ end
145
+ end