Skip to content

Commit 95d8baa

Browse files
committed
add a json output option
1 parent 5589664 commit 95d8baa

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

lib/gem_dating/cli.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Cli
1414
--older-than=<AGE>, --ot=<AGE> Filter gems updated within the last X (e.g. 2y, 1m, 4w, 10d)
1515
--sort-by=<FIELD> Sort by field (name or date), defaults to name
1616
--order=<DIRECTION> Sort direction (asc or desc), defaults to asc
17+
--json Output results as JSON
1718
HELP
1819

1920
def initialize(argv = [])
@@ -42,7 +43,9 @@ def run
4243
end
4344
end
4445

45-
$stdout << GemDating.from_file(@file_path, @options).table_print << "\n"
46+
result = GemDating.from_file(@file_path, @options)
47+
output = @options[:json] ? result.to_json : result.table_print
48+
$stdout << output << "\n"
4649

4750
SUCCESS
4851
end
@@ -52,6 +55,7 @@ def run
5255
def parse_args(args = @args)
5356
options = {}
5457
options[:help] = true if (args & %w[-h --help -?]).any?
58+
options[:json] = true if args.include?("--json")
5559

5660
if (older_than = args.find { |arg| arg.start_with?("--older-than=", "--ot=") })
5761
options[:older_than] = older_than.split("=").last

lib/gem_dating/result.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "table_print"
2+
require "json"
23

34
module GemDating
45
class Result
@@ -25,6 +26,10 @@ def table_print
2526
TablePrint::Printer.table_print(specs, [:name, :version, {date: {time_format: "%Y-%m-%d", width: 10}}]).encode("utf-8")
2627
end
2728

29+
def to_json
30+
JSON.generate(to_h)
31+
end
32+
2833
def older_than(date)
2934
specs.select! { |spec| spec.date.to_date < self.cut_off(date) }
3035
self

test/cli_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ def test_parse_args
114114
cli = GemDating::Cli.new(["--help", "--older-than=2y"])
115115
assert_equal({ help: true, older_than: "2y" }, cli.send(:parse_args))
116116

117+
cli = GemDating::Cli.new(["--json"])
118+
assert_equal({ json: true }, cli.send(:parse_args))
119+
117120
cli = GemDating::Cli.new([])
118121
assert_equal({}, cli.send(:parse_args))
119122
end
@@ -155,4 +158,33 @@ def test_sort_by_date_desc
155158
assert_equal 0, exit_code
156159
assert_equal expected_out, stdout
157160
end
161+
162+
def test_json_output
163+
exit_code = nil
164+
165+
stdout, _stderr = capture_io do
166+
exit_code = GemDating::Cli.new(["test/Gemfile.example", "--json"]).run
167+
end
168+
169+
expected_data = {
170+
"banana-client" => {
171+
"name" => "banana-client",
172+
"version" => "21.1.0",
173+
"date" => "1990-08-21"
174+
},
175+
"giraffeql" => {
176+
"name" => "giraffeql",
177+
"version" => "0.0.2227",
178+
"date" => "2023-05-17"
179+
},
180+
"rails-on-rubies" => {
181+
"name" => "rails-on-rubies",
182+
"version" => "70.0.5",
183+
"date" => "2123-05-24"
184+
}
185+
}
186+
187+
assert_equal 0, exit_code
188+
assert_equal expected_data, JSON.parse(stdout)
189+
end
158190
end

test/result_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ def test_hash
5858
assert_equal expected, @result.to_h
5959
end
6060

61+
def test_json
62+
expected = {
63+
"hi" => {
64+
"name" => "hi",
65+
"version" => "42.42",
66+
"date" => "2015-09-18"
67+
},
68+
"there" => {
69+
"name" => "there",
70+
"version" => "1.27.0.01",
71+
"date" => "2009-09-02"
72+
}
73+
}
74+
assert_equal expected, JSON.parse(@result.to_json)
75+
end
76+
6177
def test_table
6278
expected = <<-TEXT
6379
NAME | VERSION | DATE

0 commit comments

Comments
 (0)