Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .babelish.sample
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ langs: # Languages to convert. i.e. English:en
# ignore_lang_path: true # does not care about lang component path. i.e: en.lproj/
# sheet: 0 # Index of worksheet to download. First index is 0.
# macros_filename: Babelish.h # File to output the defines of localized strings
# stripping: false # Strips values inside the spreadsheet
# csv_separator: ',' # CSV column separator character, uses ',' by default
# stripping: false # Strips values inside the spreadsheet
# csv_separator: ',' # CSV column separator character, uses ',' by default
# pretty_json: false # Prettify your json output files
1 change: 1 addition & 0 deletions lib/babelish/commandline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Commandline < Thor
method_option :ignore_lang_path, :type => :boolean, :aliases => "-I", :lazy_default => false, :desc => "Ignore the path component of langs"
method_option :fetch, :type => :boolean, :desc => "Download file from Google Drive"
method_option :sheet, :type => :numeric, :desc => "Index of worksheet to download. First index is 0"
method_option :pretty_json, :type => :boolean, aliases: "-p", desc: "Prettify your json output files"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.
Metrics/LineLength: Line is too long. [105/80]

if klass[:name] == "CSV2Strings"
method_option :macros_filename, :type => :boolean, :aliases => "-m", :lazy_default => false, :desc => "Filename containing defines of localized keys"
end
Expand Down
7 changes: 6 additions & 1 deletion lib/babelish/csv2json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ module Babelish
require 'json'
class CSV2JSON < Csv2Base

def initialize(filename, langs, args = {})
super
@pretty_json = args[:pretty_json]
end

def language_filepaths(language)
require 'pathname'
filename = @output_basename || language.code
Expand All @@ -11,7 +16,7 @@ def language_filepaths(language)
end

def hash_to_output(content = {})
return content.to_json
@pretty_json ? JSON.pretty_generate(content) : content.to_json
end

def extension
Expand Down
36 changes: 36 additions & 0 deletions test/babelish/test_csv2json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,40 @@ def test_converting_csv_to_dotstrings_one_output_option
# clean up
system("rm -rf ./" + single_file)
end

def test_converting_csv_to_json_with_unpretty_json
csv_file = "test/data/test_data.csv"
expected_json_filename = "test_unpretty_json.json"
given_json_filename = "output.json"

expected_json = File.read("test/data/" + expected_json_filename)
converter = Babelish::CSV2JSON.new(csv_file,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/TrailingWhitespace: Trailing whitespace detected.

{ "English" => "en" },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/AlignParameters: Align the parameters of a method call if they span more than one line.
Layout/TrailingWhitespace: Trailing whitespace detected.

output_basename: "output",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/AlignParameters: Align the parameters of a method call if they span more than one line.
Layout/TrailingWhitespace: Trailing whitespace detected.

pretty_json: false)
converter.convert
given_json = File.read(given_json_filename)
assert_equal(expected_json, given_json, "JSON file has incorrect format")

# clean up
system("rm -rf ./" + given_json_filename)
end

def test_converting_csv_to_json_with_pretty_json
csv_file = "test/data/test_data.csv"
expected_json_filename = "test_pretty_json.json"
given_json_filename = "output.json"

expected_json = File.read("test/data/" + expected_json_filename)
converter = Babelish::CSV2JSON.new(csv_file,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/TrailingWhitespace: Trailing whitespace detected.

{ "English" => "en" },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/AlignParameters: Align the parameters of a method call if they span more than one line.
Layout/TrailingWhitespace: Trailing whitespace detected.

output_basename: "output",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/AlignParameters: Align the parameters of a method call if they span more than one line.
Layout/TrailingWhitespace: Trailing whitespace detected.

pretty_json: true)
converter.convert
given_json = File.read(given_json_filename)
assert_equal(expected_json, given_json, "JSON file has incorrect format")

# clean up
system("rm -rf ./" + given_json_filename)
end
end
4 changes: 4 additions & 0 deletions test/data/test_pretty_json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ERROR_HANDLER_WARNING_DISMISS": "OK",
"ANOTHER_STRING": "hello"
}
1 change: 1 addition & 0 deletions test/data/test_unpretty_json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"ERROR_HANDLER_WARNING_DISMISS":"OK","ANOTHER_STRING":"hello"}