zendesk_support_api 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c78cf01ac68e20c32f2e1d3ac3d47a9b4574012200718e33d06f3f92d75d9a68
4
+ data.tar.gz: f066e3f7c5fbfd5008a8a2d8b3fff4ba416c8d9e5a3373a7f8b8d586126c5fb9
5
+ SHA512:
6
+ metadata.gz: f55d3a02c043022f70047bfe080d1227a9f1ea4dd9355f6e670ddb33187fd7bd6b79fbf207a99c671616ab0f370e56d4f9b8a510b966d50947a7c6e9ccf5c46e
7
+ data.tar.gz: 4eeb5abd58621fc511552e686859e68e95cbec5b8cca8363b93c1b9a57cdb2e1f01860b21158351a5290e44b3c929307b231bfd3f65917d7e8fc3a6451211492
@@ -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,8 @@
1
+ lib/zendesk_support_api.rb fee89f019b11ec8feb844644e000cec6cad6a695
2
+ lib/zendesk_support_api/jobs.rb 15e3db660bb16f1a2bc580ce17a9368f81fc9d0e
3
+ lib/zendesk_support_api/users.rb eb4f8d81f6f9ccf779679f7bfe354c0cc6be8d0c
4
+ lib/zendesk_support_api/client.rb 19147ca149a521a068e8a8818ad9b26091df0542
5
+ lib/zendesk_support_api/groups.rb bcc2a0b1af5bf3552c7838d80f339efbf8636e53
6
+ lib/zendesk_support_api/search.rb e741717c61809c578afbcd44f9b8e8f18a8c0363
7
+ lib/zendesk_support_api/version.rb b95930e804ad2f9d4718efe8b3c740f79304913f
8
+ lib/zendesk_support_api/organizations.rb 684c74525f26697a7a6e5fca921a1a937b0d20c8
File without changes
Binary file
Binary file
Binary file
@@ -0,0 +1,73 @@
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
+
40
+ ## Usage
41
+
42
+ Soon to come
43
+
44
+ ## Changelog
45
+
46
+ * v0.1.0 - Initial creation of gem
47
+ * v0.2.0 - Added Search functions, Fixed some yard syntax/formatting, Changed version number
48
+ * v0.3.0 - Changed version number, Added Group functions, Added User.groups function
49
+ * v0.4.0 - Added Organization functions, Changed version number
50
+
51
+ ## Upcoming changes
52
+
53
+ * v0.5.0 - Requests
54
+ * v0.6.0 - Tickets
55
+ * v0.7.0 - Attachments?
56
+ * v0.8.0 - Satisfaction
57
+ * v0.9.0 - Sessions
58
+ * v0.10.0 - Tags
59
+ * v0.11.0 - Views
60
+ * v0.12.0 - Triggers
61
+ * v0.13.0 - Automations
62
+ * v0.14.0 - SLA
63
+ * v0.15.0 - Targets
64
+ * v0.16.0 - Macros
65
+ * v0.17.0 - Brands
66
+ * v0.18.0 - Locales
67
+ * v0.19.0 - Schedules
68
+ * v0.20.0 - Ticket Forms
69
+ * v0.21.0 - Ticket Fields
70
+ * v0.22.0 - User Fields
71
+ * v0.23.0 - Organization Fields
72
+ * v0.24.0 - Account Settings
73
+ * v1.0.0 - Usage details, any polishing needed
@@ -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,91 @@
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/groups'
7
+ require 'zendesk_support_api/jobs'
8
+ require 'zendesk_support_api/organizations'
9
+ require 'zendesk_support_api/search'
10
+ require 'zendesk_support_api/users'
11
+
12
+ # Create a new instance of Client
13
+ #
14
+ # @param user [String] - The API username to use
15
+ # @param token [String] - The API token to use
16
+ # @param url [String] - The API URL to use
17
+ #
18
+ # @example
19
+ # ZendeskSupportAPI::Client.new('user', '123', 'zendesk.com/api')
20
+ # #=> #<ZendeskSupportAPI::Client:0x00007f88779cb330 @user="user",
21
+ # #=> @token="123", @url="zendesk.com/api">
22
+ def initialize(user, token, url)
23
+ @user = user
24
+ @token = token
25
+ @url = url
26
+ end
27
+
28
+ # Make a request to the Zendesk Support API
29
+ #
30
+ # @param http_method [Symbol] The HTTP method to utilize
31
+ # @param endpoint [String] The endpoint to hit
32
+ # @param params [Hash] Parameters for the request
33
+ # @return [Hash]
34
+ #
35
+ # @example
36
+ # client = ZendeskSupportAPI::Client.new('user', '123', 'zendesk.com/api')
37
+ # client.response(:get, 'users.json')
38
+ # #=> {users:[{user1},{user2}...{user100}]}
39
+
40
+ def request(http_method, endpoint, params = {})
41
+ response = client.public_send(http_method, endpoint, params)
42
+ Oj.load(response.body)
43
+ end
44
+
45
+ # Outputs a spinner symbol
46
+ #
47
+ # @param string [String] The string to output at the beginning
48
+ # @param num [Integer] The index of the iteration
49
+ # @return [String]
50
+ #
51
+ # @example
52
+ # ZendeskSupportAPI::Client.spinner('users', 1) #=> Grabbing users... \
53
+ # ZendeskSupportAPI::Client.spinner('groups', 3) #=> /
54
+
55
+ def spinner(string, num)
56
+ print "Grabbing #{string}... " if num.to_i == 1
57
+ symbols = ['-', '\\', '|', '/']
58
+ print symbols[num.to_i % 4]
59
+ print "\b"
60
+ end
61
+
62
+ # Handles responses that create jobs
63
+ #
64
+ # @param job [Hash] - The output from a request that created a job
65
+ # @return [Hash]
66
+
67
+ def handle_job(job)
68
+ print 'Checking job'
69
+ while job['job_status']['status'] != 'completed'
70
+ print '.'
71
+ job = ZendeskSupportAPI::Jobs.show(self, job['job_status']['id'])
72
+ end
73
+ puts 'completed'
74
+ job['job_status']['results']
75
+ end
76
+
77
+ private
78
+
79
+ # Creates a new Faraday instance
80
+ #
81
+ # @return [ZendeskSupportAPI::Client]
82
+
83
+ def client
84
+ @client ||= Faraday.new(@url) do |c|
85
+ c.request :url_encoded
86
+ c.adapter Faraday.default_adapter
87
+ c.basic_auth "#{@user}/token", @token
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,178 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZendeskSupportAPI
4
+ # Groups class - https://developer.zendesk.com/rest_api/docs/support/groups
5
+ class Groups
6
+ # Function to return a string that side-loads users
7
+ #
8
+ # @return [String]
9
+
10
+ def self.user_map(groups, users)
11
+ groups.map { |g| group_object(g, users) }
12
+ end
13
+
14
+ # Creates a group hash (for mappping the user into the group Hash)
15
+ #
16
+ # @param group [Hash] The group details to use
17
+ # @param users [Array] The Array of users to use
18
+ # @return [Hash]
19
+
20
+ def self.group_object(group, users)
21
+ group['user'] = users.select { |u| u['id'] == group['user_id'] }
22
+ group
23
+ end
24
+
25
+ # Returns the string of the next_page for pagination
26
+ #
27
+ # @param res [Hash] The Hash containing the response from a request
28
+ # @return [nil|String]
29
+ #
30
+ # @example
31
+ # ZendeskSupportAPI::Users.next_page(response) #=> nil
32
+ # ZendeskSupportAPI::Users.next_page(response)
33
+ # #=> "memberships.json?include=users&page=3
34
+
35
+ def self.next_page(res)
36
+ (res['next_page'].nil? ? nil : res['next_page'].split('/').last)
37
+ end
38
+
39
+ # Lists groups (first 100)
40
+ #
41
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
42
+ # @return [Array]
43
+ #
44
+ # @example
45
+ # ZendeskSupportAPI::Groups.list(client)
46
+ # #=> [
47
+ # #=> {
48
+ # #=> "name": "DJs",
49
+ # #=> "created_at": "2009-05-13T00:07:08Z",
50
+ # #=> "updated_at": "2011-07-22T00:11:12Z",
51
+ # #=> "id": 211
52
+ # #=> },
53
+ # #=> {
54
+ # #=> "name": "MCs",
55
+ # #=> "created_at": "2009-08-26T00:07:08Z",
56
+ # #=> "updated_at": "2010-05-13T00:07:08Z",
57
+ # #=> "id": 122
58
+ # #=> }
59
+ # #=> ]
60
+
61
+ def self.list(client)
62
+ client.request(:get, 'groups.json')['groups']
63
+ end
64
+
65
+ # Shows assignable groups (first 100)
66
+ #
67
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
68
+ # @return [Array]
69
+ #
70
+ # @example
71
+ # ZendeskSupportAPI::Groups.assignable(client)
72
+ # #=> [
73
+ # #=> {
74
+ # #=> "name": "DJs",
75
+ # #=> "created_at": "2009-05-13T00:07:08Z",
76
+ # #=> "updated_at": "2011-07-22T00:11:12Z",
77
+ # #=> "id": 211
78
+ # #=> },
79
+ # #=> {
80
+ # #=> "name": "MCs",
81
+ # #=> "created_at": "2009-08-26T00:07:08Z",
82
+ # #=> "updated_at": "2010-05-13T00:07:08Z",
83
+ # #=> "id": 122
84
+ # #=> }
85
+ # #=> ]
86
+
87
+ def self.assignable(client)
88
+ client.request(:get, 'assignable.json')['groups']
89
+ end
90
+
91
+ # Shows info about a specific group
92
+ #
93
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
94
+ # @param gid [Integer] The Group ID
95
+ # @return [Hash]
96
+ #
97
+ # @example
98
+ # ZendeskSupportAPI::Groups.show(client, 122)
99
+ # #=> {
100
+ # #=> "name": "MCs",
101
+ # #=> "created_at": "2009-08-26T00:07:08Z",
102
+ # #=> "updated_at": "2010-05-13T00:07:08Z",
103
+ # #=> "id": 122
104
+ # #=> }
105
+
106
+ def self.show(client, gid)
107
+ client.request(:get, "groups/#{gid}.json")['group']
108
+ end
109
+
110
+ # Create a group
111
+ #
112
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
113
+ # @param group [Hash] The group details to use
114
+ # @return [String|Hash]
115
+ #
116
+ # @example
117
+ # group = {
118
+ # name: 'Test Group'
119
+ # }
120
+ # ZendeskSupportAPI::Groups.create(client, group)
121
+ # #=> {
122
+ # #=> "name": "Test Group",
123
+ # #=> "created_at": "2011-04-20T17:49:00Z",
124
+ # #=> "updated_at": "2011-04-20T17:49:00Z",
125
+ # #=> "id": 123
126
+ # #=> }
127
+
128
+ def self.create(client, group)
129
+ res = client.request(:post, 'groups.json', group: group)
130
+ return "Creation failed: #{res['details']}" if res['error']
131
+
132
+ res
133
+ end
134
+
135
+ # Updates a group
136
+ #
137
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
138
+ # @param gid [Integer] The group ID to use
139
+ # @param group [Hash] The group details to use
140
+ # @return [String|Hash]
141
+ #
142
+ # @example
143
+ # group = {
144
+ # name: 'Test Group - defunct'
145
+ # }
146
+ # ZendeskSupportAPI::Groups.update(client, 123, group)
147
+ # #=> {
148
+ # #=> "name": "Test Group - defunct",
149
+ # #=> "created_at": "2011-04-20T17:49:00Z",
150
+ # #=> "updated_at": "2011-07-20T17:49:00Z",
151
+ # #=> "id": 123
152
+ # #=> }
153
+
154
+ def self.update(client, gid, group)
155
+ res = client.request(:post, "groups/#{gid}.json", group: group)
156
+ return "Update failed: #{res['details']}" if res['error']
157
+
158
+ res
159
+ end
160
+
161
+ # Deletes a group
162
+ #
163
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
164
+ # @param gid [Integer] The group ID to use
165
+ # @return [String]
166
+ #
167
+ # @example
168
+ # ZendeskSupportAPI::Groups.delete(client, 123)
169
+ # #=> Group 123 has been deleted
170
+
171
+ def self.delete(client, gid)
172
+ res = client.request(:delete, "groups/#{gid}.json")
173
+ return "Deletion of #{gid} failed: #{res['error']}" if res['error']
174
+
175
+ "Group #{gid} has been deleted"
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,95 @@
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 to use
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
+
42
+ # Make a request to show all job statuses
43
+ #
44
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
45
+ # @return [Hash]
46
+ #
47
+ # @example
48
+ # ZendeskSupportAPI::Jobs.show_many(client, ['abc123', 'def456'])
49
+ # #=> {
50
+ # #=> "job_statuses": [
51
+ # #=> {
52
+ # #=> "id": "abc123",
53
+ # #=> "status": "completed",
54
+ # #=> ...
55
+ # #=> },
56
+ # #=> {
57
+ # #=> "id": "def456",
58
+ # #=> "status": "completed",
59
+ # #=> ...
60
+ # #=> }
61
+ # #=> ]
62
+ # #=> }
63
+
64
+ def self.list(client)
65
+ client.request(:get, 'job_statuses.json')['job_statuses']
66
+ end
67
+
68
+ # Show many job statuses
69
+ #
70
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
71
+ # @param ids [Array] An Array of job IDs to show
72
+ # @return [Hash]
73
+ #
74
+ # @example
75
+ # ZendeskSupportAPI::Jobs.show_many(client, ['abc123', 'def456'])
76
+ # #=> {
77
+ # #=> "job_statuses": [
78
+ # #=> {
79
+ # #=> "id": "abc123",
80
+ # #=> "status": "completed",
81
+ # #=> ...
82
+ # #=> },
83
+ # #=> {
84
+ # #=> "id": "def456",
85
+ # #=> "status": "completed",
86
+ # #=> ...
87
+ # #=> }
88
+ # #=> ]
89
+ # #=> }
90
+
91
+ def self.show_many(client, ids)
92
+ client.request(:get, "job_statuses/show_many.json?ids=#{ids.join(',')}")
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,268 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZendeskSupportAPI
4
+ # Organizations class - https://developer.zendesk.com/rest_api/docs/support/organizations
5
+ class Organizations
6
+ # Prints out organizations
7
+ #
8
+ # @return [String]
9
+
10
+ def self.orgs
11
+ 'organizations'
12
+ end
13
+
14
+ # Lists Organizations (first 100)
15
+ #
16
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
17
+ # @return [Array]
18
+ #
19
+ # @example
20
+ # ZendeskSupportAPI::Organizations.list(client)
21
+ # #=> [
22
+ # #=> {
23
+ # #=> "url": "https://zendesk.com/api/v2/organizations/1.json",
24
+ # #=> "id": 1,
25
+ # #=> "name": "One Organization",
26
+ # #=> ...
27
+ # #=> },
28
+ # #=> ...
29
+ # #=> {
30
+ # #=> "url": "https://zendesk.com/api/v2/organizations/100.json",
31
+ # #=> "id: 100,
32
+ # #=> "name": "Other Organization",
33
+ # #=> ...
34
+ # #=> }
35
+ # #=> ]
36
+
37
+ def self.list(client)
38
+ client.request(:get, "#{orgs}.json")[orgs]
39
+ end
40
+
41
+ # Shows info about an organization
42
+ #
43
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
44
+ # @param oid [Integer] The Organization ID to use
45
+ # @return [Hash]
46
+ #
47
+ # @example
48
+ # ZendeskSupportAPI::Organizations.show(client, 123)
49
+ # #=> {
50
+ # #=> "url": "https://zendesk.com/api/v2/organizations/1.json",
51
+ # #=> "id": 1,
52
+ # #=> "name": "One Organization",
53
+ # #=> ...
54
+ # #=> }
55
+
56
+ def self.show(client, oid)
57
+ client.request(:get, "#{orgs}/#{oid}.json")['organization']
58
+ end
59
+
60
+ # Show several organizations
61
+ #
62
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
63
+ # @param oids [Array] An Array of Organization IDs to show
64
+ # @return [Array]
65
+ #
66
+ # @example
67
+ # ZendeskSupportAPI::Organizations.show(client, [1,2])
68
+ # #=> [
69
+ # #=> {
70
+ # #=> "url": "https://zendesk.com/api/v2/organizations/1.json",
71
+ # #=> "id": 1,
72
+ # #=> "name": "One Organization",
73
+ # #=> ...
74
+ # #=> },
75
+ # #=> {
76
+ # #=> "url": "https://zendesk.com/api/v2/organizations/2.json",
77
+ # #=> "id": 2,
78
+ # #=> "name": "Two Organization",
79
+ # #=> ...
80
+ # #=> }
81
+ # #=> ]
82
+
83
+ def self.show_many(client, oids)
84
+ ids = "ids=#{oids.join(',')}"
85
+ client.request(:get, "#{orgs}/show_many.json?#{ids}")[orgs]
86
+ end
87
+
88
+ # Creates an organization
89
+ #
90
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
91
+ # @param org [Hash] The organization info to use
92
+ # @return [Hash|String]
93
+ #
94
+ # @example
95
+ # org = {
96
+ # name: 'Test Organization',
97
+ # }
98
+ # ZendeskSupportAPI::Organizations.create(client, org)
99
+ # #=> {
100
+ # #=> "url": "",
101
+ # #=> "id": 123,
102
+ # #=> "name": "Test Organization",
103
+ # #=> ...
104
+ # #=> }
105
+
106
+ def self.create(client, org)
107
+ res = client.request(:post, "#{orgs}.json", organization: org)
108
+ return "Creation failed: #{res['details']}" if res['error']
109
+
110
+ res['organization']
111
+ end
112
+
113
+ # Creates many organizations
114
+ #
115
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
116
+ # @param users [Array] The organizations to create
117
+ # @return [ZendeskSupportAPI::Client.handle_job]
118
+
119
+ def self.create_many(client, orgs)
120
+ url = "#{orgs}/create_many.json"
121
+ res = client.request(:post, url, organizations: orgs)
122
+ client.handle_job(res)
123
+ end
124
+
125
+ # Creates or updates an organization
126
+ #
127
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
128
+ # @param org [Hash] The organization info to use
129
+ # @return [Hash|String]
130
+ #
131
+ # @example
132
+ # org = {
133
+ # name: 'Test Organization',
134
+ # }
135
+ # ZendeskSupportAPI::Organizations.create_or_update(client, org)
136
+ # #=> {
137
+ # #=> "url": "",
138
+ # #=> "id": 123,
139
+ # #=> "name": "Test Organization",
140
+ # #=> ...
141
+ # #=> }
142
+
143
+ def self.create_or_update(client, org)
144
+ url = "#{orgs}/create_or_update.json"
145
+ res = client.request(:post, url, organization: org)
146
+ return "Create/Update failed: #{res['description']}" if res['error']
147
+
148
+ res['organization']
149
+ end
150
+
151
+ # Updates an organization
152
+ #
153
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
154
+ # @param oid [Integer] The Organization ID to use
155
+ # @param org [Hash] The organization info to use
156
+ # @return [Hash|String]
157
+ #
158
+ # @example
159
+ # ZendeskSupportAPI::Organizations.update(client, 123, org)
160
+ # #=> {organization}
161
+
162
+ def self.update(client, oid, org)
163
+ res = client.request(:post, "#{orgs}/#{oid}.json", organization: org)
164
+ return "Update of #{uid} failed: #{res['error']}" if res['error']
165
+
166
+ res['organization']
167
+ end
168
+
169
+ # Updates many organizations
170
+ #
171
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
172
+ # @param orgs [Array] The organizations to update
173
+ # @return [ZendeskSupportAPI::Client.handle_job]
174
+
175
+ def self.update_many(client, orgs)
176
+ url = "#{orgs}/update_many.json"
177
+ res = client.request(:put, url, organizations: orgs)
178
+ client.handle_job(res)
179
+ end
180
+
181
+ # Deletes an organization
182
+ #
183
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
184
+ # @param oid [Integer] The Organization ID to delete
185
+ # @return [String]
186
+ #
187
+ # @example
188
+ # ZendeskSupportAPI::Organizations.delete(client, 123)
189
+ # #=> Organization 123 has been deleted
190
+ # ZendeskSupportAPI::Organizations.delete(client, 123)
191
+ # #=> "Deletion of 123 failed: RecordNotFound"
192
+
193
+ def self.delete(client, oid)
194
+ res = client.request(:delete, "#{orgs}/#{oid}.json")
195
+ return "Deletion of #{oid} failed: #{res['error']}" if res['error']
196
+
197
+ "Organization #{uid} has been deleted"
198
+ end
199
+
200
+ # Deletes many organizations
201
+ #
202
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
203
+ # @param ids [Array] The array of Organization IDs to delete
204
+ # @return [ZendeskSupportAPI::Client.handle_job
205
+
206
+ def self.bulk_delete(client, ids)
207
+ url = "#{orgs}/destroy_many.json?ids=#{ids.join(',')}"
208
+ res = client.request(:delete, url)
209
+ client.handle_job(res)
210
+ end
211
+
212
+ # Searches for orgs by their external_id (first 100)
213
+ #
214
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
215
+ # @param eid [String] The External ID to use
216
+ # @return [Array]
217
+ #
218
+ # @example
219
+ # ZendeskSupportAPI::Organizations.search_by_external_id(client, 'abc123')
220
+ # #=> [
221
+ # #=> {
222
+ # #=> "url": "https://zendesk.com/api/v2/organizations/1.json",
223
+ # #=> "id": 1,
224
+ # #=> "name": "One Organization",
225
+ # #=> ...
226
+ # #=> },
227
+ # #=> ...
228
+ # #=> {
229
+ # #=> "url": "https://zendesk.com/api/v2/organizations/100.json",
230
+ # #=> "id: 100,
231
+ # #=> "name": "Other Organization",
232
+ # #=> ...
233
+ # #=> }
234
+ # #=> ]
235
+
236
+ def self.search_by_external_id(client, eid)
237
+ client.request(:get, "#{orgs}/search.json?external_id=#{eid}")[orgs]
238
+ end
239
+
240
+ # Get an Organization's members
241
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
242
+ # @param oid [Integer] The Organization ID to use
243
+ # @return [Array]
244
+ #
245
+ # @example
246
+ # ZendeskSupportAPI::Organizations.members(client, 123)
247
+ # #=> [
248
+ # #=> {
249
+ # #=> "id": 1,
250
+ # #=> "name": "Albert",
251
+ # #=> "email": "albert@example.com",
252
+ # #=> ...
253
+ # #=> },
254
+ # #=> ...
255
+ # #=> {
256
+ # #=> "id": 22,
257
+ # #=> "name": "Victor",
258
+ # #=> "email": "victor@example.com",
259
+ # #=> ...
260
+ # #=> }
261
+ # #=> ]
262
+
263
+ def self.members(client, oid)
264
+ url = "#{orgs}/#{oid}/organization_memberships.json?include=users"
265
+ client.request(:get, url)['users']
266
+ end
267
+ end
268
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZendeskSupportAPI
4
+ # Search class - https://developer.zendesk.com/rest_api/docs/support/search
5
+ class Search
6
+ # Perform a search and list results (first 100)
7
+ #
8
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
9
+ # @param query [String] The query to use
10
+ # @param sort [String] The sorting method to use (defaults to revelance)
11
+ # @param order [String] The order to use (defaults to desc)
12
+ # @return [Array]
13
+ #
14
+ # @example
15
+ # ZendeskSupportAPI::Search.list(client, 'bob')
16
+ # #=> [
17
+ # #=> {
18
+ # #=> "name": "Bob McBob",
19
+ # #=> "created_at": "2009-05-13T00:07:08Z",
20
+ # #=> "updated_at": "2011-07-22T00:11:12Z",
21
+ # #=> "id": 211,
22
+ # #=> "result_type": "user"
23
+ # #=> "url": "https://zendesk.com/api/v2/users/211.json"
24
+ # #=> },
25
+ # #=> {
26
+ # #=> "name": "Bob's Company",
27
+ # #=> "created_at": "2009-08-26T00:07:08Z",
28
+ # #=> "updated_at": "2010-05-13T00:07:08Z",
29
+ # #=> "id": 122,
30
+ # #=> "result_type": "group"
31
+ # #=> "url": "https://zendesk.com/api/v2/groups/122.json"
32
+ # #=> },
33
+ # #=> ...
34
+ # #=> ]
35
+
36
+ def self.list(client, query, sort = '', order = 'desc')
37
+ return bad_search("invalid sort - #{sort}") unless sort_valid?(sort)
38
+ return bad_search("invalid order - #{order}") unless order_valid?(order)
39
+
40
+ sort_order = sort_order_string(sort, order)
41
+ client.request(:get, "search.json?query=#{query}#{sort_order}")['results']
42
+ end
43
+
44
+ # Determines if a given sort string is valid
45
+ #
46
+ # @param sort [String] The sort string to check
47
+ # @return [Boolean]
48
+ #
49
+ # @example
50
+ # ZendeskSupportAPI::Search.sort_valid? 'updated_at'
51
+ # #=> true
52
+ # ZendeskSupportAPI::Search.sort_valid? 'ticket_type'
53
+ # #=> true
54
+ # ZendeskSupportAPI::Search.sort_valid? 'random'
55
+ # #=> false
56
+
57
+ def self.sort_valid?(sort)
58
+ return true if sort.empty?
59
+
60
+ valid = %w[updated_at created_at priority status ticket_type]
61
+ valid.include? sort
62
+ end
63
+
64
+ # Determines if a given order string is valid
65
+ #
66
+ # @param order [String] The order string to check
67
+ # @return [Boolean]
68
+ #
69
+ # @example
70
+ # ZendeskSupportAPI::Search.order_valid? 'asc'
71
+ # #=> true
72
+ # ZendeskSupportAPI::Search.order_valid? 'desc'
73
+ # #=> true
74
+ # ZendeskSupportAPI::Search.order_valid? 'random'
75
+ # #=> false
76
+
77
+ def self.order_valid?(order)
78
+ valid = %w[asc desc]
79
+ valid.include? order
80
+ end
81
+
82
+ # Prints out an error message
83
+ #
84
+ # @param string [String] The error that occurred
85
+ # @return [String]
86
+ #
87
+ # @example
88
+ # ZendeskSupportAPI::Search.bad_search('invalid sort - name')
89
+ # #=> "Search cannot be completed: invalid sort - name"
90
+
91
+ def self.bad_search(string)
92
+ "Search cannot be completed: #{string}"
93
+ end
94
+
95
+ # Determines the sort and order to use in the query
96
+ #
97
+ # @param sort [String] The sort string to use
98
+ # @param order [String] The order string to use
99
+ # @return [String]
100
+ #
101
+ # @example
102
+ # ZendeskSupportAPI::Search.sort_order_string('', 'asc')
103
+ # #=> "&order_by=asc"
104
+ # ZendeskSupportAPI::Search.sort_order_string('priority', 'desc')
105
+ # #=> "&sort_by=priority"
106
+ # ZendeskSupportAPI::Search.sort_order_string('updated_at', 'asc')
107
+ # #=> "&sort_by=updated_at&order_by=asc"
108
+
109
+ def self.sort_order_string(sort, order)
110
+ sort = (sort.empty? ? '' : "&sort_by=#{sort}")
111
+ order = (order == 'desc' ? '' : '&order_by=asc')
112
+ "#{sort}#{order}"
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,317 @@
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 (total: 215336)... / ...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
+ # #=> ...
89
+ # #=> }
90
+
91
+ def self.show(client, id)
92
+ res = client.request(:get, "users/#{id}.json?#{included}")
93
+ user_object(res['user'], res['organizations'])
94
+ end
95
+
96
+ # Shows many users
97
+ #
98
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
99
+ # @param ids [Array] An array of User IDs to use
100
+ # @return [Array]
101
+ #
102
+ # @example
103
+ # ZendeskSupportAPI::Users.show_many(client, [123, 456])
104
+ # #=> [
105
+ # #=> {
106
+ # #=> "id": 123,
107
+ # #=> "name": "Johnny Appleseed",
108
+ # #=> ...
109
+ # #=> },
110
+ # #=> {
111
+ # #=> "id": 456,
112
+ # #=> "name": "Rupert Root",
113
+ # #=> ...
114
+ # #=> }
115
+ # #=> ]
116
+
117
+ def self.show_many(client, ids)
118
+ ids = ids.join(',')
119
+ res = client.request(:get, "users/show_many.json?#{included}&ids=#{ids}")
120
+ res['users'].map { |u| user_object(u, res['organizations']) }
121
+ res['users']
122
+ end
123
+
124
+ # Creates a user hash (for mapping the org into the user Hash)
125
+ #
126
+ # @param user [Hash] The user details to use
127
+ # @param orgs [Array] The Array of orgs to use
128
+ # @return [Hash]
129
+
130
+ def self.user_object(user, orgs)
131
+ oid = 'organization_id'
132
+ user['organization'] = orgs.select { |o| o['id'] == user[oid] }
133
+ user
134
+ end
135
+
136
+ # Creates a user
137
+ #
138
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
139
+ # @param user [Hash] The user to create
140
+ # @return [Hash|String] Either the user details or the error
141
+ #
142
+ # @example
143
+ # user = {name: 'Roger Wilco', email: 'rw@example.com'}
144
+ # ZendeskSupportAPI::Users.create(client, user)
145
+ # #=> {
146
+ # #=> "user": {
147
+ # #=> "id": 9873843,
148
+ # #=> "name": "Roger Wilco",
149
+ # #=> "email": "rw@example.com"
150
+ # #=> ...
151
+ # #=> }
152
+ # #=> }
153
+ # ZendeskSupportAPI::User.create(client, user)
154
+ # #=> Creation failed: => "Creation failed: {\"email\"=>[{\"description\"
155
+ # #=> =>\"Email: rw@example.com is already being used by another user\",
156
+ # #=> \"error\"=>\"DuplicateValue\"}]}"
157
+
158
+ def self.create(client, user)
159
+ res = client.request(:post, 'users.json', user: user)
160
+ return "Creation failed: #{res['details']}" if res['error']
161
+
162
+ res
163
+ end
164
+
165
+ # Creates many users
166
+ #
167
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
168
+ # @param users [Array] The users to create
169
+ # @return [ZendeskSupportAPI::Client.handle_job]
170
+
171
+ def self.create_many(client, users)
172
+ res = client.request(:post, 'users/create_many.json', users: users)
173
+ client.handle_job(res)
174
+ end
175
+
176
+ # Creates or updates a user
177
+ #
178
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
179
+ # @param user [Hash] The user to create/update
180
+ # @return [Hash|String] Either the user details or the error
181
+ #
182
+ # @example
183
+ # ZendeskSupportAPI::User.create_or_update(client, user)
184
+ # #=> {
185
+ # #=> "user": {
186
+ # #=> "id": 9873843,
187
+ # #=> "name": "Roger Wilco",
188
+ # #=> "email": "rw@example.com"
189
+ # #=> ...
190
+ # #=> }
191
+ # #=> }
192
+
193
+ def self.create_or_update(client, user)
194
+ res = client.request(:post, 'users/create_or_update.json', user: user)
195
+ return "Create/Update failed: #{res['description']}" if res['error']
196
+
197
+ res
198
+ end
199
+
200
+ # Creates or updates many users
201
+ #
202
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
203
+ # @param users [Array] The users to create/update
204
+ # @return [ZendeskSupportAPI::Client.handle_job]
205
+
206
+ def self.create_or_update_many(client, users)
207
+ res = client.request(:post,
208
+ 'users/create_or_update_many.json',
209
+ users: users)
210
+ client.handle_job(res)
211
+ end
212
+
213
+ # Updates a user
214
+ #
215
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
216
+ # @param uid [Integer] The User's ID
217
+ # @param user [Hash] The user details to update
218
+ # @return [Hash|String] Either the user details or the error
219
+ #
220
+ # @example
221
+ # ZendeskSupportAPI::User.update(client, 123, user)
222
+ # #=> {user}
223
+
224
+ def self.update(client, uid, user)
225
+ res = client.request(:put, "users/#{uid}.json", user: user)
226
+ return "Update of #{uid} failed: #{res['error']}" if res['error']
227
+
228
+ res
229
+ end
230
+
231
+ # Updates many users
232
+ #
233
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
234
+ # @param users [Array] The users to update
235
+ # @return [ZendeskSupportAPI::Client.handle_job]
236
+
237
+ def self.update_many(client, users)
238
+ res = client.request(:put, 'users/update_many.json', users: users)
239
+ client.handle_job(res)
240
+ end
241
+
242
+ # Deletes a user
243
+ #
244
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
245
+ # @param uid [Integer] The User ID to delete
246
+ # @return [String]
247
+ #
248
+ # @example
249
+ # ZendeskSupportAPI::Users.delete(client, 123)
250
+ # #=> User 123 has been deleted
251
+ # ZendeskSupportAPI::Users.delete(client, 123)
252
+ # #=> "Deletion of 123 failed: RecordNotFound"
253
+
254
+ def self.delete(client, uid)
255
+ res = client.request(:delete, "users/#{uid}.json")
256
+ return "Deletion of #{uid} failed: #{res['error']}" if res['error']
257
+
258
+ "User #{uid} has been deleted"
259
+ end
260
+
261
+ # Deletes many users
262
+ #
263
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
264
+ # @param ids [Array] The array of User IDs to delete
265
+ # @return [ZendeskSupportAPI::Client.handle_job]
266
+
267
+ def self.delete_many(client, ids)
268
+ ids = ids.join(',')
269
+ res = client.request(:delete, "users/destroy_many.json?ids=#{ids}")
270
+ client.handle_job(res)
271
+ end
272
+
273
+ # Suspends a user
274
+ #
275
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
276
+ # @param id [Integer] The User ID to suspend
277
+ # @return [String] Either a success message or an error
278
+ #
279
+ # @example
280
+ # ZendeskSupportAPI::Users.suspend(client, 123)
281
+ # #=> User 123 is suspended
282
+
283
+ def self.suspend(client, id)
284
+ res = client.request(:put, "users/#{id}.json", user: { suspended: true })
285
+ return "Suspension of #{id} failed: #{res['error']}" if res['error']
286
+
287
+ "User #{id} suspended" if res['user']['suspended']
288
+ end
289
+
290
+ # Shows a users groups
291
+ #
292
+ # @param client [ZendeskSupportAPI::Client] The client instance to use
293
+ # @param uid [Integer] The User ID to use
294
+ # @return [Array]
295
+ #
296
+ # @example
297
+ # ZendeskSupportAPI::Users.groups(client, 1234)
298
+ # #=> [
299
+ # #=> {
300
+ # #=> "name": "DJs",
301
+ # #=> "created_at": "2009-05-13T00:07:08Z",
302
+ # #=> "updated_at": "2011-07-22T00:11:12Z",
303
+ # #=> "id": 211
304
+ # #=> },
305
+ # #=> {
306
+ # #=> "name": "MCs",
307
+ # #=> "created_at": "2009-08-26T00:07:08Z",
308
+ # #=> "updated_at": "2010-05-13T00:07:08Z",
309
+ # #=> "id": 122
310
+ # #=> }
311
+ # #=> ]
312
+
313
+ def self.groups(client, uid)
314
+ client.request(:get, "users/#{uid}/groups.json")['groups']
315
+ end
316
+ end
317
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZendeskSupportAPI
4
+ # Outputs the gem version
5
+
6
+ VERSION = '0.4.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,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zendesk_support_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.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/groups.rb
57
+ - lib/zendesk_support_api/jobs.rb
58
+ - lib/zendesk_support_api/organizations.rb
59
+ - lib/zendesk_support_api/search.rb
60
+ - lib/zendesk_support_api/users.rb
61
+ - lib/zendesk_support_api/version.rb
62
+ - zendesk_support_api.gemspec
63
+ homepage: https://gitlab.com/reyloc/zendesk_support_api
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.0.3
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Zendesk Support API wrapper gem
86
+ test_files: []