desk_api 0.5.8 → 0.6.0
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.
- checksums.yaml +4 -4
- data/LICENSE +2 -2
- data/README.md +274 -120
- data/lib/desk_api.rb +2 -2
- data/lib/desk_api/client.rb +6 -1
- data/lib/desk_api/configuration.rb +34 -26
- data/lib/desk_api/request/encode_json.rb +11 -0
- data/lib/desk_api/request/oauth.rb +20 -0
- data/lib/desk_api/request/retry.rb +37 -42
- data/lib/desk_api/resource.rb +86 -22
- data/lib/desk_api/response/parse_dates.rb +28 -0
- data/lib/desk_api/response/parse_json.rb +9 -0
- data/lib/desk_api/response/raise_error.rb +11 -16
- data/lib/desk_api/version.rb +1 -1
- data/spec/cassettes/DeskApi_Resource/_all/iterates_over_each_resource_on_each_page.yml +1953 -0
- data/spec/cassettes/DeskApi_Resource/_each_page/iterates_over_each_page.yml +1953 -0
- data/spec/cassettes/DeskApi_Resource/_each_page/raises_NoMethodError_is_called_on_non-page_resources.yml +207 -0
- data/spec/cassettes/DeskApi_Resource/_each_page/uses_a_default_per_page_of_1000.yml +1953 -0
- data/spec/cassettes/DeskApi_Resource/_load/loads_the_resource_if_not_already_loaded.yml +205 -0
- data/spec/cassettes/DeskApi_Resource/_loaded_/returns_true_if_the_resource_is_loaded.yml +205 -0
- data/spec/cassettes/DeskApi_Resource/_next_/changes__definition_to_next_page.yml +407 -0
- data/spec/cassettes/DeskApi_Resource/_next_/returns_nil_on_the_last_page.yml +315 -0
- data/spec/cassettes/DeskApi_Resource/_request/sends_request_through_a_client_and_returns_Faraday_Response.yml +207 -0
- data/spec/cassettes/DeskApi_Resource/_reset_/sets__links__embedded__changed_and__loaded_to_default_values.yml +253 -0
- data/spec/cassettes/DeskApi_Resource/_to_hash/converts_embedded_resources_to_hashes.yml +251 -0
- data/spec/cassettes/DeskApi_Resource/_to_hash/returns_a_hash_for_a_desk_resource.yml +66 -0
- data/spec/cassettes/DeskApi_Resource/_update/can_handle_action_params.yml +427 -0
- data/spec/desk_api/client_spec.rb +23 -17
- data/spec/desk_api/configuration_spec.rb +49 -41
- data/spec/desk_api/default_spec.rb +3 -3
- data/spec/desk_api/error_spec.rb +9 -7
- data/spec/desk_api/rate_limit_spec.rb +1 -1
- data/spec/desk_api/request/encode_json_spec.rb +33 -0
- data/spec/desk_api/request/oauth_spec.rb +31 -0
- data/spec/desk_api/request/retry_spec.rb +4 -4
- data/spec/desk_api/resource_spec.rb +247 -71
- data/spec/desk_api/response/parse_dates_spec.rb +34 -0
- data/spec/desk_api/response/parse_json_spec.rb +34 -0
- data/spec/desk_api/response/raise_error_spec.rb +31 -0
- data/spec/desk_api_spec.rb +6 -6
- data/spec/spec_helper.rb +2 -2
- data/spec/stubs/article.json +51 -0
- data/spec/stubs/to_hash_embed.json +1 -0
- metadata +78 -28
- data/.coveralls.yml +0 -1
- data/.gitignore +0 -23
- data/.rspec +0 -1
- data/.travis.yml +0 -4
- data/.yardopts +0 -3
- data/Gemfile +0 -10
- data/Guardfile +0 -5
- data/Rakefile +0 -32
- data/desk_api.gemspec +0 -32
@@ -43,7 +43,7 @@ describe DeskApi::Client do
|
|
43
43
|
it 'inherits the module configuration' do
|
44
44
|
client = DeskApi::Client.new
|
45
45
|
DeskApi::Configuration.keys.each do |key|
|
46
|
-
client.instance_variable_get(:"@#{key}").
|
46
|
+
expect(client.instance_variable_get(:"@#{key}")).to eq(@configuration[key])
|
47
47
|
end
|
48
48
|
end
|
49
49
|
end
|
@@ -53,7 +53,7 @@ describe DeskApi::Client do
|
|
53
53
|
it "overrides the module configuration" do
|
54
54
|
client = DeskApi::Client.new(@configuration)
|
55
55
|
DeskApi::Configuration.keys.each do |key|
|
56
|
-
client.instance_variable_get(:"@#{key}").
|
56
|
+
expect(client.instance_variable_get(:"@#{key}")).to eq(@configuration[key])
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
@@ -66,9 +66,9 @@ describe DeskApi::Client do
|
|
66
66
|
config.send("#{key}=", value)
|
67
67
|
end
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
DeskApi::Configuration.keys.each do |key|
|
71
|
-
client.instance_variable_get(:"@#{key}").
|
71
|
+
expect(client.instance_variable_get(:"@#{key}")).to eq(@configuration[key])
|
72
72
|
end
|
73
73
|
end
|
74
74
|
end
|
@@ -79,26 +79,26 @@ describe DeskApi::Client do
|
|
79
79
|
describe '#get', :vcr do
|
80
80
|
it 'fetches resources' do
|
81
81
|
response = subject.get('/api/v2/cases/3014')
|
82
|
-
response.body['subject'].
|
82
|
+
expect(response.body['subject']).to eq('Testing Quick Case')
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
86
|
describe '#post', :vcr do
|
87
87
|
it 'creates a resource' do
|
88
88
|
response = subject.post('/api/v2/topics', @topic_create_data)
|
89
|
-
response.body['name'].
|
89
|
+
expect(response.body['name']).to eq(@topic_create_data[:name])
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
93
93
|
describe '#patch', :vcr do
|
94
94
|
it 'updates a resource' do
|
95
|
-
subject.patch('/api/v2/topics/601117', @topic_update_data).body['name'].
|
95
|
+
expect(subject.patch('/api/v2/topics/601117', @topic_update_data).body['name']).to eq(@topic_update_data[:name])
|
96
96
|
end
|
97
97
|
end
|
98
98
|
|
99
99
|
describe '#delete', :vcr do
|
100
100
|
it 'deletes a resource' do
|
101
|
-
subject.delete('/api/v2/topics/601117').status.
|
101
|
+
expect(subject.delete('/api/v2/topics/601117').status).to eq(204)
|
102
102
|
end
|
103
103
|
end
|
104
104
|
end
|
@@ -113,50 +113,56 @@ describe DeskApi::Client do
|
|
113
113
|
describe '#get', :vcr do
|
114
114
|
it 'fetches resources' do
|
115
115
|
response = @client.get('/api/v2/articles/1391017')
|
116
|
-
response.body['subject'].
|
116
|
+
expect(response.body['subject']).to eq('Testing OAuth')
|
117
117
|
end
|
118
118
|
end
|
119
119
|
|
120
120
|
describe '#post', :vcr do
|
121
121
|
it 'creates a resource' do
|
122
122
|
response = @client.post('/api/v2/articles', @article_create_data)
|
123
|
-
response.body['subject'].
|
123
|
+
expect(response.body['subject']).to eq(@article_create_data[:subject])
|
124
124
|
end
|
125
125
|
end
|
126
126
|
|
127
127
|
describe '#patch', :vcr do
|
128
128
|
it 'updates a resource' do
|
129
|
-
@client.patch('/api/v2/articles/1391017', @article_update_data).body['subject'].
|
129
|
+
expect(@client.patch('/api/v2/articles/1391017', @article_update_data).body['subject']).to eq(@article_update_data[:subject])
|
130
130
|
end
|
131
131
|
end
|
132
132
|
|
133
133
|
describe '#delete', :vcr do
|
134
134
|
it 'deletes a resource' do
|
135
|
-
@client.delete('/api/v2/articles/1391017').status.
|
135
|
+
expect(@client.delete('/api/v2/articles/1391017').status).to eq(204)
|
136
136
|
end
|
137
137
|
end
|
138
138
|
end
|
139
139
|
|
140
|
+
describe '#by_url', :vcr do
|
141
|
+
it 'finds resources by url' do
|
142
|
+
expect(subject.by_url('/api/v2/articles/1295677')).to be_an_instance_of(DeskApi::Resource)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
140
146
|
describe '#connection' do
|
141
147
|
it 'looks like Faraday connection' do
|
142
|
-
subject.send(:connection).
|
148
|
+
expect(subject.send(:connection)).to be_an_instance_of(Faraday::Connection)
|
143
149
|
end
|
144
150
|
|
145
151
|
it 'memoizes the connection' do
|
146
152
|
c1, c2 = subject.send(:connection), subject.send(:connection)
|
147
|
-
c1.
|
153
|
+
expect(c1).to equal(c2)
|
148
154
|
end
|
149
155
|
end
|
150
156
|
|
151
157
|
describe '#request' do
|
152
158
|
it 'catches Faraday errors' do
|
153
159
|
allow(subject).to receive(:connection).and_raise(Faraday::Error::ClientError.new('Oops'))
|
154
|
-
lambda { subject.send(:request, :get, '/path') }.
|
160
|
+
expect(lambda { subject.send(:request, :get, '/path') }).to raise_error(DeskApi::Error::ClientError)
|
155
161
|
end
|
156
162
|
|
157
163
|
it 'catches JSON::ParserError errors' do
|
158
164
|
allow(subject).to receive(:connection).and_raise(JSON::ParserError.new('unexpected token'))
|
159
|
-
lambda { subject.send(:request, :get, '/path') }.
|
165
|
+
expect(lambda { subject.send(:request, :get, '/path') }).to raise_error(DeskApi::Error::ParserError)
|
160
166
|
end
|
161
167
|
end
|
162
|
-
end
|
168
|
+
end
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe DeskApi::Configuration do
|
4
4
|
context '#keys' do
|
5
5
|
it 'returns an array of configuration keys' do
|
6
|
-
DeskApi::Configuration.keys.
|
6
|
+
expect(DeskApi::Configuration.keys).to eq([
|
7
7
|
:consumer_key,
|
8
8
|
:consumer_secret,
|
9
9
|
:token,
|
@@ -24,18 +24,18 @@ describe DeskApi::Configuration do
|
|
24
24
|
|
25
25
|
it 'returns the endpoint if set' do
|
26
26
|
DeskApi.endpoint = 'https://devel.desk.com'
|
27
|
-
DeskApi.endpoint.
|
27
|
+
expect(DeskApi.endpoint).to eq('https://devel.desk.com')
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'returns the subdomain endpoint if subdomain is set' do
|
31
31
|
DeskApi.subdomain = 'devel'
|
32
|
-
DeskApi.endpoint.
|
32
|
+
expect(DeskApi.endpoint).to eq('https://devel.desk.com')
|
33
33
|
end
|
34
34
|
|
35
35
|
it 'gives presidence to the endpoint' do
|
36
36
|
DeskApi.subdomain = 'subdomain'
|
37
37
|
DeskApi.endpoint = 'https://endpoint.desk.com'
|
38
|
-
DeskApi.endpoint.
|
38
|
+
expect(DeskApi.endpoint).to eq('https://endpoint.desk.com')
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
@@ -65,59 +65,67 @@ describe DeskApi::Configuration do
|
|
65
65
|
end
|
66
66
|
|
67
67
|
DeskApi::Configuration.keys.each do |key|
|
68
|
-
client.instance_variable_get(:"@#{key}").
|
68
|
+
expect(client.instance_variable_get(:"@#{key}")).to eq(@configuration[key])
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
72
|
it 'throws an exception if credentials are not set' do
|
73
73
|
client = DeskApi::Client.new
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
74
|
+
expect(
|
75
|
+
lambda {
|
76
|
+
client.configure do |config|
|
77
|
+
@configuration.each do |key, value|
|
78
|
+
config.send("#{key}=", value)
|
79
|
+
end
|
80
|
+
config.username = nil
|
81
|
+
config.consumer_key = nil
|
78
82
|
end
|
79
|
-
|
80
|
-
|
81
|
-
end
|
82
|
-
}.should raise_error(DeskApi::Error::ConfigurationError)
|
83
|
+
}
|
84
|
+
).to raise_error(DeskApi::Error::ConfigurationError)
|
83
85
|
end
|
84
86
|
|
85
87
|
it 'throws an exception if basic auth credentials are invalid' do
|
86
88
|
client = DeskApi::Client.new
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
89
|
+
expect(
|
90
|
+
lambda {
|
91
|
+
client.configure do |config|
|
92
|
+
@configuration.each do |key, value|
|
93
|
+
config.send("#{key}=", value)
|
94
|
+
end
|
95
|
+
config.username = 1
|
96
|
+
config.consumer_key = nil
|
91
97
|
end
|
92
|
-
|
93
|
-
|
94
|
-
end
|
95
|
-
}.should raise_error(DeskApi::Error::ConfigurationError)
|
98
|
+
}
|
99
|
+
).to raise_error(DeskApi::Error::ConfigurationError)
|
96
100
|
end
|
97
101
|
|
98
102
|
it 'throws an exception if oauth credentials are invalid' do
|
99
103
|
client = DeskApi::Client.new
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
+
expect(
|
105
|
+
lambda {
|
106
|
+
client.configure do |config|
|
107
|
+
@configuration.each do |key, value|
|
108
|
+
config.send("#{key}=", value)
|
109
|
+
end
|
110
|
+
config.username = nil
|
111
|
+
config.consumer_key = 1
|
104
112
|
end
|
105
|
-
|
106
|
-
|
107
|
-
end
|
108
|
-
}.should raise_error(DeskApi::Error::ConfigurationError)
|
113
|
+
}
|
114
|
+
).to raise_error(DeskApi::Error::ConfigurationError)
|
109
115
|
end
|
110
116
|
|
111
117
|
it 'throws an exception if endpoint is not a valid url' do
|
112
118
|
client = DeskApi::Client.new
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
119
|
+
expect(
|
120
|
+
lambda {
|
121
|
+
client.configure do |config|
|
122
|
+
@configuration.each do |key, value|
|
123
|
+
config.send("#{key}=", value)
|
124
|
+
end
|
125
|
+
config.endpoint = 'some_funky_endpoint'
|
117
126
|
end
|
118
|
-
|
119
|
-
|
120
|
-
}.should raise_error(DeskApi::Error::ConfigurationError)
|
127
|
+
}
|
128
|
+
).to raise_error(DeskApi::Error::ConfigurationError)
|
121
129
|
end
|
122
130
|
end
|
123
131
|
|
@@ -148,7 +156,7 @@ describe DeskApi::Configuration do
|
|
148
156
|
client.reset!
|
149
157
|
|
150
158
|
DeskApi::Configuration.keys.each do |key|
|
151
|
-
client.instance_variable_get(:"@#{key}").
|
159
|
+
expect(client.instance_variable_get(:"@#{key}")).not_to eq(@configuration[key])
|
152
160
|
end
|
153
161
|
end
|
154
162
|
end
|
@@ -163,13 +171,13 @@ describe DeskApi::Configuration do
|
|
163
171
|
end
|
164
172
|
|
165
173
|
it 'returns false if no authentication credentials are set' do
|
166
|
-
@client.credentials
|
174
|
+
expect(@client.credentials?).to be_false
|
167
175
|
end
|
168
176
|
|
169
177
|
it 'returns true if basic auth credentials are set' do
|
170
178
|
@client.username = 'UN'
|
171
179
|
@client.password = 'PW'
|
172
|
-
@client.credentials
|
180
|
+
expect(@client.credentials?).to be_true
|
173
181
|
end
|
174
182
|
|
175
183
|
it 'returns true if oauth credentials are set' do
|
@@ -177,7 +185,7 @@ describe DeskApi::Configuration do
|
|
177
185
|
@client.consumer_secret = 'CS'
|
178
186
|
@client.token = 'TOK'
|
179
187
|
@client.token_secret = 'TOKS'
|
180
|
-
@client.credentials
|
188
|
+
expect(@client.credentials?).to be_true
|
181
189
|
end
|
182
190
|
end
|
183
|
-
end
|
191
|
+
end
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe DeskApi::Default do
|
4
4
|
context '#options' do
|
5
5
|
it 'returns a hash with mostly nil values' do
|
6
|
-
DeskApi::Default.options.
|
6
|
+
expect(DeskApi::Default.options).to eq({
|
7
7
|
consumer_key: nil,
|
8
8
|
consumer_secret: nil,
|
9
9
|
token: nil,
|
@@ -35,7 +35,7 @@ describe DeskApi::Default do
|
|
35
35
|
ENV['DESK_SUBDOMAIN'] = 'SD'
|
36
36
|
ENV['DESK_ENDPOINT'] = 'EP'
|
37
37
|
|
38
|
-
DeskApi::Default.options.
|
38
|
+
expect(DeskApi::Default.options).to eq({
|
39
39
|
consumer_key: 'CK',
|
40
40
|
consumer_secret: 'CS',
|
41
41
|
token: 'TOK',
|
@@ -66,4 +66,4 @@ describe DeskApi::Default do
|
|
66
66
|
ENV['DESK_ENDPOINT'] = nil
|
67
67
|
end
|
68
68
|
end
|
69
|
-
end
|
69
|
+
end
|
data/spec/desk_api/error_spec.rb
CHANGED
@@ -7,16 +7,18 @@ describe DeskApi::Error do
|
|
7
7
|
|
8
8
|
context '.from_response' do
|
9
9
|
it 'can be created from a faraday response', :vcr do
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
expect(
|
11
|
+
lambda {
|
12
|
+
subject.articles.create({ subject: 'Testing', body: 'Testing' })
|
13
|
+
}
|
14
|
+
).to raise_error(DeskApi::Error::UnprocessableEntity)
|
13
15
|
end
|
14
16
|
|
15
17
|
it 'uses the body message if present', :vcr do
|
16
18
|
begin
|
17
19
|
subject.articles.create({ subject: 'Testing', body: 'Testing' })
|
18
20
|
rescue DeskApi::Error::UnprocessableEntity => e
|
19
|
-
e.message.
|
21
|
+
expect(e.message).to eq('Validation Failed')
|
20
22
|
end
|
21
23
|
end
|
22
24
|
end
|
@@ -26,9 +28,9 @@ describe DeskApi::Error do
|
|
26
28
|
begin
|
27
29
|
subject.articles.create({ subject: 'Testing', body: 'Testing' })
|
28
30
|
rescue DeskApi::Error::UnprocessableEntity => e
|
29
|
-
e.errors.
|
30
|
-
e.errors.
|
31
|
+
expect(e.errors).to be_an_instance_of(Hash)
|
32
|
+
expect(e.errors).to eq({"_links" => { "topic" => ["blank"]}})
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
34
|
-
end
|
36
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'desk_api/request/encode_json'
|
3
|
+
|
4
|
+
describe DeskApi::Request::EncodeJson do
|
5
|
+
before do
|
6
|
+
VCR.turn_off!
|
7
|
+
|
8
|
+
@stubs = Faraday::Adapter::Test::Stubs.new
|
9
|
+
@conn = Faraday.new do |builder|
|
10
|
+
builder.request :desk_encode_json
|
11
|
+
builder.adapter :test, @stubs
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
after do
|
16
|
+
VCR.turn_on!
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'sets the content type header' do
|
20
|
+
@stubs.post('/echo') do |env|
|
21
|
+
expect(env[:request_headers]).to have_key('Content-Type')
|
22
|
+
expect(env[:request_headers]['Content-Type']).to eql('application/json')
|
23
|
+
end
|
24
|
+
@conn.post('http://localhost/echo', test: 'test')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'encodes the body into json' do
|
28
|
+
@stubs.post('/echo') do |env|
|
29
|
+
expect(!!JSON.parse(env[:body])).to be_true
|
30
|
+
end
|
31
|
+
@conn.post('http://localhost/echo', test: 'test')
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'desk_api/request/oauth'
|
3
|
+
|
4
|
+
describe DeskApi::Request::OAuth do
|
5
|
+
before do
|
6
|
+
VCR.turn_off!
|
7
|
+
|
8
|
+
@stubs = Faraday::Adapter::Test::Stubs.new
|
9
|
+
@conn = Faraday.new do |builder|
|
10
|
+
builder.request :desk_oauth, {
|
11
|
+
consumer_key: 'consumer_key',
|
12
|
+
consumer_secret: 'consumer_secret',
|
13
|
+
token: 'token',
|
14
|
+
token_secret: "token_secret"
|
15
|
+
}
|
16
|
+
builder.adapter :test, @stubs
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
after do
|
21
|
+
VCR.turn_on!
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'sets the authorization header' do
|
25
|
+
@stubs.post('/echo') do |env|
|
26
|
+
expect(env[:request_headers]).to have_key('Authorization')
|
27
|
+
expect(env[:request_headers]['Authorization']).to include('OAuth')
|
28
|
+
end
|
29
|
+
@conn.post('http://localhost/echo')
|
30
|
+
end
|
31
|
+
end
|
@@ -7,7 +7,7 @@ describe DeskApi::Request::Retry do
|
|
7
7
|
|
8
8
|
@stubs = Faraday::Adapter::Test::Stubs.new
|
9
9
|
@conn = Faraday.new do |builder|
|
10
|
-
builder.request :
|
10
|
+
builder.request :desk_retry, { interval: 0 }
|
11
11
|
builder.adapter :test, @stubs
|
12
12
|
end
|
13
13
|
end
|
@@ -25,7 +25,7 @@ describe DeskApi::Request::Retry do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
@conn.post('http://localhost/echo') rescue nil
|
28
|
-
times_called.
|
28
|
+
expect(times_called).to eq(4)
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'retries once if we have too many requests' do
|
@@ -47,6 +47,6 @@ describe DeskApi::Request::Retry do
|
|
47
47
|
end
|
48
48
|
|
49
49
|
@conn.post('http://localhost/echo') rescue nil
|
50
|
-
times_called.
|
50
|
+
expect(times_called).to eq(2)
|
51
51
|
end
|
52
|
-
end
|
52
|
+
end
|