teamsupport 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,134 @@
1
+ require 'teamsupport/error'
2
+ require 'teamsupport/ticket'
3
+ require 'teamsupport/rest/utils'
4
+ require 'teamsupport/utils'
5
+
6
+ module Teamsupport
7
+ module REST
8
+ module Tickets
9
+ include Teamsupport::REST::Utils
10
+ include Teamsupport::Utils
11
+
12
+ # Returns all available tickets for the TeamSupport organization
13
+ #
14
+ # @see http://help.teamsupport.com/1/en/topic/api
15
+ #
16
+ # @example
17
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
18
+ # teamsupport_api.tickets()
19
+ #
20
+ # @authentication Requires Basic Authentication
21
+ #
22
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied API credentials are not valid.
23
+ #
24
+ # @return [Array<Teamsupport::Ticket>]
25
+ #
26
+ # @param options [Hash] A customizable set of options.
27
+ # @option options [Integer] :count Specifies the number of records to retrieve.
28
+ #
29
+ # @api public
30
+ def tickets(options = {})
31
+ perform_get_with_objects_from_collection('/api/json/tickets.json', options, Teamsupport::Ticket, :Tickets)
32
+ end
33
+
34
+ # Returns a customer
35
+ #
36
+ # @example
37
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
38
+ # teamsupport_api.ticket('1')
39
+ #
40
+ # @authentication Requires Basic Authentication
41
+ #
42
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied user credentials are not valid.
43
+ #
44
+ # @return [Teamsupport::Ticket] The requested ticket.
45
+ #
46
+ # @param id [Integer] A ticket ID.
47
+ # @param options [Hash] A customizable set of options.
48
+ #
49
+ # @api public
50
+ def ticket(id, options = {})
51
+ perform_get_with_object_from_collection("/api/json/tickets/#{id}.json", options, Teamsupport::Ticket, :Ticket)
52
+ end
53
+
54
+ # Create a ticket
55
+ #
56
+ # @example
57
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
58
+ # teamsupport_api.create_ticket(Name: 'New Ticket', Description: 'Ticket Description')
59
+ #
60
+ # @authentication Requires Basic Authentication
61
+ #
62
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied user credentials are not valid.
63
+ #
64
+ # @return [Teamsupport::Ticket] The new ticket.
65
+ #
66
+ # @param options [Hash] A customizable set of options.
67
+ #
68
+ # @api public
69
+ def create_ticket(options = {})
70
+ perform_post_with_object_from_collection('/api/json/tickets.json', options, Teamsupport::Ticket, :Ticket)
71
+ end
72
+
73
+ # Updates the ticket
74
+ #
75
+ # @example
76
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
77
+ # teamsupport_api.update_ticket('1', Name: 'Updated Ticket Name')
78
+ #
79
+ # @authentication Requires Basic Authentication
80
+ #
81
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied user credentials are not valid.
82
+ #
83
+ # @return [Teamsupport::Ticket] The updated ticket.
84
+ #
85
+ # @param id [Integer] A ticket ID.
86
+ # @param options [Hash] A customizable set of options.
87
+ #
88
+ # @api public
89
+ def update_ticket(id, options = {})
90
+ ticket_hash = ticket(id).to_h
91
+ perform_put_with_object_from_collection("/api/json/tickets/#{id}.json", ticket_hash.merge(options), Teamsupport::Ticket, :Ticket)
92
+ end
93
+
94
+ # Deletes the ticket
95
+ #
96
+ # @example
97
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
98
+ # teamsupport_api.delete_ticket('1')
99
+ #
100
+ # @authentication Requires Basic Authentication
101
+ #
102
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied user credentials are not valid.
103
+ #
104
+ # @param id [Integer] A ticket ID.
105
+ # @param options [Hash] A customizable set of options.
106
+ #
107
+ # @api public
108
+ def delete_ticket(id, options = {})
109
+ perform_delete("/api/json/tickets/#{id}.json", options)
110
+ end
111
+
112
+ # Returns the actions for a ticket
113
+ #
114
+ # @example
115
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
116
+ # teamsupport_api.ticket_actions('1')
117
+ #
118
+ # @authentication Requires Basic Authentication
119
+ #
120
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied user credentials are not valid.
121
+ #
122
+ # @return [Array<Teamsupport::Product>]
123
+ #
124
+ # @param id [Integer] A ticket ID.
125
+ # @param options [Hash] A customizable set of options.
126
+ #
127
+ # @api public
128
+ def ticket_actions(id, options = {})
129
+ options = options.dup
130
+ perform_get_with_objects_from_collection("/api/json/tickets/#{id}/actions.json", options, Teamsupport::TicketAction, :Actions)
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,325 @@
1
+ require 'addressable/uri'
2
+ require 'teamsupport/arguments'
3
+ require 'teamsupport/rest/request'
4
+ require 'teamsupport/utils'
5
+ require 'uri'
6
+
7
+ module Teamsupport
8
+ module REST
9
+ module Utils
10
+ include Teamsupport::Utils
11
+
12
+ private
13
+
14
+ # Take a URI string or Teamsupport::Identity object and return its ID
15
+ #
16
+ # @param object [Integer, String, URI, Teamsupport::Identity] An ID, URI, or object.
17
+ #
18
+ # @return [Integer]
19
+ #
20
+ # @api private
21
+ def extract_id(object)
22
+ case object
23
+ when ::Integer
24
+ object
25
+ when ::String
26
+ object.split('/').last.to_i
27
+ when URI, Addressable::URI
28
+ object.path.split('/').last.to_i
29
+ when Teamsupport::Identity
30
+ object.ID
31
+ end
32
+ end
33
+
34
+ # Alias for performing HTTP GET request
35
+ #
36
+ # @param path [String]
37
+ # @param options [Hash]
38
+ #
39
+ # @return [Array, Hash]
40
+ #
41
+ # @api private
42
+ def perform_get(path, options = {})
43
+ perform_request(:get, path, options)
44
+ end
45
+
46
+ # Alias for performing HTTP POST request
47
+ #
48
+ # @param path [String]
49
+ # @param options [Hash]
50
+ #
51
+ # @return [Array, Hash]
52
+ #
53
+ # @api private
54
+ def perform_post(path, options = {})
55
+ perform_request(:post, path, options)
56
+ end
57
+
58
+ # Alias for performing HTTP PUT request
59
+ #
60
+ # @param path [String]
61
+ # @param options [Hash]
62
+ #
63
+ # @return [Array, Hash]
64
+ #
65
+ # @api private
66
+ def perform_put(path, options = {})
67
+ perform_request(:put, path, options)
68
+ end
69
+
70
+ # Alias for performing HTTP DELETE request
71
+ #
72
+ # @param path [String]
73
+ # @param options [Hash]
74
+ #
75
+ # @return [Array, Hash]
76
+ #
77
+ # @api private
78
+ def perform_delete(path, options = {})
79
+ perform_request(:delete, path, options)
80
+ end
81
+
82
+ # Create a new Teamsupport::REST::Request and perform an HTTP request
83
+ #
84
+ # @param request_method [Symbol]
85
+ # @param path [String]
86
+ # @param options [Hash]
87
+ #
88
+ # @return [Array, Hash]
89
+ #
90
+ # @api private
91
+ def perform_request(request_method, path, options = {})
92
+ Teamsupport::REST::Request.new(self, request_method, path, options).perform
93
+ end
94
+
95
+ # Alias for performing HTTP GET request returning an object
96
+ #
97
+ # @param path [String]
98
+ # @param options [Hash]
99
+ # @param klass [Class]
100
+ #
101
+ # @return [Object]
102
+ #
103
+ # @api private
104
+ def perform_get_with_object(path, options, klass)
105
+ perform_request_with_object(:get, path, options, klass)
106
+ end
107
+
108
+ # Alias for performing HTTP POST request returning an object
109
+ #
110
+ # @param path [String]
111
+ # @param options [Hash]
112
+ # @param klass [Class]
113
+ #
114
+ # @return [Object]
115
+ #
116
+ # @api private
117
+ def perform_post_with_object(path, options, klass)
118
+ perform_request_with_object(:post, path, options, klass)
119
+ end
120
+
121
+ # Alias for performing HTTP PUT request returning an object
122
+ #
123
+ # @param path [String]
124
+ # @param options [Hash]
125
+ # @param klass [Class]
126
+ #
127
+ # @return [Object]
128
+ #
129
+ # @api private
130
+ def perform_put_with_object(path, options, klass)
131
+ perform_request_with_object(:put, path, options, klass)
132
+ end
133
+
134
+ # Alias for performing HTTP requests returning an object
135
+ #
136
+ # @param path [String]
137
+ # @param options [Hash]
138
+ # @param klass [Class]
139
+ #
140
+ # @return [Object]
141
+ #
142
+ # @api private
143
+ def perform_request_with_object(request_method, path, options, klass)
144
+ response = perform_request(request_method, path, options)
145
+ klass.new(response)
146
+ end
147
+
148
+ # Alias for performing HTTP GET request returning an object from a collection
149
+ #
150
+ # @param path [String]
151
+ # @param options [Hash]
152
+ # @param klass [Class]
153
+ # @param collection_name [Symbol]
154
+ #
155
+ # @return [Object]
156
+ #
157
+ # @api private
158
+ def perform_get_with_object_from_collection(path, options, klass, collection_name)
159
+ perform_request_with_object_from_collection(:get, path, options, klass, collection_name)
160
+ end
161
+
162
+ # Alias for performing HTTP POST request returning an object from a collection
163
+ #
164
+ # @param path [String]
165
+ # @param options [Hash]
166
+ # @param klass [Class]
167
+ # @param collection_name [Symbol]
168
+ #
169
+ # @return [Object]
170
+ #
171
+ # @api private
172
+ def perform_post_with_object_from_collection(path, options, klass, collection_name)
173
+ perform_request_with_object_from_collection(:post, path, options, klass, collection_name)
174
+ end
175
+
176
+ # Alias for performing HTTP PUT request returning an object from a collection
177
+ #
178
+ # @param path [String]
179
+ # @param options [Hash]
180
+ # @param klass [Class]
181
+ # @param collection_name [Symbol]
182
+ #
183
+ # @return [Object]
184
+ #
185
+ # @api private
186
+ def perform_put_with_object_from_collection(path, options, klass, collection_name)
187
+ perform_request_with_object_from_collection(:put, path, options, klass, collection_name)
188
+ end
189
+
190
+ # Alias for performing HTTP requests returning an object from a collection
191
+ #
192
+ # @param request_method [Symbol]
193
+ # @param path [String]
194
+ # @param options [Hash]
195
+ # @param klass [Class]
196
+ # @param collection_name [Symbol]
197
+ #
198
+ # @return [Object]
199
+ #
200
+ # @api private
201
+ def perform_request_with_object_from_collection(request_method, path, options, klass, collection_name) # rubocop:disable Metrics/ParameterLists
202
+ options = {collection_name.to_sym => options} unless options.empty?
203
+
204
+ response = perform_request(request_method, path, options).fetch(collection_name.to_sym)
205
+ klass.new(response)
206
+ end
207
+
208
+ # Alias for performing HTTP GET request returning multiple objects
209
+ #
210
+ # @param path [String]
211
+ # @param options [Hash]
212
+ # @param klass [Class]
213
+ #
214
+ # @return [Array]
215
+ #
216
+ # @api private
217
+ def perform_get_with_objects(path, options, klass)
218
+ perform_request_with_objects(:get, path, options, klass)
219
+ end
220
+
221
+ # Alias for performing HTTP POST request returning multiple objects
222
+ #
223
+ # @param path [String]
224
+ # @param options [Hash]
225
+ # @param klass [Class]
226
+ #
227
+ # @return [Array]
228
+ #
229
+ # @api private
230
+ def perform_post_with_objects(path, options, klass)
231
+ perform_request_with_objects(:post, path, options, klass)
232
+ end
233
+
234
+ # Alias for performing HTTP PUT request returning multiple objects
235
+ #
236
+ # @param path [String]
237
+ # @param options [Hash]
238
+ # @param klass [Class]
239
+ #
240
+ # @return [Array]
241
+ #
242
+ # @api private
243
+ def perform_put_with_objects(path, options, klass)
244
+ perform_request_with_objects(:put, path, options, klass)
245
+ end
246
+
247
+ # Alias for performing HTTP requests returning multiple objects
248
+ #
249
+ # @param request_method [Symbol]
250
+ # @param path [String]
251
+ # @param options [Hash]
252
+ # @param klass [Class]
253
+ #
254
+ # @return [Array]
255
+ #
256
+ # @api private
257
+ def perform_request_with_objects(request_method, path, options, klass)
258
+ perform_request(request_method, path, options).collect do |element|
259
+ klass.new(element)
260
+ end
261
+ end
262
+
263
+ # Alias for performing HTTP GET request returning multiple objects in a collection
264
+ #
265
+ # @param path [String]
266
+ # @param options [Hash]
267
+ # @param klass [Class]
268
+ # @param collection_name [Symbol]
269
+ #
270
+ # @return [Array]
271
+ #
272
+ # @api private
273
+ def perform_get_with_objects_from_collection(path, options, klass, collection_name)
274
+ perform_request_with_objects_from_collection(:get, path, options, klass, collection_name)
275
+ end
276
+
277
+ # Alias for performing HTTP POST request returning multiple objects in a collection
278
+ #
279
+ # @param path [String]
280
+ # @param options [Hash]
281
+ # @param klass [Class]
282
+ # @param collection_name [Symbol]
283
+ #
284
+ # @return [Array]
285
+ #
286
+ # @api private
287
+ def perform_post_with_objects_from_collection(path, options, klass, collection_name)
288
+ perform_request_with_objects_from_collection(:post, path, options, klass, collection_name)
289
+ end
290
+
291
+ # Alias for performing HTTP PUT request returning multiple objects in a collection
292
+ #
293
+ # @param path [String]
294
+ # @param options [Hash]
295
+ # @param klass [Class]
296
+ # @param collection_name [Symbol]
297
+ #
298
+ # @return [Array]
299
+ #
300
+ # @api private
301
+ def perform_put_with_objects_from_collection(path, options, klass, collection_name)
302
+ perform_request_with_objects_from_collection(:put, path, options, klass, collection_name)
303
+ end
304
+
305
+ # Alias for performing HTTP requests returning multiple objects in a collection
306
+ #
307
+ # @param request_method [Symbol]
308
+ # @param path [String]
309
+ # @param options [Hash]
310
+ # @param klass [Class]
311
+ # @param collection_name [Symbol]
312
+ #
313
+ # @return [Array]
314
+ #
315
+ # @api private
316
+ def perform_request_with_objects_from_collection(request_method, path, options, klass, collection_name) # rubocop:disable Metrics/ParameterLists
317
+ options = {collection_name.to_sym => options} unless options.empty?
318
+
319
+ perform_request(request_method, path, options).fetch(collection_name.to_sym).collect do |element|
320
+ klass.new(element)
321
+ end
322
+ end
323
+ end
324
+ end
325
+ end