zendesk_api 0.3.14 → 0.4.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. data/.yardopts +6 -2
  2. data/Gemfile +3 -2
  3. data/Gemfile.lock +4 -4
  4. data/Readme.md +3 -268
  5. data/lib/zendesk_api/actions.rb +51 -46
  6. data/lib/zendesk_api/association.rb +1 -1
  7. data/lib/zendesk_api/associations.rb +2 -6
  8. data/lib/zendesk_api/client.rb +8 -9
  9. data/lib/zendesk_api/collection.rb +84 -50
  10. data/lib/zendesk_api/configuration.rb +4 -0
  11. data/lib/zendesk_api/error.rb +22 -0
  12. data/lib/zendesk_api/middleware/request/etag_cache.rb +1 -7
  13. data/lib/zendesk_api/middleware/request/upload.rb +1 -2
  14. data/lib/zendesk_api/middleware/response/raise_error.rb +20 -0
  15. data/lib/zendesk_api/resource.rb +33 -5
  16. data/lib/zendesk_api/resources.rb +36 -11
  17. data/lib/zendesk_api/server/base.rb +1 -1
  18. data/lib/zendesk_api/server/helper.rb +3 -3
  19. data/lib/zendesk_api/server/models/zlib_json.rb +1 -1
  20. data/lib/zendesk_api/server/public/doc/ZendeskAPI.html +7 -7
  21. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Ability.html +415 -13
  22. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Activity.html +414 -51
  23. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Attachment.html +321 -25
  24. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Automation.html +413 -46
  25. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Bookmark.html +389 -26
  26. data/lib/zendesk_api/server/public/doc/ZendeskAPI/CRMData.html +415 -13
  27. data/lib/zendesk_api/server/public/doc/ZendeskAPI/CRMDataStatus.html +415 -13
  28. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Category.html +410 -47
  29. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Client.html +98 -362
  30. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Collection.html +457 -676
  31. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Configuration.html +110 -20
  32. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Create.html +264 -6
  33. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Create/ClassMethods.html +43 -82
  34. data/lib/zendesk_api/server/public/doc/ZendeskAPI/CreateResource.html +671 -15
  35. data/lib/zendesk_api/server/public/doc/ZendeskAPI/CustomRole.html +415 -13
  36. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Data.html +207 -233
  37. data/lib/zendesk_api/server/public/doc/ZendeskAPI/DataResource.html +499 -24
  38. data/lib/zendesk_api/server/public/doc/ZendeskAPI/DeleteResource.html +614 -15
  39. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Destroy.html +73 -23
  40. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Destroy/ClassMethod.html +68 -18
  41. data/lib/zendesk_api/server/public/doc/ZendeskAPI/{FormatError.html → Error.html} +19 -27
  42. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Error/ClientError.html +127 -0
  43. data/lib/zendesk_api/server/public/doc/ZendeskAPI/{Server/UserRequest.html → Error/NetworkError.html} +18 -16
  44. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Error/RecordInvalid.html +371 -0
  45. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Error/RecordNotFound.html +127 -0
  46. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Forum.html +426 -63
  47. data/lib/zendesk_api/server/public/doc/ZendeskAPI/ForumSubscription.html +414 -51
  48. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Group.html +389 -26
  49. data/lib/zendesk_api/server/public/doc/ZendeskAPI/GroupMembership.html +414 -51
  50. data/lib/zendesk_api/server/public/doc/ZendeskAPI/JobStatus.html +405 -16
  51. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Locale.html +405 -16
  52. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Macro.html +413 -46
  53. data/lib/zendesk_api/server/public/doc/ZendeskAPI/MobileDevice.html +404 -41
  54. data/lib/zendesk_api/server/public/doc/ZendeskAPI/OAuthClient.html +606 -0
  55. data/lib/zendesk_api/server/public/doc/ZendeskAPI/OAuthToken.html +810 -0
  56. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Organization.html +422 -59
  57. data/lib/zendesk_api/server/public/doc/ZendeskAPI/OrganizationField.html +606 -0
  58. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Read.html +75 -12
  59. data/lib/zendesk_api/server/public/doc/ZendeskAPI/ReadResource.html +593 -16
  60. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Request.html +418 -55
  61. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Request/Comment.html +420 -31
  62. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Resource.html +1025 -20
  63. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Role.html +415 -13
  64. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Rule.html +610 -0
  65. data/lib/zendesk_api/server/public/doc/ZendeskAPI/RuleExecution.html +317 -21
  66. data/lib/zendesk_api/server/public/doc/ZendeskAPI/SatisfactionRating.html +428 -39
  67. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Save.html +63 -79
  68. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Search.html +34 -26
  69. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Search/Result.html +305 -9
  70. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Setting.html +433 -41
  71. data/lib/zendesk_api/server/public/doc/ZendeskAPI/SharingAgreement.html +405 -16
  72. data/lib/zendesk_api/server/public/doc/ZendeskAPI/SingularResource.html +389 -26
  73. data/lib/zendesk_api/server/public/doc/ZendeskAPI/SuspendedTicket.html +600 -31
  74. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Ticket.html +464 -101
  75. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Ticket/Audit.html +427 -25
  76. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Ticket/Comment.html +435 -32
  77. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Ticket/Tag.html +389 -26
  78. data/lib/zendesk_api/server/public/doc/ZendeskAPI/TicketField.html +389 -26
  79. data/lib/zendesk_api/server/public/doc/ZendeskAPI/TicketMetric.html +592 -15
  80. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Topic.html +427 -64
  81. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Topic/TopicComment.html +907 -17
  82. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Topic/TopicVote.html +414 -55
  83. data/lib/zendesk_api/server/public/doc/ZendeskAPI/TopicComment.html +325 -29
  84. data/lib/zendesk_api/server/public/doc/ZendeskAPI/TopicSubscription.html +414 -51
  85. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Trigger.html +413 -46
  86. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Update.html +258 -6
  87. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Update/ClassMethod.html +62 -12
  88. data/lib/zendesk_api/server/public/doc/ZendeskAPI/UpdateResource.html +671 -15
  89. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Upload.html +800 -28
  90. data/lib/zendesk_api/server/public/doc/ZendeskAPI/User.html +505 -142
  91. data/lib/zendesk_api/server/public/doc/ZendeskAPI/User/Identity.html +412 -49
  92. data/lib/zendesk_api/server/public/doc/ZendeskAPI/User/TopicComment.html +475 -13
  93. data/lib/zendesk_api/server/public/doc/ZendeskAPI/UserField.html +606 -0
  94. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Verbs.html +9 -15
  95. data/lib/zendesk_api/server/public/doc/ZendeskAPI/View.html +433 -66
  96. data/lib/zendesk_api/server/public/doc/ZendeskAPI/ViewCount.html +415 -13
  97. data/lib/zendesk_api/server/public/doc/ZendeskAPI/ViewRow.html +451 -49
  98. data/lib/zendesk_api/server/public/doc/_index.html +72 -72
  99. data/lib/zendesk_api/server/public/doc/class_list.html +1 -1
  100. data/lib/zendesk_api/server/public/doc/css/style.css +10 -0
  101. data/lib/zendesk_api/server/public/doc/file.Readme.html +20 -260
  102. data/lib/zendesk_api/server/public/doc/file_list.html +1 -1
  103. data/lib/zendesk_api/server/public/doc/frames.html +1 -1
  104. data/lib/zendesk_api/server/public/doc/index.html +20 -260
  105. data/lib/zendesk_api/server/public/doc/method_list.html +309 -517
  106. data/lib/zendesk_api/server/public/doc/top-level-namespace.html +3 -35
  107. data/lib/zendesk_api/verbs.rb +0 -4
  108. data/lib/zendesk_api/version.rb +1 -1
  109. data/spec/core/client_spec.rb +53 -13
  110. data/spec/core/collection_spec.rb +32 -4
  111. data/spec/core/create_resource_spec.rb +1 -1
  112. data/spec/core/data_resource_spec.rb +2 -2
  113. data/spec/core/middleware/request/etag_cache_spec.rb +6 -7
  114. data/spec/core/middleware/request/upload_spec.rb +2 -10
  115. data/spec/core/middleware/response/callback_spec.rb +1 -1
  116. data/spec/core/middleware/response/raise_error_spec.rb +40 -0
  117. data/spec/core/read_resource_spec.rb +0 -2
  118. data/spec/core/resource_spec.rb +13 -5
  119. data/spec/core/search_spec.rb +1 -1
  120. data/spec/core/spec_helper.rb +3 -3
  121. data/spec/live/automation_spec.rb +23 -0
  122. data/spec/live/organization_field_spec.rb +12 -0
  123. data/spec/live/ticket_spec.rb +8 -2
  124. data/spec/live/trigger_spec.rb +23 -0
  125. data/spec/live/user_field_spec.rb +12 -0
  126. data/spec/live/user_spec.rb +0 -2
  127. data/spec/live/view_spec.rb +17 -1
  128. data/spec/server/spec_helper.rb +2 -2
  129. data/zendesk_api.gemspec +1 -1
  130. metadata +88 -51
  131. checksums.yaml +0 -7
  132. data/lib/zendesk_api/rescue.rb +0 -63
  133. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Console.html +0 -860
  134. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Console/Eval.html +0 -190
  135. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Server.html +0 -131
  136. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Server/App.html +0 -523
  137. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Server/Helper.html +0 -684
  138. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Server/HtmlRenderer.html +0 -303
  139. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Server/HtmlRenderer/RedcarpetRenderer.html +0 -508
  140. data/lib/zendesk_api/server/public/doc/ZendeskAPI/Server/ZlibJSON.html +0 -244
  141. data/spec/core/rescue_spec.rb +0 -151
@@ -41,7 +41,7 @@ module ZendeskAPI
41
41
  namespace[1..1] = parent_namespace if parent_namespace
42
42
  namespace[0] = parent_class.resource_name
43
43
  else
44
- namespace[0] = @options.path || @options[:class].resource_name
44
+ namespace[0] = @options.path || @options[:class].resource_path
45
45
  end
46
46
 
47
47
  if id = extract_id(instance, options, original_options)
@@ -30,8 +30,6 @@ module ZendeskAPI
30
30
 
31
31
  # @private
32
32
  module ClassMethods
33
- include Rescue
34
-
35
33
  def self.extended(klass)
36
34
  klass.extend Has
37
35
  klass.extend HasMany
@@ -112,10 +110,8 @@ module ZendeskAPI
112
110
  elsif found = method_missing(association[:name].to_sym)
113
111
  wrap_resource(found, association, :include_key => association[:include_key])
114
112
  elsif klass.superclass == DataResource && !association[:inline]
115
- rescue_client_error do
116
- response = @client.connection.get(instance_association.generate_path(:with_parent => true))
117
- klass.new(@client, response.body[klass.singular_resource_name].merge(:association => instance_association))
118
- end
113
+ response = @client.connection.get(instance_association.generate_path(:with_parent => true))
114
+ klass.new(@client, response.body[klass.singular_resource_name].merge(:association => instance_association))
119
115
  end
120
116
 
121
117
  send("#{association[:id_column]}=", resource.id) if resource && has_key?(association[:id_column])
@@ -2,7 +2,6 @@ require 'faraday'
2
2
  require 'faraday_middleware'
3
3
 
4
4
  require 'zendesk_api/version'
5
- require 'zendesk_api/rescue'
6
5
  require 'zendesk_api/sideloading'
7
6
  require 'zendesk_api/configuration'
8
7
  require 'zendesk_api/collection'
@@ -14,14 +13,13 @@ require 'zendesk_api/middleware/response/callback'
14
13
  require 'zendesk_api/middleware/response/deflate'
15
14
  require 'zendesk_api/middleware/response/gzip'
16
15
  require 'zendesk_api/middleware/response/parse_iso_dates'
16
+ require 'zendesk_api/middleware/response/raise_error'
17
17
  require 'zendesk_api/middleware/response/logger'
18
18
 
19
19
  module ZendeskAPI
20
20
  # The top-level class that handles configuration and connection to the Zendesk API.
21
21
  # Can also be used as an accessor to resource collections.
22
22
  class Client
23
- include Rescue
24
-
25
23
  # @return [Configuration] Config instance
26
24
  attr_reader :config
27
25
  # @return [Array] Custom response callbacks
@@ -57,8 +55,6 @@ module ZendeskAPI
57
55
  @current_account = Hashie::Mash.new(connection.get('account/resolve').body)
58
56
  end
59
57
 
60
- rescue_client_error :current_account
61
-
62
58
  # Returns the current locale
63
59
  # @return [ZendeskAPI::Locale] Current locale or nil
64
60
  def current_locale(reload = false)
@@ -86,6 +82,7 @@ module ZendeskAPI
86
82
  config.retry = !!config.retry # nil -> false
87
83
 
88
84
  set_token_auth
85
+
89
86
  set_default_logger
90
87
  add_warning_callback
91
88
  end
@@ -125,8 +122,7 @@ module ZendeskAPI
125
122
  def build_connection
126
123
  Faraday.new(config.options) do |builder|
127
124
  # response
128
- builder.use Faraday::Request::BasicAuthentication, config.username, config.password
129
- builder.use Faraday::Response::RaiseError
125
+ builder.use ZendeskAPI::Middleware::Response::RaiseError
130
126
  builder.use ZendeskAPI::Middleware::Response::Callback, self
131
127
  builder.use ZendeskAPI::Middleware::Response::Logger, config.logger if config.logger
132
128
  builder.use ZendeskAPI::Middleware::Response::ParseIsoDates
@@ -140,10 +136,13 @@ module ZendeskAPI
140
136
  end
141
137
 
142
138
  # request
143
- if config.cache
144
- builder.use ZendeskAPI::Middleware::Request::EtagCache, :cache => config.cache
139
+ if config.access_token
140
+ builder.authorization("Bearer", config.access_token)
141
+ else
142
+ builder.use Faraday::Request::BasicAuthentication, config.username, config.password
145
143
  end
146
144
 
145
+ builder.use ZendeskAPI::Middleware::Request::EtagCache, :cache => config.cache
147
146
  builder.use ZendeskAPI::Middleware::Request::Upload
148
147
  builder.request :multipart
149
148
  builder.request :json
@@ -10,8 +10,6 @@ module ZendeskAPI
10
10
  # Options passed in that are automatically converted from an array to a comma-separated list.
11
11
  SPECIALLY_JOINED_PARAMS = [:ids, :only]
12
12
 
13
- include Rescue
14
-
15
13
  # @return [ZendeskAPI::Association] The class association
16
14
  attr_reader :association
17
15
 
@@ -45,29 +43,21 @@ module ZendeskAPI
45
43
  end
46
44
  end
47
45
 
48
- # Passes arguments and the proper path to the resource class method.
49
- # @param [Hash] attributes Attributes to pass to Create#create
50
- def create(attributes = {})
51
- attributes.merge!(:association => @association)
52
- @resource_class.create(@client, @options.merge(attributes))
53
- end
46
+ methods = %w{create find update destroy}
47
+ methods += methods.map {|method| method + "!"}
48
+ methods.each do |deferrable|
49
+ # Passes arguments and the proper path to the resource class method.
50
+ # @param [Hash] options Options or attributes to pass
51
+ define_method deferrable do |*args|
52
+ unless @resource_class.respond_to?(deferrable)
53
+ raise NoMethodError.new("undefined method \"#{deferrable}\" for #{@resource_class}", deferrable, args)
54
+ end
54
55
 
55
- # (see #create)
56
- def find(opts = {})
57
- opts.merge!(:association => @association)
58
- @resource_class.find(@client, @options.merge(opts))
59
- end
56
+ opts = args.last.is_a?(Hash) ? args.pop : {}
57
+ opts.merge!(:association => @association)
60
58
 
61
- # (see #create)
62
- def update(opts = {})
63
- opts.merge!(:association => @association)
64
- @resource_class.update(@client, @options.merge(opts))
65
- end
66
-
67
- # (see #create)
68
- def destroy(opts = {})
69
- opts.merge!(:association => association)
70
- @resource_class.destroy(@client, @options.merge(opts))
59
+ @resource_class.send(deferrable, @client, @options.merge(opts))
60
+ end
71
61
  end
72
62
 
73
63
  # @return [Number] The total number of resources server-side (disregarding pagination).
@@ -76,6 +66,12 @@ module ZendeskAPI
76
66
  @count || -1
77
67
  end
78
68
 
69
+ # @return [Number] The total number of resources server-side (disregarding pagination).
70
+ def count!
71
+ fetch!
72
+ @count || -1
73
+ end
74
+
79
75
  # Changes the per_page option. Returns self, so it can be chained. No execution.
80
76
  # @return [Collection] self
81
77
  def per_page(count)
@@ -103,17 +99,13 @@ module ZendeskAPI
103
99
  # Saves all newly created resources stored in this collection.
104
100
  # @return [Collection] self
105
101
  def save
106
- if @resources
107
- @resources.map! do |item|
108
- unless !item.respond_to?(:save) || item.changes.empty?
109
- item.save
110
- end
111
-
112
- item
113
- end
114
- end
102
+ _save
103
+ end
115
104
 
116
- self
105
+ # Saves all newly created resources stored in this collection.
106
+ # @return [Collection] self
107
+ def save!
108
+ _save(:save!)
117
109
  end
118
110
 
119
111
  # Adds an item (or items) to the list of side-loaded resources to request
@@ -146,7 +138,7 @@ module ZendeskAPI
146
138
 
147
139
  # Executes actual GET from API and loads resources into proper class.
148
140
  # @param [Boolean] reload Whether to disregard cache
149
- def fetch(reload = false)
141
+ def fetch!(reload = false)
150
142
  if @resources && (!@fetchable || !reload)
151
143
  return @resources
152
144
  elsif association && association.options.parent && association.options.parent.new_record?
@@ -160,32 +152,42 @@ module ZendeskAPI
160
152
  @resources
161
153
  end
162
154
 
163
- rescue_client_error :fetch, :with => lambda { Array.new }
155
+ def fetch(*args)
156
+ fetch!(*args)
157
+ rescue Faraday::Error::ClientError => e
158
+ []
159
+ end
164
160
 
165
161
  # Alias for fetch(false)
166
162
  def to_a
167
163
  fetch
168
164
  end
169
165
 
166
+ # Alias for fetch!(false)
167
+ def to_a!
168
+ fetch!
169
+ end
170
+
170
171
  # Calls #each on every page with the passed in block
171
172
  # @param [Block] block Passed to #each
172
- def each_page(start_page = @options["page"], &block)
173
- page(start_page)
174
- clear_cache
175
-
176
- while !empty?
177
- each do |resource|
178
- arguments = [resource, @options["page"] || 1]
173
+ def all!(start_page = @options["page"], &block)
174
+ _all(start_page, :bang, &block)
175
+ end
179
176
 
180
- if block.arity >= 0
181
- arguments = arguments.take(block.arity)
182
- end
177
+ # Calls #each on every page with the passed in block
178
+ # @param [Block] block Passed to #each
179
+ def all(start_page = @options["page"], &block)
180
+ _all(start_page, &block)
181
+ end
183
182
 
184
- block.call(*arguments)
185
- end
183
+ def each_page!(*args, &block)
184
+ warn "ZendeskAPI::Collection#each_page! is deprecated, please use ZendeskAPI::Collection#all!"
185
+ all!(*args, &block)
186
+ end
186
187
 
187
- self.next
188
- end
188
+ def each_page(*args, &block)
189
+ warn "ZendeskAPI::Collection#each_page is deprecated, please use ZendeskAPI::Collection#all"
190
+ all(*args, &block)
189
191
  end
190
192
 
191
193
  # Replaces the current (loaded or not) resources with the passed in collection
@@ -234,7 +236,6 @@ module ZendeskAPI
234
236
  @count = nil
235
237
  @next_page = nil
236
238
  @prev_page = nil
237
- @query = nil
238
239
  end
239
240
 
240
241
  # @private
@@ -278,6 +279,39 @@ module ZendeskAPI
278
279
  end
279
280
  end
280
281
 
282
+ def _all(start_page = @options["page"], bang = false, &block)
283
+ page(start_page)
284
+ clear_cache
285
+
286
+ while (bang ? fetch! : fetch) && !empty?
287
+ each do |resource|
288
+ arguments = [resource, @options["page"] || 1]
289
+
290
+ if block.arity >= 0
291
+ arguments = arguments.take(block.arity)
292
+ end
293
+
294
+ block.call(*arguments)
295
+ end
296
+
297
+ self.next
298
+ end
299
+ end
300
+
301
+ def _save(method = :save)
302
+ if @resources
303
+ @resources.map! do |item|
304
+ if item.respond_to?(method) && !item.changes.empty?
305
+ item.send(method)
306
+ end
307
+
308
+ item
309
+ end
310
+ end
311
+
312
+ self
313
+ end
314
+
281
315
  ## Initialize
282
316
 
283
317
  def join_special_params
@@ -28,6 +28,9 @@ module ZendeskAPI
28
28
  # @return [Boolean] Whether to allow non-HTTPS connections for development purposes.
29
29
  attr_accessor :allow_http
30
30
 
31
+ # @return [String] OAuth2 access_token
32
+ attr_accessor :access_token
33
+
31
34
  # Use this cache instead of default ZendeskAPI::LRUCache.new
32
35
  # - must respond to read/write/fetch e.g. ActiveSupport::Cache::MemoryStore.new)
33
36
  # - pass false to disable caching
@@ -36,6 +39,7 @@ module ZendeskAPI
36
39
 
37
40
  def initialize
38
41
  @client_options = {}
42
+
39
43
  self.cache = ZendeskAPI::LRUCache.new(1000)
40
44
  end
41
45
 
@@ -0,0 +1,22 @@
1
+ module ZendeskAPI
2
+ module Error
3
+ class ClientError < Faraday::Error::ClientError; end
4
+
5
+ class RecordInvalid < ClientError
6
+ attr_accessor :response, :errors
7
+
8
+ def initialize(response)
9
+ @response = response
10
+
11
+ if response[:body].is_a?(Hash) && response[:body].key?("details")
12
+ @errors = response[:body]["details"]
13
+ else
14
+ @errors = {}
15
+ end
16
+ end
17
+ end
18
+
19
+ class NetworkError < ClientError; end
20
+ class RecordNotFound < ClientError; end
21
+ end
22
+ end
@@ -30,13 +30,7 @@ module ZendeskAPI
30
30
 
31
31
  @app.call(env).on_complete do
32
32
  if cached && env[:status] == 304 # not modified
33
- env[:body] = cached[:body]
34
- env[:response_headers].merge!(
35
- :etag => cached[:response_headers][:etag],
36
- :content_type => cached[:response_headers][:content_type],
37
- :content_length => cached[:response_headers][:content_length],
38
- :content_encoding => cached[:response_headers][:content_encoding]
39
- )
33
+ env.merge!(:body => cached[:body], :response_headers => cached[:response_headers])
40
34
  elsif env[:status] == 200 && env[:response_headers]["Etag"] # modified and cacheable
41
35
  @cache.write(cache_key(env), env)
42
36
  end
@@ -38,7 +38,6 @@ module ZendeskAPI
38
38
  else
39
39
  if defined?(ActionDispatch) && file.is_a?(ActionDispatch::Http::UploadedFile)
40
40
  path = file.tempfile.path
41
- mime_type = file.content_type
42
41
  else
43
42
  warn "WARNING: Passed invalid filename #{file} of type #{file.class} to upload"
44
43
  end
@@ -50,7 +49,7 @@ module ZendeskAPI
50
49
  hash = hash[key]
51
50
  end
52
51
 
53
- mime_type ||= MIME::Types.type_for(path).first || "application/octet-stream"
52
+ mime_type = MIME::Types.type_for(path).first || "application/octet-stream"
54
53
 
55
54
  hash[:filename] ||= if file.respond_to?(:original_filename)
56
55
  file.original_filename
@@ -0,0 +1,20 @@
1
+ require 'zendesk_api/error'
2
+
3
+ module ZendeskAPI
4
+ module Middleware
5
+ module Response
6
+ class RaiseError < Faraday::Response::RaiseError
7
+ def on_complete(env)
8
+ case env[:status]
9
+ when 404
10
+ raise Error::RecordNotFound.new(env)
11
+ when 422
12
+ raise Error::RecordInvalid.new(env)
13
+ when 400...600
14
+ raise Error::NetworkError.new(env)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -9,7 +9,6 @@ module ZendeskAPI
9
9
  # Represents a resource that only holds data.
10
10
  class Data
11
11
  include Associations
12
- include Rescue
13
12
 
14
13
  class << self
15
14
  def inherited(klass)
@@ -30,6 +29,10 @@ module ZendeskAPI
30
29
  @resource_name ||= Inflection.plural(singular_resource_name)
31
30
  end
32
31
 
32
+ def resource_path
33
+ [@namespace, resource_name].compact.join("/")
34
+ end
35
+
33
36
  alias :model_key :resource_name
34
37
 
35
38
  # @private
@@ -41,13 +44,18 @@ module ZendeskAPI
41
44
  def unnested_params
42
45
  @unnested_params ||= false
43
46
  end
47
+
48
+ def namespace(namespace)
49
+ @namespace = namespace
50
+ end
44
51
  end
45
52
 
46
53
  # @return [Hash] The resource's attributes
47
54
  attr_reader :attributes
48
55
  # @return [ZendeskAPI::Association] The association
49
56
  attr_accessor :association
50
-
57
+ # @return [Array] The last received errors
58
+ attr_accessor :errors
51
59
  # Place to dump the last response
52
60
  attr_accessor :response
53
61
 
@@ -111,11 +119,21 @@ module ZendeskAPI
111
119
 
112
120
  # Compares resources by class and id. If id is nil, then by object_id
113
121
  def ==(other)
114
- warn "Trying to compare #{other.class} to a Resource from #{caller.first}" if other && !other.is_a?(Data)
115
- other.is_a?(self.class) && ((other.id && other.id == id) || (other.object_id == self.object_id))
122
+ return true if other.object_id == self.object_id
123
+
124
+ if other && !(other.is_a?(Data) || other.is_a?(Integer))
125
+ warn "Trying to compare #{other.class} to a Resource from #{caller.first}"
126
+ end
127
+
128
+ if other.is_a?(Data)
129
+ other.id && other.id == id
130
+ elsif other.is_a?(Integer)
131
+ id == other
132
+ else
133
+ false
134
+ end
116
135
  end
117
136
  alias :eql :==
118
- alias :hash :id
119
137
 
120
138
  # @private
121
139
  def inspect
@@ -123,6 +141,16 @@ module ZendeskAPI
123
141
  end
124
142
 
125
143
  alias :to_param :attributes
144
+
145
+ private
146
+
147
+ def attributes_for_save
148
+ if self.class.unnested_params
149
+ attributes.changes
150
+ else
151
+ { self.class.singular_resource_name.to_sym => attributes.changes }
152
+ end
153
+ end
126
154
  end
127
155
 
128
156
  # Indexable resource