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.



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

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.



7
8
9
# File 'lib/guidepost/provider/zendesk.rb', line 7

def project_name
  @project_name
end

#subdomainObject (readonly)

Returns the value of attribute subdomain.



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

def subdomain
  @subdomain
end

Instance Method Details

#backup_all_articles(options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/guidepost/provider/zendesk.rb', line 55

def backup_all_articles(options={})
    # Get all articles (with pagination)
    sideload = options[:sideload] || false
    all_locales = options[:all_locales] || true
    articles = self.retrieve_all_articles(sideload: sideload, all_locales: all_locales)
        
    # 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/#{@subdomain}/article_backups/#{filename}.json", string_content: articles.to_json)
        
    articles.count
end

#base_api_urlObject



383
384
385
# File 'lib/guidepost/provider/zendesk.rb', line 383

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

#retrieve_all_article_attachments(options = {}) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/guidepost/provider/zendesk.rb', line 271

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, url: next_page, locale: options[:locale])
            break if attachments.nil? || attachments.empty?
            article_attachments += attachments
            break if next_page.nil?
        end
    end

    article_attachments
end

#retrieve_all_articles(options = {}) ⇒ Object



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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/guidepost/provider/zendesk.rb', line 71

def retrieve_all_articles(options={})
    sideload = options[:sideload] || false
    all_locales = options[:all_locales] || true

    page_next = nil
    articles = []
    article_attachments = nil

    locales = (self.retrieve_all_locales || []).map{|locale_json| locale_json['locale'] } if all_locales
    locales = [''] if locales.nil? || locales.empty?

    if !sideload
        article_attachments = []
        locales.each do |locale|
            page_next = nil
            locale_articles = []
            while true
                page_articles, page_next = self.retrieve_articles(url: page_next, locale: locale)
                break if page_articles.nil? || page_articles.empty?
                locale_articles += page_articles
                break if page_next.nil?
            end
            articles += locale_articles

            article_attachments += self.retrieve_all_article_attachments(articles: locale_articles, locale: locale)
        end

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

        section_urls = Hash.new
        category_urls = Hash.new

        locales.each do |locale|
            page_next = nil
            locales_articles = []

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

                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

                locales_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
            articles += locales_articles
            
            article_attachments += self.retrieve_all_article_attachments(articles: locales_articles, locale: locale)
        end

        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_locales(options = {}) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/guidepost/provider/zendesk.rb', line 311

def retrieve_all_locales(options={})
    locales = []
    next_page = nil

    while true
        tmp_locales, next_page = self.retrieve_locales(url: next_page)
        break if tmp_locales.nil? || tmp_locales.empty?
        locales += tmp_locales
        break if next_page.nil?
    end

    locales
end

#retrieve_all_permission_groups(options = {}) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/guidepost/provider/zendesk.rb', line 237

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_translations(options = {}) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/guidepost/provider/zendesk.rb', line 346

def retrieve_all_translations(options={})
    translations = []
    next_page = nil
    article = options[:for_article]

    while true
        tmp_translations, next_page = self.retrieve_translations(url: next_page, for_article: article)
        break if tmp_translations.nil? || tmp_translations.empty?
        translations += tmp_translations
        break if next_page.nil?
    end

    translations
end

#retrieve_all_user_segments(options = {}) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/guidepost/provider/zendesk.rb', line 203

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



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/guidepost/provider/zendesk.rb', line 288

def retrieve_article_attachments(options={})
    url = options[:url]
    article = options[:for_article]
    locale = (options[:locale] || "").downcase

    url = "#{self.base_api_url}/help_center/#{locale}/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



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/guidepost/provider/zendesk.rb', line 171

def retrieve_articles(options={})
    url = options[:url]
    locale = (options[:locale] || "").downcase
    sideload = options[:sideload] || false

    if !sideload
        url = "#{self.base_api_url}/help_center/#{locale}/articles.json?per_page=25&page=1" if url.nil?
    else
        url = "#{self.base_api_url}/help_center/#{locale}/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_locales(options = {}) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/guidepost/provider/zendesk.rb', line 325

def retrieve_locales(options={})
    url = options[:url]

    url = "#{self.base_api_url}/locales.json" 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["locales"], j_body["next_page"]
end

#retrieve_permission_groups(options = {}) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/guidepost/provider/zendesk.rb', line 251

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_translations(options = {}) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/guidepost/provider/zendesk.rb', line 361

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

    url = "#{self.base_api_url}/help_center/articles/#{article['id']}/translations.json" 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["translations"], j_body["next_page"]
end

#retrieve_user_segments(options = {}) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/guidepost/provider/zendesk.rb', line 217

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



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
51
52
53
# File 'lib/guidepost/provider/zendesk.rb', line 22

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

    url = "#{self.base_api_url}/help_center/articles/search.json?query=#{URI::encode(query)}&per_page=10"
    url += "&locale=#{options[:locale]}" if !options[:locale].nil? && !options[:locale].empty?
    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