zendesk_api 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. data/.gitignore +7 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +5 -0
  4. data/.yardopts +1 -0
  5. data/Gemfile +6 -0
  6. data/Gemfile.lock +59 -0
  7. data/LICENSE +19 -0
  8. data/Rakefile +49 -0
  9. data/Readme.md +178 -0
  10. data/lib/zendesk_api.rb +10 -0
  11. data/lib/zendesk_api/actions.rb +176 -0
  12. data/lib/zendesk_api/association.rb +267 -0
  13. data/lib/zendesk_api/client.rb +150 -0
  14. data/lib/zendesk_api/collection.rb +233 -0
  15. data/lib/zendesk_api/configuration.rb +52 -0
  16. data/lib/zendesk_api/core_ext/inflection.rb +13 -0
  17. data/lib/zendesk_api/core_ext/modulize.rb +10 -0
  18. data/lib/zendesk_api/core_ext/snakecase.rb +12 -0
  19. data/lib/zendesk_api/lru_cache.rb +38 -0
  20. data/lib/zendesk_api/middleware/request/etag_cache.rb +38 -0
  21. data/lib/zendesk_api/middleware/request/retry.rb +39 -0
  22. data/lib/zendesk_api/middleware/request/upload.rb +32 -0
  23. data/lib/zendesk_api/middleware/response/callback.rb +19 -0
  24. data/lib/zendesk_api/middleware/response/deflate.rb +18 -0
  25. data/lib/zendesk_api/middleware/response/gzip.rb +18 -0
  26. data/lib/zendesk_api/middleware/response/parse_iso_dates.rb +29 -0
  27. data/lib/zendesk_api/rescue.rb +44 -0
  28. data/lib/zendesk_api/resource.rb +133 -0
  29. data/lib/zendesk_api/resources/forum.rb +51 -0
  30. data/lib/zendesk_api/resources/misc.rb +66 -0
  31. data/lib/zendesk_api/resources/playlist.rb +64 -0
  32. data/lib/zendesk_api/resources/ticket.rb +76 -0
  33. data/lib/zendesk_api/resources/user.rb +44 -0
  34. data/lib/zendesk_api/track_changes.rb +72 -0
  35. data/lib/zendesk_api/trackie.rb +8 -0
  36. data/lib/zendesk_api/verbs.rb +43 -0
  37. data/lib/zendesk_api/version.rb +3 -0
  38. data/live/Readme.md +4 -0
  39. data/live/activity_spec.rb +5 -0
  40. data/live/audit_spec.rb +5 -0
  41. data/live/bookmark_spec.rb +11 -0
  42. data/live/category_spec.rb +12 -0
  43. data/live/collection_spec.rb +68 -0
  44. data/live/crm_spec.rb +11 -0
  45. data/live/custom_role_spec.rb +5 -0
  46. data/live/forum_spec.rb +14 -0
  47. data/live/forum_subscription_spec.rb +12 -0
  48. data/live/group_membership_spec.rb +18 -0
  49. data/live/group_spec.rb +14 -0
  50. data/live/identity_spec.rb +14 -0
  51. data/live/locale_spec.rb +11 -0
  52. data/live/macro_spec.rb +5 -0
  53. data/live/mobile_device_spec.rb +11 -0
  54. data/live/organization_spec.rb +12 -0
  55. data/live/satisfaction_rating_spec.rb +6 -0
  56. data/live/setting_spec.rb +5 -0
  57. data/live/suspended_ticket_spec.rb +8 -0
  58. data/live/ticket_field_spec.rb +12 -0
  59. data/live/ticket_metrics_spec.rb +6 -0
  60. data/live/ticket_spec.rb +88 -0
  61. data/live/topic_comment_spec.rb +13 -0
  62. data/live/topic_spec.rb +18 -0
  63. data/live/topic_subscription_spec.rb +12 -0
  64. data/live/topic_vote_spec.rb +13 -0
  65. data/live/upload_spec.rb +9 -0
  66. data/live/user_spec.rb +13 -0
  67. data/live/view_spec.rb +6 -0
  68. data/spec/association_spec.rb +210 -0
  69. data/spec/client_spec.rb +149 -0
  70. data/spec/collection_spec.rb +302 -0
  71. data/spec/configuration_spec.rb +24 -0
  72. data/spec/create_resource_spec.rb +39 -0
  73. data/spec/data_resource_spec.rb +229 -0
  74. data/spec/fixtures/Argentina.gif +0 -0
  75. data/spec/fixtures/Argentina2.gif +0 -0
  76. data/spec/fixtures/credentials.yml.example +3 -0
  77. data/spec/fixtures/test_resources.rb +8 -0
  78. data/spec/fixtures/zendesk.rb +88 -0
  79. data/spec/lru_cache_spec.rb +26 -0
  80. data/spec/macros/resource_macros.rb +157 -0
  81. data/spec/middleware/request/etag_cache_spec.rb +17 -0
  82. data/spec/middleware/request/retry_spec.rb +47 -0
  83. data/spec/middleware/request/test.jpg +0 -0
  84. data/spec/middleware/request/upload_spec.rb +74 -0
  85. data/spec/middleware/response/callback_spec.rb +17 -0
  86. data/spec/middleware/response/deflate_spec.rb +15 -0
  87. data/spec/middleware/response/gzip_spec.rb +19 -0
  88. data/spec/middleware/response/parse_iso_dates_spec.rb +44 -0
  89. data/spec/playlist_spec.rb +95 -0
  90. data/spec/read_resource_spec.rb +37 -0
  91. data/spec/rescue_spec.rb +94 -0
  92. data/spec/resource_spec.rb +332 -0
  93. data/spec/spec_helper.rb +120 -0
  94. data/spec/string_spec.rb +7 -0
  95. data/spec/trackie_spec.rb +39 -0
  96. data/zendesk_api.gemspec +38 -0
  97. metadata +364 -0
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe ZendeskAPI::Upload, :not_findable do
4
+ def valid_attributes
5
+ { :file => "spec/fixtures/Argentina.gif" }
6
+ end
7
+
8
+ it_should_be_creatable :id => :token
9
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe ZendeskAPI::User, :delete_after do
4
+ def valid_attributes
5
+ { :name => "Test U.", :email => "test@example.org" }
6
+ end
7
+
8
+ it_should_be_creatable
9
+ it_should_be_updatable :name
10
+ it_should_be_deletable :find => [:active?, false]
11
+ it_should_be_readable :users
12
+ it_should_be_readable organization, :users
13
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+
3
+ describe ZendeskAPI::View do
4
+ it_should_be_readable :views
5
+ it_should_be_readable :views, :active
6
+ end
@@ -0,0 +1,210 @@
1
+ require 'spec_helper'
2
+
3
+ describe ZendeskAPI::Association do
4
+ let(:instance) { ZendeskAPI::TestResource.new(client, :id => 1) }
5
+ let(:child) { ZendeskAPI::TestResource::TestChild.new(client, :id => 1, :test_resource_id => 2) }
6
+
7
+ describe "setting/getting" do
8
+ context "has" do
9
+ before do
10
+ ZendeskAPI::TestResource.associations.clear
11
+ ZendeskAPI::TestResource.has :child, :class => :test_child
12
+ end
13
+
14
+ it "should cache an set object" do
15
+ instance.child = child
16
+ instance.child.should == child
17
+ end
18
+
19
+ it "should set id on set if it was there" do
20
+ instance.child_id = nil
21
+ instance.child = child
22
+ instance.child_id.should == child.id
23
+ end
24
+
25
+ it "should not set id on set if it was not there" do
26
+ instance.child = child
27
+ instance.child_id.should == nil
28
+ end
29
+
30
+ it "should build a object set via hash" do
31
+ instance.child = {:id => 2}
32
+ instance.child.id.should == 2
33
+ end
34
+
35
+ it "should build a object set via id" do
36
+ instance.child = 2
37
+ instance.child.id.should == 2
38
+ end
39
+
40
+ it "should fetch a unknown object" do
41
+ stub_json_request(:get, %r{test_resources/1/child}, json(:test_child => {:id => 2}))
42
+ instance.child.id.should == 2
43
+ end
44
+
45
+ it "should fetch an object known by id" do
46
+ stub_json_request(:get, %r{test_resources/1/child/5}, json(:test_child => {:id => 5}))
47
+ instance.child_id = 5
48
+ instance.child.id.should == 5
49
+ end
50
+
51
+ it "should handle client errors" do
52
+ stub_request(:get, %r{test_resources/1/child/5}).to_return(:status => 500)
53
+ instance.child_id = 5
54
+ expect { silence_logger { instance.child } }.to_not raise_error
55
+ end
56
+
57
+ it "should handle resource not found errors" do
58
+ stub_request(:get, %r{test_resources/1/child/5}).to_return(:status => 404)
59
+ instance.child_id = 5
60
+ silence_logger{ instance.child.should be_nil }
61
+ end
62
+
63
+ it "is not used when not used" do
64
+ instance.child_used?.should == false
65
+ end
66
+
67
+ it "is used when used" do
68
+ instance.child = child
69
+ instance.child_used?.should == true
70
+ end
71
+ end
72
+
73
+ context "has_many" do
74
+ it "should cache a set object" do
75
+ instance.children = [child]
76
+ instance.children.map(&:id).should == [1]
77
+ end
78
+
79
+ it "should set ids" do
80
+ instance.children_ids = []
81
+ instance.children = [child]
82
+ instance.children_ids.should == [child.id]
83
+ end
84
+
85
+ it "should build and cache objects set via hash" do
86
+ instance.children = [{:id => 2}]
87
+ instance.children.map(&:id).should == [2]
88
+ end
89
+
90
+ it "should build a object set via id" do
91
+ instance.children = [2]
92
+ instance.children.map(&:id).should == [2]
93
+ end
94
+
95
+ it "should fetch unknown objects" do
96
+ stub_json_request(:get, %r{test_resources/1/children}, json(:test_children => [{:id => 2}, {:id => 3}]))
97
+ instance.children.map(&:id).should == [2,3]
98
+ end
99
+
100
+ it "is not used when not used" do
101
+ instance.children_used?.should == false
102
+ end
103
+
104
+ it "is used when used" do
105
+ instance.children = [child]
106
+ instance.children_used?.should == true
107
+ end
108
+ end
109
+ end
110
+
111
+ context "class only" do
112
+ subject { described_class.new(:class => ZendeskAPI::TestResource) }
113
+
114
+ it "should generate resource path" do
115
+ subject.generate_path.should == "test_resources"
116
+ end
117
+
118
+ context "with an instance" do
119
+ it "should generate a specific resource path" do
120
+ subject.generate_path(instance).should == "test_resources/1"
121
+ end
122
+
123
+ context "with_id => false" do
124
+ it "should generate general resource path" do
125
+ subject.generate_path(instance, :with_id => false).should == "test_resources"
126
+ end
127
+ end
128
+
129
+ context "with an instance that is a new record" do
130
+ it "should generate general resource path" do
131
+ subject.generate_path(ZendeskAPI::TestResource.new(client)).should == "test_resources"
132
+ end
133
+ end
134
+ end
135
+
136
+ context "with a specified path" do
137
+ before(:each) { subject.options[:path] = "blergh" }
138
+
139
+ it "should generate general resource path" do
140
+ subject.generate_path.should == "blergh"
141
+ end
142
+ end
143
+
144
+ context "with a passed in id" do
145
+ it "should generate specific resource path" do
146
+ opts = { :id => 1 }
147
+ subject.generate_path(opts).should == "test_resources/1"
148
+ opts.should be_empty
149
+ end
150
+ end
151
+ end
152
+
153
+ context "class with a specified parent" do
154
+ subject { described_class.new(:class => ZendeskAPI::TestResource::TestChild, :parent => instance) }
155
+
156
+ it "should generate nested resource path" do
157
+ subject.generate_path.should == "test_resources/1/children"
158
+ end
159
+
160
+ context "with an instance" do
161
+ it "should generate a specific nested resource path" do
162
+ subject.generate_path(child).should == "test_resources/1/children/1"
163
+ end
164
+
165
+ context "with_id => false" do
166
+ it "should generate nested resource path" do
167
+ subject.generate_path(child, :with_id => false).should == "test_resources/1/children"
168
+ end
169
+ end
170
+ end
171
+
172
+ context "with a specified path" do
173
+ before(:each) { subject.options[:path] = "blergh" }
174
+
175
+ it "should generate nested resource path" do
176
+ subject.generate_path.should == "test_resources/1/blergh"
177
+ end
178
+ end
179
+ end
180
+
181
+ context "class with a parent id" do
182
+ subject { described_class.new(:class => ZendeskAPI::TestResource::TestChild) }
183
+
184
+ it "should raise an error if not passed an instance or id" do
185
+ expect { subject.generate_path }.to raise_error(ArgumentError)
186
+ end
187
+
188
+ it "should generate specific nested resource path" do
189
+ subject.generate_path(child).should == "test_resources/2/children/1"
190
+ end
191
+
192
+ context "with parent id passed in" do
193
+ it "should generate nested resource path" do
194
+ opts = { :test_resource_id => 3 }
195
+ subject.generate_path(opts).should == "test_resources/3/children"
196
+ opts.should be_empty
197
+ end
198
+ end
199
+ end
200
+
201
+ context "with a singular resource" do
202
+ subject { described_class.new(:class => ZendeskAPI::SingularTestResource) }
203
+
204
+ context "with an instance" do
205
+ it "should not generate a specific resource path" do
206
+ subject.generate_path(ZendeskAPI::SingularTestResource.new(client, :id => 1)).should == "singular_test_resources"
207
+ end
208
+ end
209
+ end
210
+ end
@@ -0,0 +1,149 @@
1
+ require 'spec_helper'
2
+
3
+ class SimpleClient < ZendeskAPI::Client
4
+ def build_connection
5
+ "FOO"
6
+ end
7
+ end
8
+
9
+ describe ZendeskAPI::Client do
10
+ subject { client }
11
+
12
+ context "#initialize" do
13
+ it "should require a block" do
14
+ expect { ZendeskAPI::Client.new }.to raise_error(ArgumentError)
15
+ end
16
+
17
+ it "should raise an exception when url isn't ssl" do
18
+ expect do
19
+ ZendeskAPI::Client.new do |config|
20
+ config.url = "http://www.google.com"
21
+ end
22
+ end.to raise_error(ArgumentError)
23
+ end
24
+
25
+ it "should not raise an exception when url isn't ssl and allow_http is set to true" do
26
+ expect do
27
+ ZendeskAPI::Client.new do |config|
28
+ config.allow_http = true
29
+ config.url = "http://www.google.com/"
30
+ end
31
+ end.to_not raise_error
32
+ end
33
+
34
+ it "should handle valid url" do
35
+ expect do
36
+ ZendeskAPI::Client.new do |config|
37
+ config.url = "https://example.zendesk.com/"
38
+ end.to_not raise_error
39
+ end
40
+ end
41
+
42
+ context "#logger" do
43
+ before(:each) do
44
+ @client = ZendeskAPI::Client.new do |config|
45
+ config.url = "https://example.zendesk.com/"
46
+ config.logger = subject
47
+ end
48
+
49
+ stub_request(:get, %r{/bs$}).to_return(:status => 200)
50
+ end
51
+
52
+ context "with true value" do
53
+ subject { true }
54
+
55
+ it "should log in faraday" do
56
+ @client.connection.builder.handlers.should include(Faraday::Response::Logger)
57
+ end
58
+
59
+ context "with a request" do
60
+ it "should log" do
61
+ client.config.logger.should_receive(:info).at_least(:once)
62
+ @client.connection.get('/bs')
63
+ end
64
+ end
65
+ end
66
+
67
+ context "with false value" do
68
+ subject { false }
69
+
70
+ it "should not log" do
71
+ @client.connection.builder.handlers.should_not include(Faraday::Response::Logger)
72
+ end
73
+ end
74
+
75
+ context "with a nil value" do
76
+ subject { nil }
77
+
78
+ it "should log" do
79
+ @client.connection.builder.handlers.should include(Faraday::Response::Logger)
80
+ end
81
+ end
82
+
83
+ context "with a logger" do
84
+ let(:out){ StringIO.new }
85
+ subject { Logger.new(out) }
86
+
87
+ it "should log" do
88
+ @client.connection.builder.handlers.should include(Faraday::Response::Logger)
89
+ end
90
+
91
+ context "with a request" do
92
+ it "should log to the subject" do
93
+ out.should_receive(:write).at_least(:once)
94
+ @client.connection.get('/bs')
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ context "#current_user" do
102
+ before(:each) do
103
+ stub_json_request(:get, %r{users/me}, json("user" => {}))
104
+ end
105
+
106
+ it "should be a user instance" do
107
+ client.current_user.should be_instance_of(ZendeskAPI::User)
108
+ end
109
+ end
110
+
111
+ context "#connection" do
112
+ it "should initially be false" do
113
+ subject.instance_variable_get(:@connection).should be_false
114
+ end
115
+
116
+ it "connection should be initialized on first call to #connection" do
117
+ subject.connection.should be_instance_of(Faraday::Connection)
118
+ end
119
+ end
120
+
121
+ context "resources" do
122
+ it "should return an instance of ZendeskAPI::Collection if there is no method" do
123
+ subject.tickets.should be_instance_of(ZendeskAPI::Collection)
124
+ subject.instance_variable_defined?(:@tickets).should be_true
125
+ end
126
+ end
127
+
128
+ context "#play" do
129
+ # TODO may be able to be replaced by VCR
130
+ before(:each) do
131
+ stub_request(:get, %r{play}).to_return do
132
+ { :status => 302 }
133
+ end
134
+ end
135
+
136
+ it "should return an instance of ZendeskAPI::Playlist" do
137
+ subject.play(1).should be_instance_of(ZendeskAPI::Playlist)
138
+ end
139
+ end
140
+
141
+ it "can be subclassed" do
142
+ client = SimpleClient.new do |config|
143
+ config.allow_http = true
144
+ end
145
+ client.config.allow_http.should == true
146
+ client.connection.should == "FOO"
147
+ client.connection.object_id.should == client.connection.object_id # it's cached
148
+ end
149
+ end
@@ -0,0 +1,302 @@
1
+ require 'spec_helper.rb'
2
+
3
+ describe ZendeskAPI::Collection do
4
+ subject do
5
+ ZendeskAPI::Collection.new(client, ZendeskAPI::TestResource)
6
+ end
7
+
8
+ context "initialization" do
9
+ it "should set the resource class" do
10
+ subject.instance_variable_get(:@resource_class).should == ZendeskAPI::TestResource
11
+ end
12
+
13
+ it "should initially be empty" do
14
+ subject.instance_variable_defined?(:@resources).should be_false
15
+ end
16
+ end
17
+
18
+ context "with array option passed in" do
19
+ subject { ZendeskAPI::Collection.new(client, ZendeskAPI::TestResource, :ids => [1, 2, 3, 4]) }
20
+
21
+ it "should join array with commas" do
22
+ subject.instance_variable_get(:@options)[:ids].should == "1,2,3,4"
23
+ end
24
+ end
25
+
26
+ context "deferral" do
27
+ before(:each) do
28
+ stub_json_request(:any, %r{test_resources}, json(:test_resource => {}))
29
+ end
30
+
31
+ it "should defer #create to the resource class" do
32
+ subject.create
33
+ end
34
+
35
+ it "should defer #find to the resource class" do
36
+ subject.find(:id => 1)
37
+ end
38
+
39
+ it "should defer #destroy to the resource class" do
40
+ subject.destroy(:id => 1)
41
+ end
42
+
43
+ it "should defer #update to the resource class" do
44
+ subject.update(:id => 1)
45
+ end
46
+
47
+ context "with a class with a parent" do
48
+ let(:association) do
49
+ ZendeskAPI::Association.new(:class => ZendeskAPI::TestResource::TestChild,
50
+ :parent => ZendeskAPI::TestResource.new(client, :id => 1))
51
+ end
52
+
53
+ subject do
54
+ ZendeskAPI::Collection.new(client, ZendeskAPI::TestResource::TestChild,
55
+ :association => association)
56
+ end
57
+
58
+ before(:each) do
59
+ stub_json_request(:any, %r{/test_resources/\d+/child}, json("test_child" => {}))
60
+ end
61
+
62
+ it "should defer #create to the resource class with the parent id" do
63
+ subject.create
64
+ end
65
+
66
+ it "should defer #destroy the resource class with the parent id" do
67
+ subject.destroy(:id => 1)
68
+ end
69
+
70
+ it "should defer #find to the resource class with the parent id" do
71
+ subject.find(:id => 1)
72
+ end
73
+
74
+ it "should defer #update to the resource class with the parent id" do
75
+ subject.update(:id => 1)
76
+ end
77
+
78
+ context "on object push" do
79
+ before(:each) do
80
+ stub_json_request(:get, %r{test_resources/\d+/children}, json(:test_children => []))
81
+ subject << { :id => 1 }
82
+ end
83
+
84
+ it "should pass association" do
85
+ subject.last.association.should == association
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ context "pagination with no options and no data" do
92
+ it "should return an empty array on #next" do
93
+ subject.next.should be_empty
94
+ end
95
+
96
+ it "should return an empty array on #prev" do
97
+ subject.prev.should be_empty
98
+ end
99
+ end
100
+
101
+ context "pagination with options and no data" do
102
+ before(:each) { subject.per_page(5).page(2) }
103
+
104
+ it "should set per_page option" do
105
+ subject.per_page(10).should == subject
106
+ subject.instance_variable_get(:@options)["per_page"].should == 10
107
+ end
108
+
109
+ it "should set page option" do
110
+ subject.page(10).should == subject
111
+ subject.instance_variable_get(:@options)["page"].should == 10
112
+ end
113
+
114
+ it "should increate page option" do
115
+ subject.next.should == 3
116
+ end
117
+
118
+ it "should decreate page option" do
119
+ subject.prev.should == 1
120
+ end
121
+ end
122
+
123
+ context "fetch" do
124
+ it "does not fetch if associated is a new record" do
125
+ ZendeskAPI::Category.new(client).forums.fetch.should == []
126
+ ZendeskAPI::Category.new(client).forums.to_a.should == []
127
+ end
128
+
129
+ context "with client error" do
130
+ before(:each) do
131
+ stub_request(:get, %r{test_resources}).to_return(:status => 500)
132
+ end
133
+
134
+ it "should properly be handled" do
135
+ silence_logger { subject.fetch(true).should be_empty }
136
+ end
137
+ end
138
+
139
+ context "with unfetchable resource" do
140
+ subject { ZendeskAPI::Collection.new(client, ZendeskAPI::NilResource) }
141
+
142
+ it "should not call connection" do
143
+ client.connection.should_not_receive(:get)
144
+ subject.fetch(true).should be_empty
145
+ end
146
+ end
147
+ end
148
+
149
+ context "save" do
150
+ let(:options) { { :abc => 1 } }
151
+ before(:each) do
152
+ stub_json_request(:get, %r{test_resources}, json(:test_resources => []))
153
+ subject.clear_cache
154
+ end
155
+
156
+ context "with a hash" do
157
+ let(:object) { mock('ZendeskAPI::TestResource', :changes => [:xxx]) }
158
+
159
+ it "should call create with those options" do
160
+ ZendeskAPI::TestResource.should_receive(:new).
161
+ with(client, options.merge(:association => subject.association)).
162
+ and_return(object)
163
+
164
+ subject << options
165
+
166
+ object.should_receive(:save)
167
+ subject.save
168
+
169
+ subject.should include(object)
170
+ end
171
+ end
172
+
173
+ context "with a new object" do
174
+ let(:object) { ZendeskAPI::TestResource.new(client, options) }
175
+ before(:each) do
176
+ subject << object
177
+ end
178
+
179
+ it "should save object" do
180
+ object.should_receive(:save)
181
+ subject.save
182
+ end
183
+
184
+ it "should have object in collection" do
185
+ subject.should include(object)
186
+ end
187
+ end
188
+
189
+ context "with everything else" do
190
+ it "should pass to new, since this is how attachment handles it" do
191
+ attachment = mock(:changes => [:xxx])
192
+ ZendeskAPI::TestResource.should_receive(:new).with(client, "img.jpg").and_return attachment
193
+ subject << "img.jpg"
194
+
195
+ attachment.should_receive :save
196
+ subject.save
197
+ end
198
+ end
199
+ end
200
+
201
+ context "without real data" do
202
+ subject do
203
+ ZendeskAPI::Collection.new(client, ZendeskAPI::User)
204
+ end
205
+
206
+ before(:each) do
207
+ stub_json_request(:get, %r{users\?page=2}, json(
208
+ :users => [{:id => 2}],
209
+ :next_page => "/users?page=3&per_page=1",
210
+ :previous_page => "/users?page=1&per_page=1"
211
+ ))
212
+
213
+ subject.per_page(1).page(2)
214
+ subject.fetch(true)
215
+ end
216
+
217
+ context "pagination with no options" do
218
+ before(:each) do
219
+ stub_json_request(:get, %r{users\?page=(1|3)}, json(:users => [{:id => 3}]))
220
+
221
+ subject.per_page(nil).page(nil)
222
+ end
223
+
224
+ it "should find the next page by calling fetch" do
225
+ current = subject.to_a.dup
226
+ nxt = subject.next
227
+
228
+ nxt.size.should == 1
229
+ nxt.should_not == current
230
+ end
231
+
232
+ it "should find the prev page by calling fetch" do
233
+ current = subject.to_a.dup
234
+ prev = subject.prev
235
+
236
+ prev.size.should == 1
237
+ prev.should_not == current
238
+ end
239
+ end
240
+
241
+ context "pagination with options", :vcr do
242
+ before(:each) { subject.per_page(1).page(2) }
243
+
244
+ it "should increase page option and not call fetch" do
245
+ subject.next.should == 3
246
+ end
247
+
248
+ it "should decrease page option and not call fetch" do
249
+ subject.prev.should == 1
250
+ end
251
+
252
+ context "with page == 1" do
253
+ before do
254
+ subject.page(1)
255
+ subject.clear_cache
256
+ subject.should_not_receive(:fetch)
257
+ end
258
+
259
+ it "should do nothing on #prev" do
260
+ subject.prev.should == []
261
+ end
262
+ end
263
+ end
264
+ end
265
+
266
+ context "method missing" do
267
+ before(:each) { subject.stub(:fetch).and_return([1, 2, nil, 3]) }
268
+
269
+ it "should pass all methods not defined to resources" do
270
+ subject.compact.should == [1, 2, 3]
271
+ end
272
+
273
+ it "should take a block" do
274
+ subject.map {|i| i.to_i + 1}.should == [2, 3, 1, 4]
275
+ end
276
+
277
+ it "should create a new collection if it isn't an array method" do
278
+ subject.recent.should be_instance_of(ZendeskAPI::Collection)
279
+ end
280
+
281
+ it "should pass the correct query_path to the new collection" do
282
+ subject.recent.instance_variable_get(:@collection_path).last.should == :recent
283
+ end
284
+ end
285
+
286
+
287
+ context "with different path" do
288
+ subject do
289
+ ZendeskAPI::Collection.new(client, ZendeskAPI::TestResource, :collection_path => ["test_resources", "active"])
290
+ end
291
+
292
+ before(:each) do
293
+ stub_json_request(:post, %r{test_resources/active})
294
+ end
295
+
296
+ context "deferral" do
297
+ it "should defer #create to the resource class with proper path" do
298
+ subject.create
299
+ end
300
+ end
301
+ end
302
+ end