Module: FCI

Defined in:
lib/fci/init.rb,
lib/fci/export.rb,
lib/fci/import.rb,
lib/fci/version.rb,
lib/fci/download.rb

Constant Summary collapse

VERSION =
'0.0.6'

Instance Method Summary collapse

Instance Method Details

#build_article_hash(article) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fci/import.rb', line 54

def build_article_hash(article)
  attr = article.attributes

  return {
    id:          article.id,
    folder_id:   attr[:folder_id],
    position:    attr[:position],
    title:       attr[:title],
    description: attr[:description],
  }
end

#build_article_xml(article) ⇒ Object



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

def build_article_xml(article)
  attr = article.attributes

  article_xml = Nokogiri::XML::Builder.new do |xml|
    xml.root {
      # id - id of the original acticle
      # folder_id - id of the original folder
      xml.article(id: article.id, folder_id: attr[:folder_id], position: attr[:position], identifier: 'article', type: 'document') {
        xml.title {
          xml.cdata attr[:title]
        }
        xml.description {
          xml.cdata attr[:description]
        }
      }
    }
  end

  return article_xml
end

#build_folder_hash(folder) ⇒ Object



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

def build_folder_hash(folder)
  attr = folder.attributes

  return {
    id:          folder.id,
    position:    attr[:position],
    name:        attr[:name],
    description: attr[:description],
  }
end

#build_folder_xml(folder) ⇒ Object



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

def build_folder_xml(folder)
  attr = folder.attributes

  folder_xml = Nokogiri::XML::Builder.new do |xml|
    xml.root {
      # id - id of the original folder
      xml.folder(id: folder.id, position: attr[:position], identifier: 'folder', type: 'document') {
        xml.name {
          xml.cdata attr[:name]
        }
        xml.description {
          xml.cdata attr[:description]
        }
      }
    }
  end

  return folder_xml
end

#create_scaffold(root_dir, project_name, force) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/fci/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/fci/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

#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/fci/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  # Freshdesk API credentials\n  freshdesk_base_url: 'https://<%subdomain%>.freshdesk.com'\n  freshdesk_username: '<%your-freshdesk-username%>'\n  freshdesk_password: '<%your-freshdesk-password%>'\n\n  # Freshdesk catogories\n  categories:\n  - freshdesk_category: '<%freshdesk-category-id%>'\n    translations:\n      -\n        crowdin_language_code: '<%crowdin-two-letters-code%>'\n        freshdesk_category_id: '<%freshdesk-category-id%>'\n      -\n        crowdin_language_code: '<%crowdin-two-letters-code%>'\n        freshdesk_category_id: '<%freshdesk-category-id%>'\n  - freshdesk_category: '<%freshdesk-category-id%>'\n    translations:\n      -\n        crowdin_language_code: '<%crowdin-two-letters-code%>'\n        freshdesk_category_id: '<%freshdesk-category-id%>'\n      -\n        crowdin_language_code: '<%crowdin-two-letters-code%>'\n        freshdesk_category_id: '<%freshdesk-category-id%>'\n  EOS\n\n  File.open(\"\#{root_dir}/\#{project_name}/fci.yml\", 'w') do |file|\n    file << config\n  end\n\n  puts \"Created \#{root_dir}/\#{project_name}/fci.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/fci/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(article_xml_file) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fci/export.rb', line 16

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

  article = {
    id: article_xml[:id],
    position: article_xml[:position],
    folder_id: article_xml[:folder_id],
    title: article_xml.xpath('title').text,
    description: article_xml.xpath('description').text,
  }

  return article
end

#parse_folder_xml(folder_xml_file) ⇒ Object



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

def parse_folder_xml(folder_xml_file)
  doc = Nokogiri::XML.parse(folder_xml_file)
  folder_xml = doc.xpath("//folder").first

  folder = {
    id: folder_xml[:id],
    name: folder_xml.xpath('name').text,
    description: folder_xml.xpath('description').text,
    position: folder_xml[:position],
  }

  return folder
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/fci/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