zendesk_support_api 0.3.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: 4914120c422052e97716f808fe888ec810bee8977f4fcd9d275cc6b1705c4ca0
4
+ data.tar.gz: 4cded8d95430a510322e44f0310f18ce9c01a5776a4dac4ab476eaee487fbecf
5
+ SHA512:
6
+ metadata.gz: 0065aa4ea3865fd6169a1650ed126d1748e290190dd5a5755c72fda7e37f6f365599e1f73fe178c07d68aa6ed2fed202cad6f6768fec91567c86dd2dafc2817a
7
+ data.tar.gz: 89c5da1d7258032fe2c56369a7d31e69131137012ada8573618b61693be110ac8a5e0b5bb196afa4cdca0ad1a1e20c21b7ddaa4c51da58f4ddda62e1aef7f162
@@ -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,7 @@
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 f73dc3643644c6bf5f1a82593971b78a47b384fc
5
+ lib/zendesk_support_api/groups.rb bcc2a0b1af5bf3552c7838d80f339efbf8636e53
6
+ lib/zendesk_support_api/search.rb e741717c61809c578afbcd44f9b8e8f18a8c0363
7
+ lib/zendesk_support_api/version.rb e6591152418f6cbe5b2a2ff5d68799f735ed597d
File without changes
Binary file
Binary file
Binary file
@@ -0,0 +1,55 @@
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
47
+ * Initial creation of gem
48
+ * v0.2.0
49
+ * Added Search functions
50
+ * Fixed some yard syntax/formatting
51
+ * Changed version number
52
+ * v0.3.0
53
+ * Changed version number
54
+ * Added Group functions
55
+ * Added User.groups function
@@ -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,90 @@
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/search'
9
+ require 'zendesk_support_api/users'
10
+
11
+ # Create a new instance of Client
12
+ #
13
+ # @param user [String] - The API username to use
14
+ # @param token [String] - The API token to use
15
+ # @param url [String] - The API URL to use
16
+ #
17
+ # @example
18
+ # ZendeskSupportAPI::Client.new('user', '123', 'zendesk.com/api')
19
+ # #=> #<ZendeskSupportAPI::Client:0x00007f88779cb330 @user="user",
20
+ # #=> @token="123", @url="zendesk.com/api">
21
+ def initialize(user, token, url)
22
+ @user = user
23
+ @token = token
24
+ @url = url
25
+ end
26
+
27
+ # Make a request to the Zendesk Support API
28
+ #
29
+ # @param http_method [Symbol] The HTTP method to utilize
30
+ # @param endpoint [String] The endpoint to hit
31
+ # @param params [Hash] Parameters for the request
32
+ # @return [Hash]
33
+ #
34
+ # @example
35
+ # client = ZendeskSupportAPI::Client.new('user', '123', 'zendesk.com/api')
36
+ # client.response(:get, 'users.json')
37
+ # #=> {users:[{user1},{user2}...{user100}]}
38
+
39
+ def request(http_method, endpoint, params = {})
40
+ response = client.public_send(http_method, endpoint, params)
41
+ Oj.load(response.body)
42
+ end
43
+
44
+ # Outputs a spinner symbol
45
+ #
46
+ # @param string [String] The string to output at the beginning
47
+ # @param num [Integer] The index of the iteration
48
+ # @return [String]
49
+ #
50
+ # @example
51
+ # ZendeskSupportAPI::Client.spinner('users', 1) #=> Grabbing users... \
52
+ # ZendeskSupportAPI::Client.spinner('groups', 3) #=> /
53
+
54
+ def spinner(string, num)
55
+ print "Grabbing #{string}... " if num.to_i == 1
56
+ symbols = ['-', '\\', '|', '/']
57
+ print symbols[num.to_i % 4]
58
+ print "\b"
59
+ end
60
+
61
+ # Handles responses that create jobs
62
+ #
63
+ # @param job [Hash] - The output from a request that created a job
64
+ # @return [Hash]
65
+
66
+ def handle_job(job)
67
+ print 'Checking job'
68
+ while job['job_status']['status'] != 'completed'
69
+ print '.'
70
+ job = ZendeskSupportAPI::Jobs.show(self, job['job_status']['id'])
71
+ end
72
+ puts 'completed'
73
+ job['job_status']['results']
74
+ end
75
+
76
+ private
77
+
78
+ # Creates a new Faraday instance
79
+ #
80
+ # @return [ZendeskSupportAPI::Client]
81
+
82
+ def client
83
+ @client ||= Faraday.new(@url) do |c|
84
+ c.request :url_encoded
85
+ c.adapter Faraday.default_adapter
86
+ c.basic_auth "#{@user}/token", @token
87
+ end
88
+ end
89
+ end
90
+ 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,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.3.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,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zendesk_support_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.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/search.rb
59
+ - lib/zendesk_support_api/users.rb
60
+ - lib/zendesk_support_api/version.rb
61
+ - zendesk_support_api.gemspec
62
+ homepage: https://gitlab.com/reyloc/zendesk_support_api
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.0.3
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Zendesk Support API wrapper gem
85
+ test_files: []