Class: ZendeskArticle

Inherits:
Object
  • Object
show all
Defined in:
lib/zentool/zendesk_article.rb

Overview

Interface with zendesk to access articles

Instance Method Summary collapse

Constructor Details

#initialize(username, password, domain) ⇒ ZendeskArticle

Returns a new instance of ZendeskArticle.



4
5
6
7
8
9
10
11
# File 'lib/zentool/zendesk_article.rb', line 4

def initialize(username, password, domain)
  @root_uri = "https://#{domain}.zendesk.com/api/v2/help_center/en-us/"
  @articles_uri = @root_uri + 'articles.json'
  @sections_uri = @root_uri + 'sections.json'
  @categories_uri = @root_uri + 'categories.json'
  @username, @password = username, password
  check_auth
end

Instance Method Details

#articlesObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/zentool/zendesk_article.rb', line 13

def articles
  @articles ||= begin
    progressbar = ProgressBar.create(title: "#{raw_articles['count']} Articles", starting_at: 1, format: '%a |%b>>%i| %p%% %t', total: raw_articles['page_count'])
    articles = raw_articles['articles']

    (raw_articles['page_count'] - 1).times do |page|
      progressbar.increment
      articles += HTTParty.get("#{@articles_uri}?page=#{page + 2}", basic_auth)['articles']
    end
  end
  articles
end

#basic_authObject



68
69
70
71
72
73
74
75
# File 'lib/zentool/zendesk_article.rb', line 68

def basic_auth
  {
    basic_auth: {
      username: @username,
      password: @password
    }
  }
end

#categoriesObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/zentool/zendesk_article.rb', line 39

def categories
  @categories ||= begin
    progressbar = ProgressBar.create(title: "#{raw_categories['count']} Categories", starting_at: 1, format: '%a |%b>>%i| %p%% %t', total: raw_categories['page_count'])
    categories = raw_categories['categories']

    (raw_categories['page_count'] - 1).times do |page|
      progressbar.increment
      categories += HTTParty.get("#{@categories_uri}?page=#{page + 2}", basic_auth)['categories']
    end
  end
  categories
end

#check_authObject



77
78
79
80
81
82
83
# File 'lib/zentool/zendesk_article.rb', line 77

def check_auth
  response = HTTParty.get(@sections_uri, basic_auth)
  unless response.code == 200
    puts "Error #{response.code}: #{response.message}"
    abort
  end
end

#export_columnsObject



64
65
66
# File 'lib/zentool/zendesk_article.rb', line 64

def export_columns
  %w(id category section title word_count draft promoted outdated html_url created_at updated_at)
end

#raw_articlesObject



52
53
54
# File 'lib/zentool/zendesk_article.rb', line 52

def raw_articles
  @raw_articles ||= HTTParty.get(@articles_uri, basic_auth)
end

#raw_categoriesObject



60
61
62
# File 'lib/zentool/zendesk_article.rb', line 60

def raw_categories
  @raw_sections ||= HTTParty.get(@categories_uri, basic_auth)
end

#raw_sectionsObject



56
57
58
# File 'lib/zentool/zendesk_article.rb', line 56

def raw_sections
  @raw_sections ||= HTTParty.get(@sections_uri, basic_auth)
end

#sectionsObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zentool/zendesk_article.rb', line 26

def sections
  @sections ||= begin
    progressbar = ProgressBar.create(title: "#{raw_sections['count']} Sections", starting_at: 1, format: '%a |%b>>%i| %p%% %t', total: raw_sections['page_count'])
    sections = raw_sections['sections']

    (raw_sections['page_count'] - 1).times do |page|
      progressbar.increment
      sections += HTTParty.get("#{@sections_uri}?page=#{page + 2}", basic_auth)['sections']
    end
  end
  sections
end