Skip to content

Commit

Permalink
Add -j (--json-pretty) option (#130)
Browse files Browse the repository at this point in the history
* Add -j (--json-pretty) option

* 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.

* Update .gitignore

* Update INSTALL

remove outdated info and use Markdown

* Update INSTALL.md
  • Loading branch information
smaeda-ks authored Dec 16, 2019
1 parent d082264 commit f81ec26
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ test/version_tmp
tmp
tmtags
tramp
vendor
23 changes: 0 additions & 23 deletions INSTALL

This file was deleted.

36 changes: 36 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
@@ -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
```
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
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion twurl.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
spec.description = %q{Curl for the Twitter API}
spec.email = ['[email protected]']
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']
Expand Down

0 comments on commit f81ec26

Please sign in to comment.