zendesk_api 0.1.2 → 0.1.3

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.
data/.travis.yml CHANGED
@@ -3,3 +3,6 @@ rvm:
3
3
  - 1.9.2
4
4
  - 1.9.3
5
5
  - jruby
6
+ branches:
7
+ except:
8
+ - zendesk_console
data/Gemfile CHANGED
@@ -2,5 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  gem "simplecov", :platforms => :ruby_19, :group => :development
4
4
  gem "jruby-openssl", :platforms => :jruby
5
+ gem "hashie", :git => "https://github.com/steved555/hashie.git"
5
6
 
6
7
  gemspec
data/Gemfile.lock CHANGED
@@ -1,3 +1,9 @@
1
+ GIT
2
+ remote: https://github.com/steved555/hashie.git
3
+ revision: d433d8b1574130f8f3752019ea4db3519c69e6df
4
+ specs:
5
+ hashie (2.0.0.beta)
6
+
1
7
  PATH
2
8
  remote: .
3
9
  specs:
@@ -13,35 +19,34 @@ PATH
13
19
  GEM
14
20
  remote: https://rubygems.org/
15
21
  specs:
16
- addressable (2.2.8)
22
+ addressable (2.3.2)
17
23
  crack (0.3.1)
18
24
  diff-lcs (1.1.3)
19
- faraday (0.8.2)
25
+ faraday (0.8.4)
20
26
  multipart-post (~> 1.1)
21
27
  faraday_middleware (0.8.8)
22
28
  faraday (>= 0.7.4, < 0.9)
23
- hashie (1.2.0)
24
29
  inflection (1.0.0)
25
- json (1.7.4)
30
+ json (1.7.5)
26
31
  mime-types (1.19)
27
32
  multi_json (1.3.6)
28
33
  multipart-post (1.1.5)
29
34
  rake (0.9.2.2)
30
- rspec (2.10.0)
31
- rspec-core (~> 2.10.0)
32
- rspec-expectations (~> 2.10.0)
33
- rspec-mocks (~> 2.10.0)
34
- rspec-core (2.10.1)
35
- rspec-expectations (2.10.0)
35
+ rspec (2.11.0)
36
+ rspec-core (~> 2.11.0)
37
+ rspec-expectations (~> 2.11.0)
38
+ rspec-mocks (~> 2.11.0)
39
+ rspec-core (2.11.1)
40
+ rspec-expectations (2.11.2)
36
41
  diff-lcs (~> 1.1.3)
37
- rspec-mocks (2.10.1)
42
+ rspec-mocks (2.11.2)
38
43
  simplecov (0.6.4)
39
44
  multi_json (~> 1.0)
40
45
  simplecov-html (~> 0.5.3)
41
46
  simplecov-html (0.5.3)
42
47
  vcr (2.2.4)
43
- webmock (1.8.8)
44
- addressable (~> 2.2.8)
48
+ webmock (1.8.9)
49
+ addressable (>= 2.2.7)
45
50
  crack (>= 0.1.7)
46
51
  yard (0.8.2.1)
47
52
 
@@ -49,9 +54,10 @@ PLATFORMS
49
54
  ruby
50
55
 
51
56
  DEPENDENCIES
57
+ hashie!
52
58
  jruby-openssl
53
59
  rake
54
- rspec (~> 2.10.0)
60
+ rspec
55
61
  simplecov
56
62
  vcr
57
63
  webmock
data/Readme.md CHANGED
@@ -39,7 +39,7 @@ client = ZendeskAPI::Client.new do |config|
39
39
 
40
40
  config.url = "<- your-zendesk-url ->" # e.g. https://mydesk.zendesk.com/api/v2
41
41
 
42
- config.username = "login.email@zendesk.com"
42
+ config.username = "login.email@zendesk.com" # use login.email@zendesk.com/token for token auth
43
43
  config.password = "your zendesk password or token"
44
44
 
45
45
  # Optional:
@@ -102,6 +102,22 @@ next_page = tickets.next
102
102
  previous_page = tickets.prev
103
103
  ```
104
104
 
105
+ Iteration over all resources and pages is handled by Collection#page_page
106
+
107
+ ```ruby
108
+ client.tickets.each_page do |resource|
109
+ # all resources will be yielded
110
+ end
111
+ ```
112
+
113
+ If given a block with two arguments, the page is also passed in.
114
+
115
+ ```ruby
116
+ client.tickets.each_page do |resource, page|
117
+ # all resources will be yielded along with the page
118
+ end
119
+ ```
120
+
105
121
  ### Callbacks
106
122
 
107
123
  Callbacks can be added to the ZendeskAPI::Client instance and will be called (with the response env) after all response middleware on a successful request.
@@ -177,7 +193,7 @@ ticket.save
177
193
  ## Supported Ruby Versions
178
194
 
179
195
  Tested with Ruby 1.8.7 and 1.9.3
180
- [![Build Status](https://secure.travis-ci.org/zendesk/zendesk_api_client_rb.png)](http://travis-ci.org/zendesk/zendesk_api_client_rb)
196
+ [![Build Status](https://secure.travis-ci.org/zendesk/zendesk_api_client_rb.png?branch=master)](http://travis-ci.org/zendesk/zendesk_api_client_rb)
181
197
 
182
198
  ## Copyright
183
199
 
data/lib/zendesk_api.rb CHANGED
@@ -1,5 +1,3 @@
1
- require "rubygems"
2
- require "bundler/setup"
3
1
 
4
2
  require 'zendesk_api/core_ext/modulize'
5
3
  require 'zendesk_api/core_ext/snakecase'
@@ -29,6 +29,7 @@ module ZendeskAPI
29
29
 
30
30
  @attributes.replace @attributes.deep_merge(response.body[self.class.singular_resource_name] || {})
31
31
  @attributes.clear_changes
32
+ clear_associations
32
33
  true
33
34
  end
34
35
 
@@ -36,17 +37,27 @@ module ZendeskAPI
36
37
  save(options) || raise("Save failed")
37
38
  end
38
39
 
40
+ def clear_associations
41
+ self.class.associations.each do |association_data|
42
+ name = association_data[:name]
43
+ instance_variable_set("@#{name}", nil) if instance_variable_defined?("@#{name}")
44
+ end
45
+ end
46
+
39
47
  def save_associations
40
48
  self.class.associations.each do |association_data|
41
49
  association_name = association_data[:name]
42
50
  next unless send("#{association_name}_used?") && association = send(association_name)
43
51
 
44
- if association.respond_to?(:save) && (association.is_a?(Collection) || !association.changes.empty?)
45
- association.save
52
+ inline_creation = association_data[:inline] == :create && new_record?
53
+ changed = association.is_a?(Collection) || !association.changes.empty?
54
+
55
+ if association.respond_to?(:save) && changed && !inline_creation && association.save
46
56
  self.send("#{association_name}=", association) # set id/ids columns
47
57
  end
48
58
 
49
- if association_data[:inline]
59
+
60
+ if association_data[:inline] == true || inline_creation
50
61
  attributes[association_name] = (association.is_a?(Collection) ? association.map(&:to_param) : association.to_param)
51
62
  end
52
63
  end
@@ -63,7 +74,7 @@ module ZendeskAPI
63
74
  def find(client, options = {})
64
75
  @client = client # so we can use client.logger in rescue
65
76
 
66
- raise "No :id given" unless options[:id] || options["id"]
77
+ raise ArgumentError, "No :id given" unless options[:id] || options["id"] || ancestors.include?(SingularResource)
67
78
  association = options.delete(:association) || Association.new(:class => self)
68
79
 
69
80
  response = client.connection.get(association.generate_path(options)) do |req|
@@ -83,6 +83,7 @@ module ZendeskAPI
83
83
  # Changes the per_page option. Returns self, so it can be chained. No execution.
84
84
  # @return [Collection] self
85
85
  def per_page(count)
86
+ clear_cache if count
86
87
  @options["per_page"] = count
87
88
  self
88
89
  end
@@ -90,6 +91,7 @@ module ZendeskAPI
90
91
  # Changes the page option. Returns self, so it can be chained. No execution.
91
92
  # @return [Collection] self
92
93
  def page(number)
94
+ clear_cache if number
93
95
  @options["page"] = number
94
96
  self
95
97
  end
@@ -144,7 +146,13 @@ module ZendeskAPI
144
146
  end
145
147
 
146
148
  response = @client.connection.send(@verb || "get", path) do |req|
147
- req.params.merge!(@options.delete_if {|k, v| v.nil?})
149
+ opts = @options.delete_if {|k, v| v.nil?}
150
+
151
+ if %w{put post}.include?(@verb.to_s)
152
+ req.body = opts
153
+ else
154
+ req.params.merge!(opts)
155
+ end
148
156
  end
149
157
 
150
158
  results = response.body[@resource_class.model_key] || response.body["results"]
@@ -153,6 +161,12 @@ module ZendeskAPI
153
161
  @count = (response.body["count"] || @resources.size).to_i
154
162
  @next_page, @prev_page = response.body["next_page"], response.body["previous_page"]
155
163
 
164
+ if @next_page =~ /page=(\d+)/
165
+ @options["page"] = $1.to_i - 1
166
+ elsif @prev_page =~ /page=(\d+)/
167
+ @options["page"] = $1.to_i + 1
168
+ end
169
+
156
170
  @resources
157
171
  end
158
172
 
@@ -163,6 +177,27 @@ module ZendeskAPI
163
177
  fetch
164
178
  end
165
179
 
180
+ # Calls #each on every page with the passed in block
181
+ # @param [Block] block Passed to #each
182
+ def each_page(&block)
183
+ page(nil)
184
+ clear_cache
185
+
186
+ while !empty?
187
+ each do |resource|
188
+ arguments = [resource, @options["page"] || 1]
189
+
190
+ if block.arity >= 0
191
+ arguments = arguments.take(block.arity)
192
+ end
193
+
194
+ block.call(*arguments)
195
+ end
196
+
197
+ self.next
198
+ end
199
+ end
200
+
166
201
  def replace(collection)
167
202
  raise "this collection is for #{@resource_class}" if collection.any?{|r| !r.is_a?(@resource_class) }
168
203
  @resources = collection
@@ -180,23 +215,25 @@ module ZendeskAPI
180
215
  @query = @next_page
181
216
  fetch(true)
182
217
  else
183
- []
218
+ clear_cache
219
+ @resources = []
184
220
  end
185
221
  end
186
222
 
187
- # Find the previous page. Does one of three things:
223
+ # Find the previous page. Does one of three things:
188
224
  # * If there is already a page number in the options hash, it increases it and invalidates the cache, returning the new page number.
189
225
  # * If there is a prev_page url cached, it executes a fetch on that url and returns the results.
190
226
  # * Otherwise, returns an empty array.
191
227
  def prev
192
- if @options["page"] && @options["page"] > 1
228
+ if @options["page"] && @options["page"] > 1
193
229
  clear_cache
194
230
  @options["page"] -= 1
195
231
  elsif @prev_page
196
232
  @query = @prev_page
197
233
  fetch(true)
198
234
  else
199
- []
235
+ clear_cache
236
+ @resources = []
200
237
  end
201
238
  end
202
239
 
@@ -1,5 +1,6 @@
1
1
  require "faraday/middleware"
2
2
  require "mime/types"
3
+ require 'tempfile'
3
4
 
4
5
  module ZendeskAPI
5
6
  module Middleware
@@ -68,7 +68,7 @@ module ZendeskAPI
68
68
 
69
69
  # Has this been object been created server-side? Does this by checking for an id.
70
70
  def new_record?
71
- id.nil?
71
+ id.nil?
72
72
  end
73
73
 
74
74
  # Returns the path to the resource
@@ -39,7 +39,7 @@ module ZendeskAPI
39
39
 
40
40
  has_many :comments, :class => :topic_comment
41
41
  has_many :subscriptions, :class => :topic_subscription
42
- has :vote, :class => :topic_vote
42
+ has :vote, :class => :topic_vote
43
43
 
44
44
  def votes(opts = {})
45
45
  return @votes if @votes && !opts[:reload]
@@ -34,7 +34,7 @@ module ZendeskAPI
34
34
  end
35
35
 
36
36
  def save
37
- upload = Upload.create!(@client, :file => file)
37
+ upload = Upload.create!(@client, attributes)
38
38
  self.token = upload.token
39
39
  end
40
40
 
@@ -1,4 +1,13 @@
1
1
  module ZendeskAPI
2
+ class Request < Resource
3
+ class Comment < ReadResource
4
+ has_many :attachments, :inline => true
5
+ end
6
+
7
+ has_many :comments
8
+ has :organization
9
+ end
10
+
2
11
  class TicketField < Resource; end
3
12
 
4
13
  class TicketComment < Data
@@ -19,7 +28,7 @@ module ZendeskAPI
19
28
  class Ticket < Resource
20
29
  class Audit < DataResource; end
21
30
 
22
- has :requester, :class => :user
31
+ has :requester, :class => :user, :inline => :create
23
32
  has :submitter, :class => :user
24
33
  has :assignee, :class => :user
25
34
  has :recipient, :class => :user
@@ -80,7 +89,7 @@ module ZendeskAPI
80
89
  has :execution, :class => :view_execution
81
90
 
82
91
  def self.preview(client, options = {})
83
- Zendesk::Collection.new(client, ViewRow, options.merge(:path => "views/preview"))
92
+ Collection.new(client, ViewRow, options.merge(:path => "views/preview", :verb => :post))
84
93
  end
85
94
  end
86
95
  end
@@ -20,6 +20,8 @@ module ZendeskAPI
20
20
  has :organization
21
21
  has :custom_role
22
22
  has_many :identities
23
+
24
+ has_many :requests
23
25
  has_many :requested_tickets, :class => :ticket, :path => 'tickets/requested'
24
26
  has_many :ccd_tickets, :class => :ticket, :path => 'tickets/ccd'
25
27
 
@@ -27,8 +29,8 @@ module ZendeskAPI
27
29
  has_many :group_memberships
28
30
  has_many :topics
29
31
 
30
- has_many :forum_subscriptions, :class => "forum/forum_subscription"
31
- has_many :topic_subscriptions, :class => "topic/topic_subscription"
32
+ has_many :forum_subscriptions, :class => "forum_subscription"
33
+ has_many :topic_subscriptions, :class => "topic_subscription"
32
34
  has_many :topic_comments, :class => "topic/topic_comment"
33
35
  has_many :topic_votes, :class => "topic/vote"
34
36
 
@@ -6,7 +6,7 @@ module ZendeskAPI
6
6
  class << self
7
7
  private
8
8
 
9
- # @macro [attach] container.create_verb
9
+ # @macro [attach] container.create_verb
10
10
  # @method $1(method)
11
11
  # Executes a $1 using the passed in method as a path.
12
12
  # Reloads the resource's attributes if any are in the response body.
@@ -16,7 +16,7 @@ module ZendeskAPI
16
16
  define_method verb do |method|
17
17
  define_method method do |*method_args|
18
18
  opts = method_args.last.is_a?(Hash) ? method_args.pop : {}
19
- return instance_variable_get("@#{method}") if instance_variable_defined?("@#{verb}") && !opts[:reload]
19
+ return instance_variable_get("@_#{verb}_#{method}") if instance_variable_defined?("@_#{verb}_#{method}") && !opts[:reload]
20
20
 
21
21
  response = @client.connection.send(verb, "#{path}/#{method}") do |req|
22
22
  req.body = opts
@@ -1,3 +1,3 @@
1
1
  module ZendeskAPI
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/live/group_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe ZendeskAPI::Group do
3
+ describe ZendeskAPI::Group, :delete_after do
4
4
  def valid_attributes
5
5
  { :name => "My Group" }
6
6
  end
@@ -10,5 +10,22 @@ describe ZendeskAPI::Group do
10
10
  it_should_be_deletable :find => [:deleted?, true]
11
11
  it_should_be_readable :groups
12
12
  it_should_be_readable :groups, :assignable
13
- it_should_be_readable agent, :groups
13
+
14
+ context "with a membership" do
15
+ before(:each) do
16
+ VCR.use_cassette("read_ZendeskAPI::User_groups_create") do
17
+ @object = described_class.create!(client, valid_attributes.merge(default_options))
18
+ @membership = agent.group_memberships.create(:group_id => @object.id, :user_id => agent.id)
19
+ end
20
+ end
21
+
22
+ after(:each) do
23
+ VCR.use_cassette("read_ZendeskAPI::User_groups_delete") do
24
+ @object.destroy
25
+ @membership.destroy
26
+ end
27
+ end
28
+
29
+ it_should_be_readable agent, :groups
30
+ end
14
31
  end
data/live/macro_spec.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ZendeskAPI::Macro do
4
+ it_should_be_readable :macros
4
5
  it_should_be_readable :macros, :active
5
6
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe ZendeskAPI::TicketField do
3
+ describe ZendeskAPI::TicketField, :delete_after do
4
4
  def valid_attributes
5
5
  { :type => "text", :title => "Age" }
6
6
  end
data/live/ticket_spec.rb CHANGED
@@ -2,13 +2,14 @@ require 'spec_helper'
2
2
 
3
3
  describe ZendeskAPI::Ticket do
4
4
  def valid_attributes
5
- {
5
+ {
6
6
  :type => "question",
7
7
  :subject => "This is a question?",
8
8
  :comment => { :value => "Indeed it is!" },
9
9
  :priority => "normal",
10
10
  :requester_id => user.id,
11
- :submitter_id => user.id
11
+ :submitter_id => user.id,
12
+ :collaborator_ids => [agent.id]
12
13
  }
13
14
  end
14
15
 
@@ -18,7 +19,7 @@ describe ZendeskAPI::Ticket do
18
19
  it_should_be_readable :tickets
19
20
  it_should_be_readable :tickets, :recent
20
21
  it_should_be_readable user, :requested_tickets
21
- it_should_be_readable user, :ccd_tickets
22
+ it_should_be_readable agent, :ccd_tickets
22
23
  it_should_be_readable organization, :tickets
23
24
 
24
25
  describe ".incremental_export" do
@@ -39,7 +40,7 @@ describe ZendeskAPI::Ticket do
39
40
 
40
41
  it "is able to do next" do
41
42
  first = results.to_a.first
42
- recent_url = "api/v2/exports/tickets.json?start_time=#{Time.now.to_i.to_s[0..5]}"
43
+ recent_url = "api/v2/exports/tickets.json\\?start_time=#{Time.now.to_i.to_s[0..5]}"
43
44
  stub_json_request(:get, /#{recent_url}/, json(:results => []))
44
45
 
45
46
  results.next
@@ -88,6 +88,51 @@ describe ZendeskAPI::Collection do
88
88
  end
89
89
  end
90
90
 
91
+ context "pagination with data" do
92
+ before(:each) do
93
+ stub_json_request(:get, %r{test_resources}, json(
94
+ :test_resources => [{ :id => 1 }]
95
+ ))
96
+ subject.fetch(true)
97
+ end
98
+
99
+ context "on #page" do
100
+ context "with nil" do
101
+ before(:each) { subject.page(nil) }
102
+
103
+ it "should not empty the cache" do
104
+ subject.instance_variable_get(:@resources).should_not be_empty
105
+ end
106
+ end
107
+
108
+ context "with a number" do
109
+ before(:each) { subject.page(3) }
110
+
111
+ it "should empty the cache" do
112
+ subject.instance_variable_get(:@resources).should be_nil
113
+ end
114
+ end
115
+ end
116
+
117
+ context "on #per_page" do
118
+ context "with nil" do
119
+ before(:each) { subject.per_page(nil) }
120
+
121
+ it "should not empty the cache" do
122
+ subject.instance_variable_get(:@resources).should_not be_empty
123
+ end
124
+ end
125
+
126
+ context "with a number" do
127
+ before(:each) { subject.per_page(20) }
128
+
129
+ it "should empty the cache" do
130
+ subject.instance_variable_get(:@resources).should be_nil
131
+ end
132
+ end
133
+ end
134
+ end
135
+
91
136
  context "pagination with no options and no data" do
92
137
  it "should return an empty array on #next" do
93
138
  subject.next.should be_empty
@@ -120,7 +165,100 @@ describe ZendeskAPI::Collection do
120
165
  end
121
166
  end
122
167
 
168
+ context "each_page" do
169
+ before(:each) do
170
+ stub_json_request(:get, %r{test_resources$}, json(
171
+ :test_resources => [{:id => 1}],
172
+ :next_page => "/test_resources?page=2"
173
+ ))
174
+
175
+ stub_json_request(:get, %r{test_resources\?page=2}, json(
176
+ :test_resources => [{:id => 2}],
177
+ :next_page => "/test_resources?page=3"
178
+ ))
179
+
180
+ stub_request(:get, %r{test_resources\?page=3}).to_return(:status => 404)
181
+ end
182
+
183
+ it "should yield resource if arity == 1" do
184
+ expect do |block|
185
+ # Needed to make sure the arity == 1
186
+ block.instance_eval do
187
+ def to_proc
188
+ @used = true
189
+
190
+ probe = self
191
+ Proc.new do |arg|
192
+ probe.num_yields += 1
193
+ probe.yielded_args << [arg]
194
+ end
195
+ end
196
+ end
197
+
198
+ silence_logger { subject.each_page(&block) }
199
+ end.to yield_successive_args(
200
+ ZendeskAPI::TestResource.new(client, :id => 1),
201
+ ZendeskAPI::TestResource.new(client, :id => 2)
202
+ )
203
+ end
204
+
205
+ it "should yield resource and page" do
206
+ expect do |b|
207
+ silence_logger { subject.each_page(&b) }
208
+ end.to yield_successive_args(
209
+ [ZendeskAPI::TestResource.new(client, :id => 1), 1],
210
+ [ZendeskAPI::TestResource.new(client, :id => 2), 2]
211
+ )
212
+ end
213
+ end
214
+
123
215
  context "fetch" do
216
+ context "grabbing the current page" do
217
+ context "from next_page" do
218
+ before(:each) do
219
+ stub_json_request(:get, %r{test_resources}, json(
220
+ :test_resources => [{:id => 2}],
221
+ :next_page => "/test_resources?page=2"
222
+ ))
223
+
224
+ subject.fetch(true)
225
+ @page = subject.instance_variable_get(:@options)["page"]
226
+ end
227
+
228
+ it "should set the page to 1" do
229
+ @page.should == 1
230
+ end
231
+ end
232
+
233
+ context "from prev_page" do
234
+ before(:each) do
235
+ stub_json_request(:get, %r{test_resources}, json(
236
+ :test_resources => [{:id => 2}],
237
+ :previous_page => "/test_resources?page=1"
238
+ ))
239
+
240
+ subject.fetch(true)
241
+ @page = subject.instance_variable_get(:@options)["page"]
242
+ end
243
+
244
+ it "should set the page to 2" do
245
+ @page.should == 2
246
+ end
247
+ end
248
+
249
+ context "with nothing" do
250
+ before(:each) do
251
+ stub_json_request(:get, %r{test_resources}, json(:test_resources => [{:id => 2}]))
252
+ subject.fetch(true)
253
+ @page = subject.instance_variable_get(:@options)["page"]
254
+ end
255
+
256
+ it "should not set the page" do
257
+ @page.should be_nil
258
+ end
259
+ end
260
+ end
261
+
124
262
  it "does not fetch if associated is a new record" do
125
263
  ZendeskAPI::Category.new(client).forums.fetch.should == []
126
264
  ZendeskAPI::Category.new(client).forums.to_a.should == []
@@ -70,11 +70,11 @@ module ResourceMacros
70
70
  end
71
71
  end
72
72
 
73
- after(:all, :if => metadata[:delete_after]) do
73
+ after(:all) do
74
74
  VCR.use_cassette("#{described_class.to_s}_update_delete") do
75
75
  @object.destroy
76
76
  end
77
- end
77
+ end if metadata[:delete_after]
78
78
  end
79
79
  end
80
80
 
@@ -58,7 +58,7 @@ describe ZendeskAPI::Middleware::Request::Upload do
58
58
 
59
59
  context "with an ActionDispatch::Http::UploadedFile" do
60
60
  before(:each) do
61
- @upload = ActionDispatch::Http::UploadedFile.new(:filename => "hello", :tempfile => Tempfile.new(filename))
61
+ @upload = ActionDispatch::Http::UploadedFile.new(:filename => "hello", :tempfile => Tempfile.new(File.basename(filename)))
62
62
  @env = subject.call(:body => { :file => @upload })
63
63
  end
64
64
 
@@ -78,7 +78,7 @@ describe ZendeskAPI::Middleware::Request::Upload do
78
78
 
79
79
  context "with a Tempfile" do
80
80
  before(:each) do
81
- @tempfile = Tempfile.new(filename)
81
+ @tempfile = Tempfile.new(File.basename(filename))
82
82
  @env = subject.call(:body => { :file => @tempfile })
83
83
  end
84
84
 
@@ -163,12 +163,18 @@ describe ZendeskAPI::Resource do
163
163
  it "should call save on the association" do
164
164
  subject.child.foo = "bar"
165
165
  subject.child.should_receive(:save)
166
+
166
167
  subject.save
168
+
169
+ subject.instance_variable_get(:@child).should be_nil
167
170
  end
168
171
 
169
172
  it "should not call save on the association if they are synced" do
170
173
  subject.child.should_not_receive(:save)
174
+
171
175
  subject.save
176
+
177
+ subject.instance_variable_get(:@child).should be_nil
172
178
  end
173
179
  end
174
180
 
@@ -186,24 +192,28 @@ describe ZendeskAPI::Resource do
186
192
  subject.children_ids = [1]
187
193
  subject.save
188
194
  subject.children_ids.should == [2,3]
195
+ subject.instance_variable_get(:@children).should be_nil
189
196
  end
190
197
 
191
198
  it "should not save the associated objects when there are no changes" do
192
199
  subject.children = [2]
193
200
  subject.children.first.should_not_receive(:save)
194
201
  subject.save
202
+ subject.instance_variable_get(:@children).should be_nil
195
203
  end
196
204
 
197
205
  it "should save the associated objects when it is new" do
198
206
  subject.children = [{:foo => "bar"}]
199
207
  subject.children.first.should_receive(:save)
200
208
  subject.save
209
+ subject.instance_variable_get(:@children).should be_nil
201
210
  end
202
211
 
203
212
  it "should not save the associated objects when it is set via full hash" do
204
213
  subject.children = [{:id => 1, :foo => "bar"}]
205
214
  subject.children.first.should_not_receive(:save)
206
215
  subject.save
216
+ subject.instance_variable_get(:@children).should be_nil
207
217
  end
208
218
 
209
219
  it "should save the associated objects when it is changes" do
@@ -211,6 +221,7 @@ describe ZendeskAPI::Resource do
211
221
  subject.children.first.foo = "bar"
212
222
  subject.children.first.should_receive(:save)
213
223
  subject.save
224
+ subject.instance_variable_get(:@children).should be_nil
214
225
  end
215
226
  end
216
227
 
@@ -221,14 +232,47 @@ describe ZendeskAPI::Resource do
221
232
  end
222
233
 
223
234
  ZendeskAPI::TestResource.associations.clear
224
- ZendeskAPI::TestResource.has :nil, :class => :nil_resource, :inline => true
235
+ end
225
236
 
226
- subject.nil = { :abc => :def }
227
- subject.save_associations
237
+ context "true" do
238
+ before(:each) do
239
+ ZendeskAPI::TestResource.has :nil, :class => :nil_resource, :inline => true
240
+
241
+ subject.nil = { :abc => :def }
242
+ subject.save_associations
243
+ end
244
+
245
+ it "should save param data" do
246
+ subject.attributes[:nil].should == "TESTDATA"
247
+ end
228
248
  end
229
249
 
230
- it "should save param data" do
231
- subject.attributes[:nil].should == "TESTDATA"
250
+ context "create" do
251
+ before(:each) do
252
+ ZendeskAPI::TestResource.has :nil, :class => :nil_resource, :inline => :create
253
+ subject.nil = { :abc => :def }
254
+ end
255
+
256
+ context "with a new record" do
257
+ before(:each) do
258
+ subject.id = nil
259
+ subject.save_associations
260
+ end
261
+
262
+ it "should save param data" do
263
+ subject.attributes[:nil].should == "TESTDATA"
264
+ end
265
+ end
266
+
267
+ context "with a saved record" do
268
+ before(:each) do
269
+ subject.save_associations
270
+ end
271
+
272
+ it "should not save param data" do
273
+ subject.attributes[:nil].should be_nil
274
+ end
275
+ end
232
276
  end
233
277
  end
234
278
  end
@@ -317,6 +361,16 @@ describe ZendeskAPI::Resource do
317
361
  end
318
362
  end
319
363
 
364
+ context "SingularTestResource" do
365
+ context "#find" do
366
+ it "should not require an id" do
367
+ expect do
368
+ ZendeskAPI::SingularTestResource.find(client)
369
+ end.to_not raise_error(ArgumentError)
370
+ end
371
+ end
372
+ end
373
+
320
374
  context "#new" do
321
375
  it "builds with hash" do
322
376
  object = ZendeskAPI::TestResource.new(client, {})
data/zendesk_api.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.required_ruby_version = ">= 1.8.7"
19
19
  s.required_rubygems_version = ">= 1.3.6"
20
20
 
21
- s.add_development_dependency "rspec", "~> 2.10.0"
21
+ s.add_development_dependency "rspec"
22
22
  s.add_development_dependency "vcr"
23
23
  s.add_development_dependency "webmock"
24
24
  s.add_development_dependency "rake"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zendesk_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,24 +10,24 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-08-27 00:00:00.000000000 Z
13
+ date: 2012-09-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
- - - ~>
20
+ - - ! '>='
21
21
  - !ruby/object:Gem::Version
22
- version: 2.10.0
22
+ version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  none: false
27
27
  requirements:
28
- - - ~>
28
+ - - ! '>='
29
29
  - !ruby/object:Gem::Version
30
- version: 2.10.0
30
+ version: '0'
31
31
  - !ruby/object:Gem::Dependency
32
32
  name: vcr
33
33
  requirement: !ruby/object:Gem::Requirement