Module: ZCI

Defined in:
lib/zci/init.rb,
lib/zci/clean.rb,
lib/zci/export.rb,
lib/zci/import.rb,
lib/zci/helpers.rb,
lib/zci/version.rb,
lib/zci/download.rb

Constant Summary collapse

VERSION =
'0.11.0'

Instance Method Summary collapse

Instance Method Details

#build_article_hash(article) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/zci/import.rb', line 74

def build_article_hash(article)
  {
    id:         article.id,
    section_id: article.section_id,
    position:   article.position,
    title:      article.title,
    body:       article.body,
  }
end

#build_article_xml(article) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/zci/import.rb', line 53

def build_article_xml(article)
  # remove control chars, unicode codepoints from 0001 to 001A
  article.title.to_s.gsub!(/[\u0001-\u001A]/ , '')
  article.body.to_s.gsub!(/[\u0001-\u001A]/ , '')

  Nokogiri::XML::Builder.new do |xml|
    xml.root {
      # id - id of the original acticle
      # section_id - id of the original section
      xml.article(id: article.id, section_id: article.section_id, position: article.position, identifier: 'article', type: 'document') {
        xml.title {
          xml.cdata article.title
        }
        xml.body {
          xml.cdata article.body
        }
      }
    }
  end
end

#build_category_hash(category) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/zci/import.rb', line 17

def build_category_hash(category)
  {
    id:          category.id,
    position:    category.position,
    name:        category.name,
    description: category.description,
  }
end

#build_category_xml(category) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/zci/import.rb', line 2

def build_category_xml(category)
  Nokogiri::XML::Builder.new do |xml|
    xml.root {
      xml.category(id: category.id, position: category.position, identifier: 'category', type: 'document') {
        xml.name {
          xml.cdata category.name
        }
        xml.description {
          xml.cdata category.description
        }
      }
    }
  end
end

#build_section_hash(section) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/zci/import.rb', line 43

def build_section_hash(section)
  {
    id:          section.id,
    category_id: section.category_id,
    position:    section.position,
    name:        section.name,
    description: section.description,
  }
end

#build_section_xml(section) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/zci/import.rb', line 26

def build_section_xml(section)
  Nokogiri::XML::Builder.new do |xml|
    xml.root {
      # id - id of the original section
      # category_id - id of the original category
      xml.section(id: section.id, category_id: section.category_id, position: section.position, identifier: 'section', type: 'document') {
        xml.name {
          xml.cdata section.name
        }
        xml.description {
          xml.cdata section.description
        }
      }
    }
  end
end

#create_scaffold(root_dir, project_name, force) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/zci/init.rb', line 4

def create_scaffold(root_dir, project_name, force)
  dir = File.join(root_dir, project_name)

  if mkdir(dir, force)
    mk_config(root_dir, project_name)
  end
end

#export_translations!(crowdin) ⇒ Object

use export API method before to download the most recent translations



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/zci/download.rb', line 18

def export_translations!(crowdin)
  print 'Building ZIP archive with the latest translations '
  export_translations = crowdin.export_translations
  if export_translations['success']
    if export_translations['success']['status'] == 'built'
      puts "- OK"
    elsif export_translations['success']['status'] == 'skipped'
      puts "- Skipped"
      puts "Warning: Export was skipped. Please note that this method can be invoked only once per 30 minutes."
    end
  end
end

#initialize_zendesk_client(base_url, username, password, verbose) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/zci/helpers.rb', line 9

def initialize_zendesk_client(base_url, username, password, verbose)
  ZendeskAPI::Client.new do |config|
    config.url       = base_url
    config.username  = username
    config.password  = password

    if verbose
      require 'logger'
      config.logger = Logger.new(STDOUT)
    end

    config.client_options = {ssl: {verify: false}}
  end
end

#mk_config(root_dir, project_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/zci/init.rb', line 32

def mk_config(root_dir, project_name)
  config = "  ---\n  # Crowdin API credentials\n  crowdin_project_id: '<%your-crowdin-project-id%>'\n  crowdin_api_key: '<%your-crowdin-api-key%>'\n  crowdin_base_url: 'https://api.crowdin.com'\n\n  # Zendesk API credentials\n  zendesk_base_url: 'https://<%subdomain%>.zendesk.com/api/v2/'\n  zendesk_username: '<%your-zendesk-username%>'\n  zendesk_password: '<%your-zendesk-password%>'\n\n  # Zendesk catogories\n  categories:\n  - zendesk_category: '<%zendesk-category-id%>'\n    translations:\n      -\n        crowdin_language_code: '<%crowdin-language-code%>' # the full list: https://support.crowdin.com/api/language-codes/\n        zendesk_locale: '<%zendesk-locale%>' # the full list: https://support.zendesk.com/hc/en-us/articles/203761906-Language-codes-for-Zendesk-supported-languages\n      -\n        crowdin_language_code: '<%crowdin-language-code%%>'\n        zendesk_locale: '<%zendesk-locale%>'\n  - zendesk_category: '<%zendesk-category-id%>'\n    translations:\n      -\n        crowdin_language_code: '<%crowdin-language-code%%>'\n        zendesk_locale: '<%zendesk-locale%>'\n      -\n        crowdin_language_code: '<%crowdin-language-code%%>'\n        zendesk_locale: '<%zendesk-locale%>'\n  EOS\n\n  File.open(\"\#{root_dir}/\#{project_name}/zci.yml\", 'w') do |file|\n    file << config\n  end\n\n  puts \"Created \#{root_dir}/\#{project_name}/zci.yml\"\nend\n".strip_heredoc

#mkdir(dir, force) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/zci/init.rb', line 12

def mkdir(dir, force)
  exists = false
  if !force
    if File.exist? dir
      raise "#{dir} exists; use --force to override"
      exists = true
    end
  end

  if !exists
    puts "Creating dir #{dir}..."
    FileUtils.mkdir_p dir
  else
    puts "Exiting..."
    false
  end

  true
end

#parse_article_xml(xml_file) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/zci/export.rb', line 27

def parse_article_xml(xml_file)
  doc = Nokogiri::XML.parse(xml_file)
  article_xml = doc.xpath('//article').first

  {
    id: article_xml[:id],
    section_id: article_xml[:section_id],
    title: article_xml.xpath('title').text,
    body: article_xml.xpath('body').text,
    position: article_xml[:position],
  }
end

#parse_category_xml(xml_file) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/zci/export.rb', line 2

def parse_category_xml(xml_file)
  doc = Nokogiri::XML.parse(xml_file)
  category_xml = doc.xpath("//category").first

  {
    id: category_xml[:id],
    name: category_xml.xpath('name').text,
    description: category_xml.xpath('description').text,
    position: category_xml[:position],
  }
end

#parse_section_xml(xml_file) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/zci/export.rb', line 14

def parse_section_xml(xml_file)
  doc = Nokogiri::XML.parse(xml_file)
  section_xml = doc.xpath("//section").first

  {
    id: section_xml[:id],
    category_id: section_xml[:category_id],
    name: section_xml.xpath('name').text,
    description: section_xml.xpath('description').text,
    position: section_xml[:position],
  }
end

#remove_dir(path) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/zci/clean.rb', line 2

def remove_dir(path)
  if File.exists?(path)
    if File.directory?(path)
      Dir.foreach(path) do |file|
        if (file.to_s != ".") && (file.to_s != "..")
          remove_dir("#{path}/#{file}")
        end
      end
      puts "Del `#{path}`"
      Dir.delete(path)
    else
      puts "Del `#{path}`"
      File.delete(path)
    end
  else
    puts "No such directory - `#{path}`"
  end
end

#unzip_file_with_translations(zipfile_name, dest_path) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/zci/download.rb', line 2

def unzip_file_with_translations(zipfile_name, dest_path)
  # overwrite files if they already exist inside of the extracted path
  Zip.on_exists_proc = true

  Zip::File.open(zipfile_name) do |zip_file|
    zip_file.select { |zip_entry| zip_entry.file? }.each do |f|
      # `f' - relative path in archive
      fpath = File.join(dest_path, f.name)
      FileUtils.mkdir_p(File.dirname(fpath))
      puts "Extracting: `#{dest_path}/#{f.name}'"
      zip_file.extract(f, fpath)
    end
  end
end