Skip to content

Commit

Permalink
Add -j (--json-pretty) option
Browse files Browse the repository at this point in the history
  • Loading branch information
smaeda-ks committed Dec 11, 2019
1 parent 03984a2 commit eeece0a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/twurl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'ostruct'
require 'stringio'
require 'yaml'
require 'json'

library_files = Dir[File.join(File.dirname(__FILE__), "/twurl/**/*.rb")].sort
library_files.each do |file|
Expand Down
7 changes: 7 additions & 0 deletions lib/twurl/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def parse_options(args)
file
filefield
base64
json_format
timeout
connection_timeout
end
Expand Down Expand Up @@ -327,6 +328,12 @@ def base64
end
end

def json_format
on('-j', '--json-pretty', 'Format response body to JSON pretty style') do |json_format|
options.json_format = true
end
end

def timeout
on('--timeout [sec]', Integer, 'Number of seconds to wait for the request to be read (default: 60)') do |timeout|
options.timeout = timeout
Expand Down
13 changes: 12 additions & 1 deletion lib/twurl/request_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def dispatch

def perform_request
client.perform_request_from_options(options) { |response|
response.read_body { |chunk| CLI.print chunk }
response.read_body { |body|
CLI.print options.json_format ? JsonFormatter.format(body) : body
}
}
rescue URI::InvalidURIError
CLI.puts NO_URI_MESSAGE
Expand All @@ -25,4 +27,13 @@ def perform_request
CLI.puts OPEN_TIMEOUT_MESSAGE
end
end

class JsonFormatter
def self.format(string)
json = JSON.parse(string)
(json.is_a?(Array) || json.is_a?(Hash)) ? JSON.pretty_generate(json) : string
rescue JSON::ParserError, TypeError
string
end
end
end
18 changes: 18 additions & 0 deletions test/request_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ def test_request_response_is_written_to_output
assert_equal expected_body, Twurl::CLI.output.string
end

def test_request_response_is_json_formatted
response_body = '{"data": {"text": "this is a fake response"}}'
expected_body = "{\n" \
" \"data\": {\n" \
" \"text\": \"this is a fake response\"\n" \
" }\n" \
"}"
custom_options = options
custom_options.json_format = true
response = Object.new
mock(response).read_body { |block| block.call response_body }
mock(client).perform_request_from_options(custom_options).times(1) { |custom_options, block| block.call(response) }

controller.perform_request

assert_equal expected_body, Twurl::CLI.output.string
end

def test_invalid_or_unspecified_urls_report_error
mock(Twurl::CLI).puts(Twurl::RequestController::NO_URI_MESSAGE).times(1)
mock(client).perform_request_from_options(options).times(1) { raise URI::InvalidURIError }
Expand Down

0 comments on commit eeece0a

Please sign in to comment.