Class: Guidepost::Provider::Zendesk

Inherits:
Object
  • Object
show all
Defined in:
lib/guidepost/provider/zendesk.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Zendesk

Returns a new instance of Zendesk.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/guidepost/provider/zendesk.rb', line 7

def initialize(options={})
    @subdomain = options[:subdomain]
    @project_name = options[:project_name]

    raise "Guidepost::Provider::Zendesk initializer is missing either a subdomain or project_name" if @subdomain.nil? || @project_name.nil?

    @project_name.upcase!
    @storage = options[:storage] || Guidepost::Storage::S3.new(project_name: @project_name)

    @email = "#{ENV["#{@project_name}_GUIDEPOST_ZENDESK_EMAIL"]}/token"
    @password = ENV["#{@project_name}_GUIDEPOST_ZENDESK_PASSWORD_TOKEN"]
end

Instance Attribute Details

#project_nameObject (readonly)

Returns the value of attribute project_name.



5
6
7
# File 'lib/guidepost/provider/zendesk.rb', line 5

def project_name
  @project_name
end

#subdomainObject (readonly)

Returns the value of attribute subdomain.



4
5
6
# File 'lib/guidepost/provider/zendesk.rb', line 4

def subdomain
  @subdomain
end

Instance Method Details

#backup_all_articles(options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/guidepost/provider/zendesk.rb', line 52

def backup_all_articles(options={})
    # Get all articles (with pagination)
    sideload = options[:sideload] || false
    articles = self.retrieve_all_articles(sideload: sideload)
        
    # Upload to S3
    timestamp = Time.now.strftime('%Y%m%d%H%M%S')

    filename = "#{timestamp}"
    filename += "_with_sideload" if sideload
    @storage.upload_file(path: "zendesk/article_backups/#{filename}.json", string_content: articles.to_json)
        
    articles.count
end

#base_api_urlObject



286
287
288
# File 'lib/guidepost/provider/zendesk.rb', line 286

def base_api_url
    "https://#{self.subdomain}.zendesk.com/api/v2"
end

#retrieve_all_article_attachments(options = {}) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/guidepost/provider/zendesk.rb', line 248

def retrieve_all_article_attachments(options={})
    article_attachments = []
    next_page = nil

    articles = options[:articles]
    articles.each do |article|
        while true
            attachments, next_page = self.retrieve_article_attachments(for_article: article)
            break if attachments.nil? || attachments.empty?
            article_attachments += attachments
            break if next_page.nil?
        end
    end

    article_attachments
end

#retrieve_all_articles(options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/guidepost/provider/zendesk.rb', line 67

def retrieve_all_articles(options={})
    sideload = options[:sideload] || false
    page_next = nil
    articles = []
    article_attachments = nil

    if !sideload
        while true
            page_articles, page_next = self.retrieve_articles(url: page_next)
            break if page_articles.nil? || page_articles.empty?
            articles += page_articles
            break if page_next.nil?
        end

        article_attachments = self.retrieve_all_article_attachments(articles: articles)

        return {
            articles: articles,
            article_count: articles.count,
            article_attachments: article_attachments,
            article_attachment_count: article_attachments.count
        }
    else
        sections = []
        categories = []

        section_urls = Hash.new
        category_urls = Hash.new

        while true
            page, page_next = self.retrieve_articles(url: page_next, sideload: true)

            articles_from_page = page["articles"]
            sections_from_page = page["sections"]
            categories_from_page = page["categories"]

            no_more_articles = articles_from_page.nil? || articles_from_page.empty?
            no_more_sections = sections_from_page.nil? || sections_from_page.empty?
            no_more_categories = categories_from_page.nil? || categories_from_page.empty?

            break if no_more_articles && no_more_sections && no_more_categories

            articles += articles_from_page

            sections_from_page.each do |s|
                url = s["url"]
                if !section_urls.has_key?(url)
                    section_urls[url] = 1
                else
                    section_urls[url] += 1
                end
                sections << s if section_urls[url] == 1
            end

            categories_from_page.each do |c|
                url = c["url"]
                if !category_urls.has_key?(url)
                    category_urls[url] = 1
                else
                    category_urls[url] += 1
                end
                categories << c if category_urls[url] == 1
            end

            break if page_next.nil?
        end

        article_attachments = self.retrieve_all_article_attachments(articles: articles)

        return { 
            categories: categories, 
            category_count: categories.count, 
            sections: sections, 
            section_count: sections.count, 
            articles: articles,
            article_count: articles.count,
            article_attachments: article_attachments,
            article_attachment_count: article_attachments.count
        }
    end
end

#retrieve_all_permission_groups(options = {}) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/guidepost/provider/zendesk.rb', line 214

def retrieve_all_permission_groups(options={})
    permission_groups = []
    next_page = nil

    while true
        groups, next_page = self.retrieve_permission_groups(url: next_page)
        break if groups.nil? || groups.empty?
        permission_groups += groups
        break if next_page.nil?
    end

    permission_groups
end

#retrieve_all_user_segments(options = {}) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/guidepost/provider/zendesk.rb', line 180

def retrieve_all_user_segments(options={})
    user_segments = []
    next_page = nil

    while true
        segments, next_page = self.retrieve_user_segments(url: next_page)
        break if segments.nil? || segments.empty?
        user_segments += segments
        break if next_page.nil?
    end

    user_segments
end

#retrieve_article_attachments(options = {}) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/guidepost/provider/zendesk.rb', line 265

def retrieve_article_attachments(options={})
    article = options[:for_article]

    url = "#{self.base_api_url}/help_center/articles/#{article["id"]}/attachments.json?per_page=25&page=1" if url.nil?
    uri = URI.parse(url)
        
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        
    request = Net::HTTP::Get.new(uri.request_uri)
    request.basic_auth(@email, @password)
    response = http.request(request)
        
    body = response.body.force_encoding("UTF-8")

    j_body = JSON.parse(body)

    return j_body["article_attachments"], j_body["next_page"]
end

#retrieve_articles(options = {}) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/guidepost/provider/zendesk.rb', line 149

def retrieve_articles(options={})
    url = options[:url]
    sideload = options[:sideload] || false

    if !sideload
        url = "#{self.base_api_url}/help_center/articles.json?per_page=25&page=1" if url.nil?
    else
        url = "#{self.base_api_url}/help_center/articles.json?include=sections,categories&per_page=25&page=1" if url.nil?
    end
    
    uri = URI.parse(url)
        
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        
    request = Net::HTTP::Get.new(uri.request_uri)
    request.basic_auth(@email, @password)
    response = http.request(request)
        
    body = response.body.force_encoding("UTF-8")
        
    j_body = JSON.parse(body)

    if !sideload
        return j_body['articles'], j_body['next_page']
    else
        return j_body, j_body['next_page']
    end
end

#retrieve_permission_groups(options = {}) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/guidepost/provider/zendesk.rb', line 228

def retrieve_permission_groups(options={})
    url = options[:url]
    url = "#{self.base_api_url}/guide/permission_groups.json?per_page=25&page=1" if url.nil?
    uri = URI.parse(url)
        
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        
    request = Net::HTTP::Get.new(uri.request_uri)
    request.basic_auth(@email, @password)
    response = http.request(request)
        
    body = response.body.force_encoding("UTF-8")

    j_body = JSON.parse(body)

    return j_body["permission_groups"], j_body['next_page']
end

#retrieve_user_segments(options = {}) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/guidepost/provider/zendesk.rb', line 194

def retrieve_user_segments(options={})
    url = options[:url]
    url = "#{self.base_api_url}/help_center/user_segments.json?per_page=25&page=1" if url.nil?
    uri = URI.parse(url)
        
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        
    request = Net::HTTP::Get.new(uri.request_uri)
    request.basic_auth(@email, @password)
    response = http.request(request)
        
    body = response.body.force_encoding("UTF-8")

    j_body = JSON.parse(body)

    return j_body["user_segments"], j_body['next_page']
end

#search(options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/guidepost/provider/zendesk.rb', line 20

def search(options={})
    query = options.fetch(:query, "")
    return [] if query.empty?

    url = "#{self.base_api_url}/help_center/articles/search.json?query=#{query}&per_page=5"
    uri = URI.parse(url)
        
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        
    request = Net::HTTP::Get.new(uri.request_uri)
    request.basic_auth(@email, @password)
    response = http.request(request)
        
    body = response.body.force_encoding("UTF-8")

    j_body = JSON.parse(body)
    results = j_body.fetch("results", [])
    
    slimmed_down_results = results.map do |result|
        slimmed_down_result = Hash.new
        slimmed_down_result[:id] = result["id"]
        slimmed_down_result[:title] = result["title"]
        slimmed_down_result[:snippet] = result["snippet"]

        slimmed_down_result
    end

    slimmed_down_results
end