Module: JsonCompacker

Defined in:
lib/json_compacker.rb,
lib/json_compacker/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.get_file_as_string(filename) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/json_compacker.rb', line 36

def self.get_file_as_string(filename)
  data = ''
  f = File.open(filename, "r") 
  f.each_line do |line|
    data += line
  end
  return data
end

.mainObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/json_compacker.rb', line 45

def self.main
  parse_argv ARGV

  src_path = @options[:src_path]
  output_file = @options[:output_file]

  output_json_obj = {}
  Dir.foreach(src_path) { |filename|
    if (filename == '.' || filename == '..' || filename == '.DS_Store') then
      next
    end

    content = get_file_as_string(src_path + '/' + filename)
    json = JSON.parse(content)

    base_filename = File.basename(filename, File.extname(filename)) 
    output_json_obj[base_filename] = json
  }

  open(output_file, 'w') { |f|
    f.puts JSON.dump(output_json_obj)
  }
end

.parse_argv(argv) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/json_compacker.rb', line 10

def self.parse_argv(argv)
  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: ./json_compacker -s src_path -o output_file"

    opts.on("-s", "--src_path=SRC_PATH", "define the src path where are json files") do |v|
      @options[:src_path] = v
    end

    opts.on("-o", "--output_file=OUTPUT_FILE", "define the output file compacked") do |v|
      @options[:output_file] = v
    end

    opts.on("-h", "--help", "Prints this help") do
      puts opts
      exit
    end
  end

  opt_parser.parse! argv

  if !@options[:src_path] or !@options[:output_file] then
    opt_parser.parse! ['--help']
  end

end