desk 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.mkd CHANGED
@@ -1,3 +1,9 @@
1
+ 0.3.2 February 25, 2013
2
+ ------------------
3
+ Support for groups (Thanks to @tstachl)
4
+ Support for max_requests (Thanks to @tstachl)
5
+ New multi_json and multi_xml version
6
+
1
7
  0.3.0 July 15, 2012
2
8
  ------------------
3
9
  Renamed from Assistly to Desk
data/desk.gemspec CHANGED
@@ -11,14 +11,13 @@ Gem::Specification.new do |s|
11
11
  s.add_development_dependency('simplecov', '~> 0.4')
12
12
  s.add_development_dependency('webmock', '~> 1.6')
13
13
  s.add_development_dependency('yard', '~> 0.6')
14
- s.add_development_dependency('ZenTest', '~> 4.5')
15
14
  s.add_runtime_dependency('json', '~> 1.7') if RUBY_VERSION < '1.9'
16
15
  s.add_runtime_dependency('hashie', '~> 1.2.0')
17
16
  s.add_runtime_dependency('faraday', '~> 0.8.0')
18
17
  s.add_runtime_dependency('faraday_middleware', '~> 0.8.0')
19
18
  s.add_runtime_dependency('jruby-openssl', '~> 0.7.2') if RUBY_PLATFORM == 'java'
20
- s.add_runtime_dependency('multi_json', '~> 1.3')
21
- s.add_runtime_dependency('multi_xml', '~> 0.4.0')
19
+ s.add_runtime_dependency('multi_json', '~> 1.6')
20
+ s.add_runtime_dependency('multi_xml', '~> 0.5')
22
21
  s.add_runtime_dependency('rash', '~> 0.3.1')
23
22
  s.add_runtime_dependency('simple_oauth', '~> 0.1.4')
24
23
  s.add_runtime_dependency('pony', '~> 1.1')
data/lib/desk.rb CHANGED
@@ -6,21 +6,27 @@ require 'pony'
6
6
 
7
7
  module Desk
8
8
  extend Configuration
9
+ @counter = 0
10
+ @minute = Time.now.min
9
11
 
10
- # Alias for Desk::Client.new
11
- #
12
- # @return [Desk::Client]
13
- def self.client(options={})
14
- Desk::Client.new(options)
15
- end
12
+ class << self
13
+ attr_accessor :counter, :minute
14
+
15
+ # Alias for Desk::Client.new
16
+ #
17
+ # @return [Desk::Client]
18
+ def client(options={})
19
+ Desk::Client.new(options)
20
+ end
16
21
 
17
- # Delegate to Desk::Client
18
- def self.method_missing(method, *args, &block)
19
- return super unless client.respond_to?(method)
20
- client.send(method, *args, &block)
21
- end
22
+ # Delegate to Desk::Client
23
+ def method_missing(method, *args, &block)
24
+ return super unless client.respond_to?(method)
25
+ client.send(method, *args, &block)
26
+ end
22
27
 
23
- def self.respond_to?(method)
24
- client.respond_to?(method) || super
28
+ def respond_to?(method)
29
+ client.respond_to?(method) || super
30
+ end
25
31
  end
26
32
  end
data/lib/desk/client.rb CHANGED
@@ -8,6 +8,7 @@ module Desk
8
8
  # order to avoid a superclass mismatch error, allowing those modules to be
9
9
  # Client-namespaced.
10
10
  require 'desk/client/user'
11
+ require 'desk/client/group'
11
12
  require 'desk/client/interaction'
12
13
  require 'desk/client/case'
13
14
  require 'desk/client/customer'
@@ -18,6 +19,7 @@ module Desk
18
19
  alias :api_endpoint :endpoint
19
20
 
20
21
  include Desk::Client::User
22
+ include Desk::Client::Group
21
23
  include Desk::Client::Interaction
22
24
  include Desk::Client::Case
23
25
  include Desk::Client::Customer
@@ -0,0 +1,37 @@
1
+ module Desk
2
+ class Client
3
+ # Defines methods related to groups
4
+ module Group
5
+ # Returns extended information of groups
6
+ #
7
+ # @option options [Boolean, String, Integer]
8
+ # @example Return extended information for 12345
9
+ # Desk.groups
10
+ # Desk.groups(:count => 5)
11
+ # Desk.groups(:count => 5, :page => 3)
12
+ # @format :json
13
+ # @authenticated true
14
+ # @see http://dev.desk.com/docs/api/groups
15
+ def groups(*args)
16
+ options = args.last.is_a?(Hash) ? args.pop : {}
17
+ response = get("groups",options)
18
+ response
19
+ end
20
+
21
+ # Returns extended information on a single group
22
+ #
23
+ # @param id [Integer] a group ID
24
+ # @option options [Hash]
25
+ # @example Return extended information for 12345
26
+ # Desk.group(12345)
27
+ # @format :json
28
+ # @authenticated true
29
+ # @see http://dev.desk.com/docs/api/groups/show
30
+ def group(id)
31
+ response = get("groups/#{id}")
32
+ response.group
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -10,11 +10,13 @@ module Desk
10
10
  :consumer_key,
11
11
  :consumer_secret,
12
12
  :format,
13
+ :max_requests,
13
14
  :oauth_token,
14
15
  :oauth_token_secret,
15
16
  :proxy,
16
17
  :subdomain,
17
18
  :support_email,
19
+ :use_max_requests,
18
20
  :user_agent,
19
21
  :version].freeze
20
22
 
@@ -39,6 +41,12 @@ module Desk
39
41
  #
40
42
  # @note JSON is preferred over XML because it is more concise and faster to parse.
41
43
  DEFAULT_FORMAT = :json
44
+
45
+ # By default, set the max requests to 60 per minute
46
+ DEFAULT_MAX_REQUESTS = 60
47
+
48
+ # By default, don't use the max request feature
49
+ DEFAULT_USE_MAX_REQUESTS = false
42
50
 
43
51
  # By default, don't set a user oauth token
44
52
  DEFAULT_OAUTH_TOKEN = nil
@@ -85,11 +93,13 @@ module Desk
85
93
  self.consumer_key = DEFAULT_CONSUMER_KEY
86
94
  self.consumer_secret = DEFAULT_CONSUMER_SECRET
87
95
  self.format = DEFAULT_FORMAT
96
+ self.max_requests = DEFAULT_MAX_REQUESTS
88
97
  self.oauth_token = DEFAULT_OAUTH_TOKEN
89
98
  self.oauth_token_secret = DEFAULT_OAUTH_TOKEN_SECRET
90
99
  self.proxy = DEFAULT_PROXY
91
100
  self.subdomain = DEFAULT_SUBDOMAIN
92
101
  self.support_email = DEFAULT_SUPPORT_EMAIL
102
+ self.use_max_requests = DEFAULT_USE_MAX_REQUESTS
93
103
  self.user_agent = DEFAULT_USER_AGENT
94
104
  self.version = DEFAULT_VERSION
95
105
  self
data/lib/desk/error.rb CHANGED
@@ -50,6 +50,9 @@ module Desk
50
50
  @http_headers.values_at('retry-after', 'Retry-After').detect {|value| value }.to_i
51
51
  end
52
52
  end
53
+
54
+ # Raised when Desk max_requests is reached and use_max_requests is set to true
55
+ class TooManyRequests < StandardError; end
53
56
 
54
57
  # Raised when Desk returns the HTTP status code 500
55
58
  class InternalServerError < Error; end
data/lib/desk/request.rb CHANGED
@@ -22,9 +22,24 @@ module Desk
22
22
  end
23
23
 
24
24
  private
25
+
26
+ def before_request
27
+ if Desk.minute != Time.now.min
28
+ Desk.minute = Time.now.min
29
+ Desk.counter = 0
30
+ end
31
+
32
+ Desk.counter += 1
33
+ if Desk.use_max_requests
34
+ if Desk.counter > Desk.max_requests
35
+ raise Desk::TooManyRequests
36
+ end
37
+ end
38
+ end
25
39
 
26
40
  # Perform an HTTP request
27
41
  def request(method, path, options, raw=false)
42
+ before_request
28
43
  response = connection(raw).send(method) do |request|
29
44
  case method
30
45
  when :get, :delete
data/lib/desk/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Desk
2
2
  # The version of the gem
3
- VERSION = '0.3.1'.freeze unless defined?(::Desk::VERSION)
3
+ VERSION = '0.3.2'.freeze unless defined?(::Desk::VERSION)
4
4
  end
@@ -36,9 +36,11 @@ describe Desk::API do
36
36
  :oauth_token_secret => 'OS',
37
37
  :adapter => :typhoeus,
38
38
  :format => :xml,
39
+ :max_requests => 50,
39
40
  :proxy => 'http://erik:sekret@proxy.example.com:8080',
40
41
  :subdomain => 'zencoder',
41
42
  :support_email => 'help@zencoder.com',
43
+ :use_max_requests => true,
42
44
  :user_agent => 'Custom User Agent',
43
45
  :version => "amazing"
44
46
  }
@@ -0,0 +1,54 @@
1
+ require 'helper'
2
+
3
+ describe Desk::Client do
4
+ Desk::Configuration::VALID_FORMATS.each do |format|
5
+ context ".new(:format => '#{format}')" do
6
+ before do
7
+ @client = Desk::Client.new(:subdomain => "example", :format => format, :consumer_key => 'CK', :consumer_secret => 'CS', :oauth_token => 'OT', :oauth_token_secret => 'OS')
8
+ end
9
+
10
+ describe ".groups" do
11
+ context "lookup" do
12
+ before do
13
+ stub_get("groups.#{format}").
14
+ to_return(:body => fixture("groups.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
15
+ end
16
+
17
+ it "should get the correct resource" do
18
+ @client.groups
19
+ a_get("groups.#{format}").
20
+ should have_been_made
21
+ end
22
+
23
+ it "should return up to 100 groups worth of extended information" do
24
+ groups = @client.groups
25
+ groups.results.should be_a Array
26
+ groups.results.last.group.id.should == 2
27
+ groups.results.last.group.name.should == "Administrators"
28
+ end
29
+ end
30
+ end
31
+
32
+ describe ".group" do
33
+ context "lookup" do
34
+ before do
35
+ stub_get("groups/1.#{format}").
36
+ to_return(:body => fixture("group.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
37
+ end
38
+
39
+ it "should get the correct resource" do
40
+ @client.group(1)
41
+ a_get("groups/1.#{format}").
42
+ should have_been_made
43
+ end
44
+
45
+ it "should return up to 100 cases worth of extended information" do
46
+ group = @client.group(1)
47
+ group.id.should == 1
48
+ group.name.should == "Sales"
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
data/spec/desk_spec.rb CHANGED
@@ -98,6 +98,32 @@ describe Desk do
98
98
  Desk.format.should == 'xml'
99
99
  end
100
100
  end
101
+
102
+ describe ".max_requests" do
103
+ it "should return the default max requests" do
104
+ Desk.max_requests.should == Desk::Configuration::DEFAULT_MAX_REQUESTS
105
+ end
106
+ end
107
+
108
+ describe ".max_requests=" do
109
+ it "should set the max_requests" do
110
+ Desk.max_requests = 50
111
+ Desk.max_requests.should == 50
112
+ end
113
+ end
114
+
115
+ describe ".use_max_requests" do
116
+ it "should return the default max requests flag" do
117
+ Desk.use_max_requests.should == Desk::Configuration::DEFAULT_USE_MAX_REQUESTS
118
+ end
119
+ end
120
+
121
+ describe ".use_max_requests=" do
122
+ it "should set the use_max_requests flag" do
123
+ Desk.max_requests = true
124
+ Desk.max_requests.should == true
125
+ end
126
+ end
101
127
 
102
128
  describe ".user_agent" do
103
129
  it "should return the default user agent" do
@@ -124,4 +150,70 @@ describe Desk do
124
150
  end
125
151
  end
126
152
  end
153
+
154
+ describe ".counter" do
155
+ before do
156
+ Desk.counter = 0
157
+ stub_get("cases.json").
158
+ to_return(:body => fixture("cases.json"), :headers => {:content_type => "application/json; charset=utf-8"})
159
+ end
160
+
161
+ it "should be 0 in the beginning" do
162
+ Desk.counter.should == 0
163
+ end
164
+
165
+ it "should count the requests" do
166
+ 5.times {
167
+ Desk.cases
168
+ }
169
+ Desk.counter.should == 5
170
+ end
171
+
172
+ context "max requests enabled" do
173
+ before do
174
+ Desk.use_max_requests = true
175
+ end
176
+
177
+ it "should only allow 60 requests" do
178
+ expect {
179
+ 70.times {
180
+ Desk.cases
181
+ }
182
+ }.to raise_error
183
+ end
184
+
185
+ it "should only allow defined requests" do
186
+ Desk.max_requests = 50
187
+ expect {
188
+ 55.times {
189
+ Desk.cases
190
+ }
191
+ }.to raise_error
192
+ end
193
+
194
+ def make_request
195
+ Desk.cases
196
+ rescue Desk::TooManyRequests
197
+ sleep(5)
198
+ make_request
199
+ end
200
+
201
+ xit "should allow more requests after minute has passed" do
202
+ 70.times {
203
+ make_request
204
+ }
205
+ Desk.counter.should == 10
206
+ end
207
+ end
208
+ end
209
+
210
+ describe ".minute" do
211
+ before do
212
+ Desk.minute = Time.now.min
213
+ end
214
+
215
+ it "should be the current minute" do
216
+ Desk.minute.should == Time.now.min
217
+ end
218
+ end
127
219
  end
@@ -0,0 +1,8 @@
1
+ {
2
+ "group": {
3
+ "id": 1,
4
+ "name": "Sales",
5
+ "created_at": "2013-01-21T09:47:23Z",
6
+ "updated_at": "2013-01-21T09:47:23Z"
7
+ }
8
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "results": [
3
+ {
4
+ "group": {
5
+ "id": 1,
6
+ "name": "Sales",
7
+ "created_at": "2013-01-21T09:47:23Z",
8
+ "updated_at": "2013-01-21T09:47:23Z"
9
+ }
10
+ },
11
+ {
12
+ "group": {
13
+ "id": 2,
14
+ "name": "Administrators",
15
+ "created_at": "2013-01-21T09:47:23Z",
16
+ "updated_at": "2013-01-21T09:47:23Z"
17
+ }
18
+ }
19
+ ],
20
+ "page": 1,
21
+ "count": 20,
22
+ "total": 2
23
+ }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: desk
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 1
10
- version: 0.3.1
9
+ - 2
10
+ version: 0.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Chris Warren
@@ -15,7 +15,8 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-07-22 00:00:00 Z
18
+ date: 2013-02-25 00:00:00 -08:00
19
+ default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: nokogiri
@@ -138,25 +139,10 @@ dependencies:
138
139
  version: "0.6"
139
140
  type: :development
140
141
  version_requirements: *id008
141
- - !ruby/object:Gem::Dependency
142
- name: ZenTest
143
- prerelease: false
144
- requirement: &id009 !ruby/object:Gem::Requirement
145
- none: false
146
- requirements:
147
- - - ~>
148
- - !ruby/object:Gem::Version
149
- hash: 17
150
- segments:
151
- - 4
152
- - 5
153
- version: "4.5"
154
- type: :development
155
- version_requirements: *id009
156
142
  - !ruby/object:Gem::Dependency
157
143
  name: json
158
144
  prerelease: false
159
- requirement: &id010 !ruby/object:Gem::Requirement
145
+ requirement: &id009 !ruby/object:Gem::Requirement
160
146
  none: false
161
147
  requirements:
162
148
  - - ~>
@@ -167,11 +153,11 @@ dependencies:
167
153
  - 7
168
154
  version: "1.7"
169
155
  type: :runtime
170
- version_requirements: *id010
156
+ version_requirements: *id009
171
157
  - !ruby/object:Gem::Dependency
172
158
  name: hashie
173
159
  prerelease: false
174
- requirement: &id011 !ruby/object:Gem::Requirement
160
+ requirement: &id010 !ruby/object:Gem::Requirement
175
161
  none: false
176
162
  requirements:
177
163
  - - ~>
@@ -183,11 +169,11 @@ dependencies:
183
169
  - 0
184
170
  version: 1.2.0
185
171
  type: :runtime
186
- version_requirements: *id011
172
+ version_requirements: *id010
187
173
  - !ruby/object:Gem::Dependency
188
174
  name: faraday
189
175
  prerelease: false
190
- requirement: &id012 !ruby/object:Gem::Requirement
176
+ requirement: &id011 !ruby/object:Gem::Requirement
191
177
  none: false
192
178
  requirements:
193
179
  - - ~>
@@ -199,11 +185,11 @@ dependencies:
199
185
  - 0
200
186
  version: 0.8.0
201
187
  type: :runtime
202
- version_requirements: *id012
188
+ version_requirements: *id011
203
189
  - !ruby/object:Gem::Dependency
204
190
  name: faraday_middleware
205
191
  prerelease: false
206
- requirement: &id013 !ruby/object:Gem::Requirement
192
+ requirement: &id012 !ruby/object:Gem::Requirement
207
193
  none: false
208
194
  requirements:
209
195
  - - ~>
@@ -215,42 +201,41 @@ dependencies:
215
201
  - 0
216
202
  version: 0.8.0
217
203
  type: :runtime
218
- version_requirements: *id013
204
+ version_requirements: *id012
219
205
  - !ruby/object:Gem::Dependency
220
206
  name: multi_json
221
207
  prerelease: false
222
- requirement: &id014 !ruby/object:Gem::Requirement
208
+ requirement: &id013 !ruby/object:Gem::Requirement
223
209
  none: false
224
210
  requirements:
225
211
  - - ~>
226
212
  - !ruby/object:Gem::Version
227
- hash: 9
213
+ hash: 3
228
214
  segments:
229
215
  - 1
230
- - 3
231
- version: "1.3"
216
+ - 6
217
+ version: "1.6"
232
218
  type: :runtime
233
- version_requirements: *id014
219
+ version_requirements: *id013
234
220
  - !ruby/object:Gem::Dependency
235
221
  name: multi_xml
236
222
  prerelease: false
237
- requirement: &id015 !ruby/object:Gem::Requirement
223
+ requirement: &id014 !ruby/object:Gem::Requirement
238
224
  none: false
239
225
  requirements:
240
226
  - - ~>
241
227
  - !ruby/object:Gem::Version
242
- hash: 15
228
+ hash: 1
243
229
  segments:
244
230
  - 0
245
- - 4
246
- - 0
247
- version: 0.4.0
231
+ - 5
232
+ version: "0.5"
248
233
  type: :runtime
249
- version_requirements: *id015
234
+ version_requirements: *id014
250
235
  - !ruby/object:Gem::Dependency
251
236
  name: rash
252
237
  prerelease: false
253
- requirement: &id016 !ruby/object:Gem::Requirement
238
+ requirement: &id015 !ruby/object:Gem::Requirement
254
239
  none: false
255
240
  requirements:
256
241
  - - ~>
@@ -262,11 +247,11 @@ dependencies:
262
247
  - 1
263
248
  version: 0.3.1
264
249
  type: :runtime
265
- version_requirements: *id016
250
+ version_requirements: *id015
266
251
  - !ruby/object:Gem::Dependency
267
252
  name: simple_oauth
268
253
  prerelease: false
269
- requirement: &id017 !ruby/object:Gem::Requirement
254
+ requirement: &id016 !ruby/object:Gem::Requirement
270
255
  none: false
271
256
  requirements:
272
257
  - - ~>
@@ -278,11 +263,11 @@ dependencies:
278
263
  - 4
279
264
  version: 0.1.4
280
265
  type: :runtime
281
- version_requirements: *id017
266
+ version_requirements: *id016
282
267
  - !ruby/object:Gem::Dependency
283
268
  name: pony
284
269
  prerelease: false
285
- requirement: &id018 !ruby/object:Gem::Requirement
270
+ requirement: &id017 !ruby/object:Gem::Requirement
286
271
  none: false
287
272
  requirements:
288
273
  - - ~>
@@ -293,7 +278,7 @@ dependencies:
293
278
  - 1
294
279
  version: "1.1"
295
280
  type: :runtime
296
- version_requirements: *id018
281
+ version_requirements: *id017
297
282
  description: A Ruby wrapper for the Desk.com REST API
298
283
  email:
299
284
  - chris@zencoder.com
@@ -321,6 +306,7 @@ files:
321
306
  - lib/desk/client/article.rb
322
307
  - lib/desk/client/case.rb
323
308
  - lib/desk/client/customer.rb
309
+ - lib/desk/client/group.rb
324
310
  - lib/desk/client/interaction.rb
325
311
  - lib/desk/client/macro.rb
326
312
  - lib/desk/client/topic.rb
@@ -338,6 +324,7 @@ files:
338
324
  - spec/desk/client/article_spec.rb
339
325
  - spec/desk/client/case_spec.rb
340
326
  - spec/desk/client/customer_spec.rb
327
+ - spec/desk/client/group_spec.rb
341
328
  - spec/desk/client/interaction_spec.rb
342
329
  - spec/desk/client/macro_spec.rb
343
330
  - spec/desk/client/topic_spec.rb
@@ -359,6 +346,8 @@ files:
359
346
  - spec/fixtures/customer_update.json
360
347
  - spec/fixtures/customer_update_email.json
361
348
  - spec/fixtures/customers.json
349
+ - spec/fixtures/group.json
350
+ - spec/fixtures/groups.json
362
351
  - spec/fixtures/interaction_create.json
363
352
  - spec/fixtures/interactions.json
364
353
  - spec/fixtures/macro.json
@@ -377,6 +366,7 @@ files:
377
366
  - spec/fixtures/user.json
378
367
  - spec/fixtures/users.json
379
368
  - spec/helper.rb
369
+ has_rdoc: true
380
370
  homepage: https://github.com/zencoder/desk
381
371
  licenses: []
382
372
 
@@ -408,7 +398,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
408
398
  requirements: []
409
399
 
410
400
  rubyforge_project: desk
411
- rubygems_version: 1.8.24
401
+ rubygems_version: 1.4.2
412
402
  signing_key:
413
403
  specification_version: 3
414
404
  summary: Ruby wrapper for the Desk.com API
@@ -417,6 +407,7 @@ test_files:
417
407
  - spec/desk/client/article_spec.rb
418
408
  - spec/desk/client/case_spec.rb
419
409
  - spec/desk/client/customer_spec.rb
410
+ - spec/desk/client/group_spec.rb
420
411
  - spec/desk/client/interaction_spec.rb
421
412
  - spec/desk/client/macro_spec.rb
422
413
  - spec/desk/client/topic_spec.rb
@@ -438,6 +429,8 @@ test_files:
438
429
  - spec/fixtures/customer_update.json
439
430
  - spec/fixtures/customer_update_email.json
440
431
  - spec/fixtures/customers.json
432
+ - spec/fixtures/group.json
433
+ - spec/fixtures/groups.json
441
434
  - spec/fixtures/interaction_create.json
442
435
  - spec/fixtures/interactions.json
443
436
  - spec/fixtures/macro.json
@@ -456,4 +449,3 @@ test_files:
456
449
  - spec/fixtures/user.json
457
450
  - spec/fixtures/users.json
458
451
  - spec/helper.rb
459
- has_rdoc: