From eeece0ad2653612d729b5e196c49994ce7933bd8 Mon Sep 17 00:00:00 2001 From: Shohei Maeda Date: Thu, 12 Dec 2019 02:05:11 +0900 Subject: [PATCH 1/5] Add -j (--json-pretty) option --- lib/twurl.rb | 1 + lib/twurl/cli.rb | 7 +++++++ lib/twurl/request_controller.rb | 13 ++++++++++++- test/request_controller_test.rb | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/twurl.rb b/lib/twurl.rb index 124a2a0..0f50089 100644 --- a/lib/twurl.rb +++ b/lib/twurl.rb @@ -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| diff --git a/lib/twurl/cli.rb b/lib/twurl/cli.rb index 0872547..c88ce7d 100644 --- a/lib/twurl/cli.rb +++ b/lib/twurl/cli.rb @@ -87,6 +87,7 @@ def parse_options(args) file filefield base64 + json_format timeout connection_timeout end @@ -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 diff --git a/lib/twurl/request_controller.rb b/lib/twurl/request_controller.rb index 71c9d5f..2da33b6 100644 --- a/lib/twurl/request_controller.rb +++ b/lib/twurl/request_controller.rb @@ -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 @@ -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 diff --git a/test/request_controller_test.rb b/test/request_controller_test.rb index 92427e0..39027ce 100644 --- a/test/request_controller_test.rb +++ b/test/request_controller_test.rb @@ -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 } From 56b424079b4653583c4fc9c5c4aeecef2f2ba309 Mon Sep 17 00:00:00 2001 From: Shohei Maeda Date: Thu, 12 Dec 2019 02:51:06 +0900 Subject: [PATCH 2/5] Fix test not all machines have the "TMPDIR" env variable set by default (e.g., Chrome OS) and hence Minitest actually removes user's "~/.twurlrc" file if the "TMPDIR" is not set. To remediate this, override the directory path in case if it's missing so running tests don't remove the user's file by accident. --- test/test_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index e22e451..1ba7179 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,7 +9,7 @@ require 'minitest/autorun' require 'rr' -Twurl::RCFile.directory = ENV['TMPDIR'] +Twurl::RCFile.directory = ENV['TMPDIR'] || File.dirname(__FILE__) module Twurl class Options From 7f0f5b13b1a04b4e6e793857d9004d9b7cc36043 Mon Sep 17 00:00:00 2001 From: Shohei Maeda Date: Thu, 12 Dec 2019 04:39:35 +0900 Subject: [PATCH 3/5] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5ee79fe..d01e512 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,4 @@ test/version_tmp tmp tmtags tramp +vendor From e378c75236d86873f15f2849dfb044556f7f857f Mon Sep 17 00:00:00 2001 From: smaeda-ks Date: Sat, 14 Dec 2019 02:42:35 +0900 Subject: [PATCH 4/5] Update INSTALL remove outdated info and use Markdown --- INSTALL | 23 ----------------------- INSTALL.md | 36 ++++++++++++++++++++++++++++++++++++ twurl.gemspec | 2 +- 3 files changed, 37 insertions(+), 24 deletions(-) delete mode 100644 INSTALL create mode 100644 INSTALL.md diff --git a/INSTALL b/INSTALL deleted file mode 100644 index 106127f..0000000 --- a/INSTALL +++ /dev/null @@ -1,23 +0,0 @@ -+-----------------------+ -| Install with RubyGems | -+-----------------------+ - - sudo gem i twurl --source http://rubygems.org - -+---------------------+ -| Build from source | -+---------------------+ - - rake build - -+---------------------+ -| Install from source | -+---------------------+ - - rake install - -+--------------+ -| Dependencies | -+--------------+ - - sudo gem i oauth diff --git a/INSTALL.md b/INSTALL.md new file mode 100644 index 0000000..55e2974 --- /dev/null +++ b/INSTALL.md @@ -0,0 +1,36 @@ +# Install + +## Install with RubyGems (recommended) + +```sh +# installing the latest release +$ gem install twurl +``` + +```sh +# verify installation +$ twurl -v +0.9.4 +``` + +## Install from source + +In case if you haven't installed `bundler` you need to install it first: + +```sh +$ gem install bundler +``` + +```sh +$ git clone https://github.com/twitter/twurl . +$ cd twurl +$ bundle install +``` + +If you don't want to install Twurl globally on your system, use `--path` [option](https://bundler.io/v2.0/bundle_install.html): + +``` +$ bundle install --path path/to/directory +$ bundle exec twurl -v +0.9.4 +``` diff --git a/twurl.gemspec b/twurl.gemspec index be27b30..9fa2c52 100644 --- a/twurl.gemspec +++ b/twurl.gemspec @@ -9,7 +9,7 @@ Gem::Specification.new do |spec| spec.description = %q{Curl for the Twitter API} spec.email = ['marcel@twitter.com'] spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } - spec.extra_rdoc_files = %w(CODE_OF_CONDUCT.md INSTALL LICENSE README.md) + spec.extra_rdoc_files = %w(CODE_OF_CONDUCT.md INSTALL.md LICENSE README.md) spec.files = `git ls-files -z`.split("\x0").reject { |f| f.start_with?('test/') } spec.homepage = 'http://github.com/twitter/twurl' spec.licenses = ['MIT'] From 146b8d4e98eaa0852262fccef93472f1733c7521 Mon Sep 17 00:00:00 2001 From: smaeda-ks Date: Tue, 17 Dec 2019 05:13:18 +0900 Subject: [PATCH 5/5] Update INSTALL.md --- INSTALL.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/INSTALL.md b/INSTALL.md index 55e2974..89404c8 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -22,7 +22,7 @@ $ gem install bundler ``` ```sh -$ git clone https://github.com/twitter/twurl . +$ git clone https://github.com/twitter/twurl $ cd twurl $ bundle install ```