Class: International::MainApp

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

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ MainApp

Returns a new instance of MainApp.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/international.rb', line 10

def initialize(arguments)

  # defaults
  @path_to_output = 'output/'
  @path_to_csv = nil
  @dryrun = false
  @platform = 'android'

  # Parse Options
  arguments.push "-h" if arguments.length == 0
  create_options_parser(arguments)

  manage_opts

end

Instance Method Details

#create_options_parser(args) ⇒ Object

Options parser



31
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
71
# File 'lib/international.rb', line 31

def create_options_parser(args)

  args.options do |opts|
    opts.banner = "Usage: international [OPTIONS]"
    opts.separator  ''
    opts.separator  "Options"

    opts.on('-c PATH_TO_CSV', '--csv PATH_TO_CSV', 'Path to the .csv file') do |csv_path|
      @path_to_csv = csv_path
    end

    opts.on('-p PLATFORM', '--platform PLATFORM', 'Choose between "android" and "ios" (default: "android")') do |platform|
      @platform = platform.downcase
    end

    opts.on('-o PATH_TO_OUTPUT', '--output PATH_TO_OUTPUT', 'Path to the desired output folder') do |path_to_output|
      unless path_to_output[-1,1] == '/'
        path_to_output = "#{path_to_output}/"
      end

      @path_to_output = path_to_output
    end

    opts.on('-d', '--dryrun', 'Only simulates the output and don\'t write files') do |aa|
      @dryrun = true
    end

    opts.on('-h', '--help', 'Displays help') do
      @require_analyses = false
      puts opts.help
      exit
    end
    opts.on('-v', '--version', 'Displays version') do
      @require_analyses = false
      puts International::VERSION
      exit
    end
    opts.parse!

  end
end

#csv_to_hash(path_to_csv) ⇒ Object

CSV TO HASH



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/international.rb', line 91

def csv_to_hash(path_to_csv)
  file = File.open(path_to_csv, "rb")
  body = file.read

  body = "key#{body}" if body[0,1] == ','

  CSV::Converters[:blank_to_nil] = lambda do |field|
    field && field.empty? ? nil : field
  end

  csv = CSV.new(body, :headers => true, :header_converters => :symbol, :converters => [:all, :blank_to_nil])

  csv.to_a.map {|row| row.to_hash }
end

#is_valid_platformObject



26
27
28
# File 'lib/international.rb', line 26

def is_valid_platform
  @platform.eql?'android' or @platform.eql?'ios'
end

#manage_optsObject

Manage options



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/international.rb', line 74

def manage_opts

  unless @path_to_csv
    puts "Please give me a path to a CSV".yellow
    exit 1
  end

  unless is_valid_platform
    puts "The platform you chose could not be found, pick 'android' or 'ios'".yellow
    exit 1
  end

  hash = csv_to_hash(@path_to_csv)
  separate_languages hash
end

#separate_languages(all) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/international.rb', line 106

def separate_languages(all)
  languages = all.first.keys.drop(1)
  separated = Hash.new

  languages.each do |lang|
    items = Array.new
    all.each do |row|

      next if row.first.nil?

      item = {
        :key => row.first.last, # dem hacks
        :translation => row[lang]
      }

      items.push item
    end

    manager = FileManager.new lang, items, @path_to_output, @platform, @dryrun
    manager.create_file
  end
end