zendesk_support_api 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 35ce4e3e27845ca77e9d5436ce5cc053f0430a74ca9765ec708425ce19549f18
4
+ data.tar.gz: b0ac9059cbd6aa2ee599d91025d43f89e1b687dbb3f982ab1f32f7ec41f8105a
5
+ SHA512:
6
+ metadata.gz: 753387da38749fd018d1c70370ff0f48112b5b287b91339f59f042e9db8c2bd9106b4c481a3ae6d450d858ff3b07db2cc307c26953991fb9b25890ecaeb190b8
7
+ data.tar.gz: fdad54556055bc06e87c0a9bb5bcc62fbbcc73891ba26ad0b9716528cad3cfb754c7e3f6543be14af788137c292a7c7b7513836a033f8cfcab2d5290fd422e2d
@@ -0,0 +1,13 @@
1
+ image: ruby:2.6.1-alpine
2
+
3
+ pages:
4
+ script:
5
+ - gem install yard
6
+ - yard doc
7
+ - rm -rf public
8
+ - mv doc public
9
+ artifacts:
10
+ paths:
11
+ - public
12
+ only:
13
+ - master
@@ -0,0 +1,5 @@
1
+ lib/zendesk_support_api.rb fee89f019b11ec8feb844644e000cec6cad6a695
2
+ lib/zendesk_support_api/jobs.rb 21241d490bcb750ab702dc07e0d98b194ced2632
3
+ lib/zendesk_support_api/users.rb bac3de0bac0a04cbe8cd1d338357470ddbca3bb6
4
+ lib/zendesk_support_api/client.rb 157b67cde7b4e66ccdcbcda67d5a34bc9c3b8572
5
+ lib/zendesk_support_api/version.rb f929e8c6d528a2522ca83288be610db1c9064a10
File without changes
Binary file
Binary file
Binary file
@@ -0,0 +1,39 @@
1
+ # ZendeskSupportAPI
2
+
3
+
4
+ ## Documentation
5
+
6
+ Documentation (generated via yard) can be found [here](https://reyloc.gitlab.io/zendesk_support_api/)
7
+
8
+ ## Installation
9
+
10
+ The gem can be installed either via rubygems or bundler:
11
+
12
+ ### Rubygems
13
+
14
+ ```bash
15
+ gem install zendesk_support_api
16
+ ```
17
+
18
+ ### Bundler
19
+
20
+ ```ruby
21
+ gem "zendesk_support_api"
22
+ ```
23
+
24
+ The current requirements for the gem are:
25
+
26
+ * 'faraday', '~>1.0.0'
27
+ * 'oj', '~>3.10.6'
28
+
29
+ ## Configuration
30
+
31
+ Configuration is done through the creation of a `ZendeskSupportAPI::Client`
32
+ instance:
33
+
34
+ ```ruby
35
+ require 'zendesk_support_api'
36
+
37
+ client = ZendeskSupportAPI::Client.new('user@example.com', '123abc', 'https://zendesk.com/api/v2')
38
+ ```
39
+
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'oj'
5
+
6
+ # ZendeskSupportAPI - https://developer.zendesk.com/rest_api/docs/support/introduction
7
+ module ZendeskSupportAPI
8
+ require 'zendesk_support_api/client'
9
+ require 'zendesk_support_api/version'
10
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZendeskSupportAPI
4
+ # Client class - https://developer.zendesk.com/rest_api/docs/support/introduction#security-and-authentication
5
+ class Client
6
+ require 'zendesk_support_api/jobs'
7
+ require 'zendesk_support_api/users'
8
+
9
+ # Create a new instance of Client
10
+ #
11
+ # @param user [String] - The API username to use
12
+ # @param token [String] - The API token to use
13
+ # @param url [String] - The API URL to use
14
+ #
15
+ # @example
16
+ # ZendeskSupportAPI::Client.new('user', '123', 'zendesk.com/api')
17
+ # #=> #<ZendeskSupportAPI::Client:0x00007f88779cb330 @user="user",
18
+ # #=> @token="123", @url="zendesk.com/api">
19
+ def initialize(user, token, url)
20
+ @user = user
21
+ @token = token
22
+ @url = url
23
+ end
24
+
25
+ # Make a request to the Zendesk Support API
26
+ #
27
+ # @param http_method [Symbol] The HTTP method to utilize
28
+ # @param endpoint [String] The endpoint to hit
29
+ # @param params [Hash] Parameters for the request
30
+ # @return [Hash]
31
+ #
32
+ # @example
33
+ # client = ZendeskSupportAPI::Client.new('user', '123', 'zendesk.com/api')
34
+ # client.response(:get, 'users.json')
35
+ # #=> {users:[{user1},{user2}...{user100}]}
36
+
37
+ def request(http_method, endpoint, params = {})
38
+ response = client.public_send(http_method, endpoint, params)
39
+ Oj.load(response.body)
40
+ end
41
+
42
+ # Outputs a spinner symbol
43
+ #
44
+ # @param string [String] The string to output at the beginning
45
+ # @param num [Integer] The index of the iteration
46
+ # @return [String]
47
+ #
48
+ # @example
49
+ # ZendeskSupportAPI::Client.spinner('users', 1) #=> Grabbing users... \
50
+ # ZendeskSupportAPI::Client.spinner('groups', 3) #=> /
51
+
52
+ def spinner(string, num)
53
+ print "Grabbing #{string}... " if num.to_i == 1
54
+ symbols = ['-', '\\', '|', '/']
55
+ print symbols[num.to_i % 4]
56
+ print "\b"
57
+ end
58
+
59
+ # Handles responses that create jobs
60
+ #
61
+ # @param job [Hash] - The output from a request that created a job
62
+ # @return [Hash]
63
+
64
+ def handle_job(job)
65
+ print 'Checking job'
66
+ while job['job_status']['status'] != 'completed'
67
+ print '.'
68
+ job = ZendeskSupportAPI::Jobs.show(self, job['job_status']['id'])
69
+ end
70
+ puts 'completed'
71
+ job['job_status']['results']
72
+ end
73
+
74
+ private
75
+
76
+ # Creates a new Faraday instance
77
+ #
78
+ # @return [ZendeskSupportAPI::Client]
79
+
80
+ def client
81
+ @client ||= Faraday.new(@url) do |c|
82
+ c.request :url_encoded
83
+ c.adapter Faraday.default_adapter
84
+ c.basic_auth "#{@user}/token", @token
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZendeskSupportAPI
4
+ # Jobs class - https://developer.zendesk.com/rest_api/docs/support/job_statuses
5
+ class Jobs
6
+ # Make a request to show the job status
7
+ #
8
+ # @param client [ZendeskSupportAPI::Client] The client instance
9
+ # @param id [String] The id of the job to check
10
+ # @return [Hash]
11
+ #
12
+ # @example
13
+ # client = ZendeskSupportAPI::Client.new('user', '123', 'zendesk.com/api')
14
+ # ZendeskSupportAPI::Jobs.show(client, 'abc123')
15
+ # #=> {
16
+ # #=> "job_status"=> {
17
+ # #=> "id"=>"abc123",
18
+ # #=> "url"=>"abc123.json",
19
+ # #=> "total"=>2,
20
+ # #=> "progress"=>2,
21
+ # #=> "status"=>"completed",
22
+ # #=> "message"=>"Completed at 2020-04-29 13:26:40 +0000",
23
+ # #=> "results"=> [
24
+ # #=> {
25
+ # #=> "id"=>369731992959,
26
+ # #=> "status"=>"Updated",
27
+ # #=> "email"=>"test@example.com"
28
+ # #=> },
29
+ # #=> {
30
+ # #=> "id"=>369728778639,
31
+ # #=> "status"=>"Updated",
32
+ # #=> "email"=>"test2@example.com"
33
+ # #=> }
34
+ # #=> ]
35
+ # #=> }
36
+ # #=> }
37
+
38
+ def self.show(client, id)
39
+ client.request(:get, "job_statuses/#{id}.json")
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,297 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZendeskSupportAPI
4
+ # Users class - https://developer.zendesk.com/rest_api/docs/support/users
5
+ class Users
6
+ # Function to return a string that side-loads organizations
7
+ #
8
+ # @return [String]
9
+
10
+ def self.included
11
+ 'include=organizations'
12
+ end
13
+
14
+ # Maps users into user_objects
15
+ #
16
+ # @param users [Array] The Array of users to map
17
+ # @param orgs [Array] The Array of orgs to use in mapping
18
+ # @return [Hash]
19
+
20
+ def self.user_map(users, orgs)
21
+ users.map { |u| user_object(u, orgs) }
22
+ end
23
+
24
+ # Returns the string of the next_page for pagination
25
+ #
26
+ # @param res [Hash] The Hash containing the response from a request
27
+ # @return [nil|String]
28
+ #
29
+ # @example
30
+ # ZendeskSupportAPI::Users.next_page(response) #=> nil
31
+ # ZendeskSupportAPI::Users.next_page(response)
32
+ # #=> "users.json?include=organizations&page=56
33
+
34
+ def self.next_page(res)
35
+ (res['next_page'].nil? ? nil : res['next_page'].split('/').last)
36
+ end
37
+
38
+ # Lists out users (first 100)
39
+ #
40
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
41
+ # @return [Hash]
42
+ #
43
+ # @example
44
+ # ZendeskSupportAPI::Users.list(client)
45
+ # #=> {users:[{user1},{user2}...{user100}]}
46
+
47
+ def self.list(client)
48
+ res = client.request(:get, "users.json?#{included}")
49
+ res['users'].map { |u| user_object(u, res['organizations']) }
50
+ res['users']
51
+ end
52
+
53
+ # Lists out all users (paginates over every page)
54
+ #
55
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
56
+ # @return [Hash]
57
+ #
58
+ # @example
59
+ # ZendeskSupportAPI::Users.all(client)
60
+ # #=> Grabbing users... / ...done
61
+ # #=> {users:[{user1},{user2}...{user201520}]}
62
+
63
+ def self.all(client)
64
+ users = []
65
+ page = "users.json?#{included}&page=1"
66
+ until page.nil?
67
+ res = client.request(:get, page)
68
+ client.spinner("users (total: #{res['count']})", page.split('=').last)
69
+ users += user_map(res['users'], res['organizations'])
70
+ page = next_page(res)
71
+ end
72
+ puts ' ...done'
73
+ users
74
+ end
75
+
76
+ # Grabs a specific user
77
+ #
78
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
79
+ # @param id [Integer] The User's ID to use
80
+ # @return [Hash]
81
+ #
82
+ # @example
83
+ # ZendeskSupportAPI::Users.show(client, 123)
84
+ # #=> {
85
+ # #=> "id"=>123,
86
+ # #=> "url"=>"https://zendesk.com/api/users/123.json",
87
+ # #=> "name"=>"Test User",
88
+ # #=> "email"=>"test@example.com",
89
+ # #=> "created_at"=>"2020-04-28T13:45:54Z",
90
+ # #=> "updated_at"=>"2020-04-28T13:45:54Z",
91
+ # #=> "time_zone"=>"Eastern Time (US & Canada)",
92
+ # #=> "iana_time_zone"=>"America/New_York",
93
+ # #=> "phone"=>nil,
94
+ # #=> "shared_phone_number"=>nil,
95
+ # #=> "photo"=>nil,
96
+ # #=> "locale_id"=>1,
97
+ # #=> "locale"=>"en-US",
98
+ # #=> "organization_id"=>nil,
99
+ # #=> "role"=>"end-user",
100
+ # #=> "verified"=>false,
101
+ # #=> "external_id"=>nil,
102
+ # #=> "tags"=>[],
103
+ # #=> "alias"=>nil,
104
+ # #=> "active"=>true,
105
+ # #=> "shared"=>false,
106
+ # #=> "shared_agent"=>false,
107
+ # #=> "last_login_at"=>nil,
108
+ # #=> "two_factor_auth_enabled"=>false,
109
+ # #=> "signature"=>nil,
110
+ # #=> "details"=>nil,
111
+ # #=> "notes"=>nil,
112
+ # #=> "role_type"=>nil,
113
+ # #=> "custom_role_id"=>nil,
114
+ # #=> "moderator"=>false,
115
+ # #=> "ticket_restriction"=>"requested",
116
+ # #=> "only_private_comments"=>false,
117
+ # #=> "restricted_agent"=>true,
118
+ # #=> "suspended"=>false,
119
+ # #=> "chat_only"=>false,
120
+ # #=> "default_group_id"=>nil,
121
+ # #=> "report_csv"=>false,
122
+ # #=> "organization"=>[]}
123
+
124
+ def self.show(client, id)
125
+ res = client.request(:get, "users/#{id}.json?#{included}")
126
+ user_object(res['user'], res['organizations'])
127
+ end
128
+
129
+ # Shows many users
130
+ #
131
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
132
+ # @param ids [Array] An array of User IDs to use
133
+ # @return [Array]
134
+ #
135
+ # @example
136
+ # ZendeskSupportAPI::Users.show_many(client, [123, 456, 789])
137
+ # #=> [{user123},{user456},{user789}]
138
+
139
+ def self.show_many(client, ids)
140
+ ids = ids.join(',')
141
+ res = client.request(:get, "users/show_many.json?#{included}&ids=#{ids}")
142
+ res['users'].map { |u| user_object(u, res['organizations']) }
143
+ res['users']
144
+ end
145
+
146
+ # Creates a user hash (for mapping the org into the user Hash)
147
+ #
148
+ # @param user [Hash] The user details to use
149
+ # @param orgs [Array] The Array of orgs to use
150
+ # @return [Hash]
151
+
152
+ def self.user_object(user, orgs)
153
+ oid = 'organization_id'
154
+ user['organization'] = orgs.select { |o| o['id'] == user[oid] }
155
+ user
156
+ end
157
+
158
+ # Creates a user
159
+ #
160
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
161
+ # @param user [Hash] The user to create
162
+ # @return [Hash|String] Either the user details or the error
163
+ #
164
+ # @example
165
+ # ZendeskSupportAPI::User.create(client, user)
166
+ # #=> {user}
167
+ # ZendeskSupportAPI::User.create(client, user)
168
+ # #=> Creation failed: => "Creation failed: {\"email\"=>[{\"description\"
169
+ # #=> =>\"Email: test@example.com is already being used by another user\",
170
+ # #=> \"error\"=>\"DuplicateValue\"}]}"
171
+
172
+ def self.create(client, user)
173
+ res = client.request(:post, 'users.json', user: user)
174
+ return "Creation failed: #{res['details']}" if res['error']
175
+
176
+ res
177
+ end
178
+
179
+ # Creates many users
180
+ #
181
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
182
+ # @param users [Array] The users to create
183
+ # @return [ZendeskSupportAPI::Client.handle_job] Either the user details or the error
184
+
185
+ def self.create_many(client, users)
186
+ res = client.request(:post, 'users/create_many.json', users: users)
187
+ client.handle_job(res)
188
+ end
189
+
190
+ # Creates or updates a user
191
+ #
192
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
193
+ # @param user [Hash] The user to create/update
194
+ # @return [Hash|String] Either the user details or the error
195
+ #
196
+ # @example
197
+ # ZendeskSupportAPI::User.create_or_update(client, user)
198
+ # #=> {user}
199
+
200
+ def self.create_or_update(client, user)
201
+ res = client.request(:post, 'users/create_or_update.json', user: user)
202
+ return "Create/Update failed: #{res['description']}" if res['error']
203
+
204
+ res
205
+ end
206
+
207
+ # Creates or updates many users
208
+ #
209
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
210
+ # @param users [Array] The users to create/update
211
+ # @return [ZendeskSupportAPI::Client.handle_job] Either the user details or the error
212
+
213
+ def self.create_or_update_many(client, users)
214
+ res = client.request(:post,
215
+ 'users/create_or_update_many.json',
216
+ users: users)
217
+ client.handle_job(res)
218
+ end
219
+
220
+ # Updates a user
221
+ #
222
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
223
+ # @param uid [Integer] The User's ID
224
+ # @param user [Hash] The user details to update
225
+ # @return [Hash|String] Either the user details or the error
226
+ #
227
+ # @example
228
+ # ZendeskSupportAPI::User.update(client, 123, user)
229
+ # #=> {user}
230
+
231
+ def self.update(client, uid, user)
232
+ res = client.request(:put, "users/#{uid}.json", user: user)
233
+ return "Update of #{uid} failed: #{res['error']}" if res['error']
234
+
235
+ res
236
+ end
237
+
238
+ # Updates many users
239
+ #
240
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
241
+ # @param users [Array] The users to update
242
+ # @return [ZendeskSupportAPI::Client.handle_job] Either the user details or the error
243
+
244
+ def self.update_many(client, users)
245
+ res = client.request(:put, 'users/update_many.json', users: users)
246
+ client.handle_job(res)
247
+ end
248
+
249
+ # Deletes a user
250
+ #
251
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
252
+ # @param uid [Integer] The User ID to delete
253
+ # @return [String]
254
+ #
255
+ # @example
256
+ # ZendeskSupportAPI::Users.delete(client, 123)
257
+ # #=> User 123 has been deleted
258
+ # ZendeskSupportAPI::Users.delete(client, 123)
259
+ # #=> "Deletion of 123 failed: RecordNotFound"
260
+
261
+ def self.delete(client, uid)
262
+ res = client.request(:delete, "users/#{uid}.json")
263
+ return "Deletion of #{uid} failed: #{res['error']}" if res['error']
264
+
265
+ "User #{uid} has been deleted"
266
+ end
267
+
268
+ # Deletes many users
269
+ #
270
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
271
+ # @param ids [Array] The array of User IDs to delete
272
+ # @return [ZendeskSupportAPI::Client.handle_job] Either the user details or the error
273
+
274
+ def self.delete_many(client, ids)
275
+ ids = ids.join(',')
276
+ res = client.request(:delete, "users/destroy_many.json?ids=#{ids}")
277
+ client.handle_job(res)
278
+ end
279
+
280
+ # Suspends a user
281
+ #
282
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
283
+ # @param id [Integer] The User ID to suspend
284
+ # @return [String] Either a success message or an error
285
+ #
286
+ # @example
287
+ # ZendeskSupportAPI::Users.suspend(client, 123)
288
+ # #=> User 123 is suspended
289
+
290
+ def self.suspend(client, id)
291
+ res = client.request(:put, "users/#{id}.json", user: { suspended: true })
292
+ return "Suspension of #{id} failed: #{res['error']}" if res['error']
293
+
294
+ "User #{id} suspended" if res['user']['suspended']
295
+ end
296
+ end
297
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZendeskSupportAPI
4
+ # Outputs the gem version
5
+
6
+ VERSION = '0.1.0'
7
+ end
@@ -0,0 +1,21 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'zendesk_support_api/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'zendesk_support_api'
7
+ s.version = ZendeskSupportAPI::VERSION
8
+ s.date = '2020-04-28'
9
+ s.summary = 'Zendesk Support API wrapper gem'
10
+ s.description = 'TBD'
11
+ s.authors = ['Jason Colyer']
12
+ s.email = 'jcolyer2007@gmail.com'
13
+ s.homepage = 'https://gitlab.com/reyloc/zendesk_support_api'
14
+ s.files = Dir.chdir(File.expand_path('..', __FILE__)) do
15
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r(^(test|spec|features)/)) }
16
+ end
17
+ # s.homepage = 'TBD'
18
+ s.license = 'MIT'
19
+ s.add_dependency 'faraday', '~>1.0.0'
20
+ s.add_dependency 'oj', '~>3.10.6'
21
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zendesk_support_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Colyer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: oj
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.10.6
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.10.6
41
+ description: TBD
42
+ email: jcolyer2007@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitlab-ci.yml"
48
+ - ".yardoc/checksums"
49
+ - ".yardoc/complete"
50
+ - ".yardoc/object_types"
51
+ - ".yardoc/objects/root.dat"
52
+ - ".yardoc/proxy_types"
53
+ - README.md
54
+ - lib/zendesk_support_api.rb
55
+ - lib/zendesk_support_api/client.rb
56
+ - lib/zendesk_support_api/jobs.rb
57
+ - lib/zendesk_support_api/users.rb
58
+ - lib/zendesk_support_api/version.rb
59
+ - zendesk_support_api.gemspec
60
+ homepage: https://gitlab.com/reyloc/zendesk_support_api
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 3.0.3
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Zendesk Support API wrapper gem
83
+ test_files: []