jr

所属分类:WEB开发
开发工具:Ruby
文件大小:0KB
下载次数:0
上传日期:2023-11-03 08:02:47
上 传 者sh-1993
说明:  Rubysets的类jq JSON处理器
(jq like JSON processor for Rubyists)

文件列表:
.dockerignore (54, 2023-12-26)
.prettierrc.json (177, 2023-12-26)
.travis.yml (192, 2023-12-26)
CHANGELOG.md (2421, 2023-12-26)
Dockerfile (164, 2023-12-26)
Gemfile (91, 2023-12-26)
Guardfile (1159, 2023-12-26)
LICENSE.txt (1070, 2023-12-26)
Rakefile (337, 2023-12-26)
bin/ (0, 2023-12-26)
bin/jr (2960, 2023-12-26)
cucumber.yml (25, 2023-12-26)
features/ (0, 2023-12-26)
features/core_ext.feature (1302, 2023-12-26)
features/json_processing.feature (7642, 2023-12-26)
features/step_definitions/ (0, 2023-12-26)
features/step_definitions/command_steps.rb (150, 2023-12-26)
features/support/ (0, 2023-12-26)
features/support/env.rb (25, 2023-12-26)
jr-cli.gemspec (1374, 2023-12-26)
lib/ (0, 2023-12-26)
lib/jr/ (0, 2023-12-26)
lib/jr/cli/ (0, 2023-12-26)
lib/jr/cli/core_ext.rb (105, 2023-12-26)
lib/jr/cli/core_ext/ (0, 2023-12-26)
lib/jr/cli/core_ext/enumerable.rb (360, 2023-12-26)
lib/jr/cli/core_ext/enumerator.rb (119, 2023-12-26)
lib/jr/cli/core_ext/hash.rb (481, 2023-12-26)
lib/jr/cli/version.rb (55, 2023-12-26)
renovate.json5 (661, 2023-12-26)
resources/ (0, 2023-12-26)
resources/img/ (0, 2023-12-26)
resources/img/jr.gif (537533, 2023-12-26)
... ...

# jr: command-line JSON processor for Rubyists jr is [jq](http://stedolan.github.io/jq/) like JSON processor. Its script can be written not a specific language but Ruby! ![jr in action](./resources/img/jr.gif) ## Installation Add this line to your application's Gemfile: gem 'jr-cli' And then execute: $ bundle Or install it yourself as: $ gem install jr-cli ## Usage jr filter can be written in Ruby! ``` $ jr [options] [file...] ``` You can also read JSON not from files but from STDIN. ### options ``` --require FILE require the FILE before execution -c, --compact-output output each JSON in single line -f, --from-file FILE read filter from file -r, --raw-output output strings as raw output -R, --raw-input read each line as string -C, --color-output output with colors even if writing to a pipe or a file -M, --monochrome-output output without colors -n, --null-input use null as input instead of any files --unbuffered output each JSON without buffering ``` ## jr filter tutorial Let's process JSON of GitHub API! At first, download JSON of repos into your local to avoid API rate limit. ``` $ curl -s 'https://api.github.com/users/yuya-takeyama/repos?per_page=100' > repos.json ``` ### Unwrap Array with `Enumerable#unwrap` Because response from `GET /users/:username/repos` is wrapped with `Array`, unwrap it using `Enumerable#unwrap`. It's a built-in method of jr. You'll get stream of JSON reperesents repositories. ``` $ jr 'unwrap' repos.json ``` ### Aggregate data with methods of `Enumerable` `Enumerable` has many useful methods and you can transform data with them. ``` $ jr 'unwrap.group_by(&:language).map{|k, v| [k, v.size] }.sort_by{|k, v| -v }' repos.json [ "Ruby", 28 ] [ "PHP", 22 ] [ "Go", 17 ] (...omitted...) [ "VimL", 1 ] [ "CoffeeScript", 1 ] [ "Perl", 1 ] ``` ### Output as text You can transform JSONs into String and output as raw text using `-r` option. ``` $ jr 'unwrap.group_by(&:language).map{|k, v| [k, v.size] }.sort_by{|k, v| -v }.map{|l, s| "#{s}\t#{l}" }' -r repos.json 28 Ruby 22 PHP 17 Go 12 JavaScript 11 3 CSS 2 Shell 2 C 1 VimL 1 CoffeeScript 1 Perl ``` ## Basic mechanism It's helpful to know jr's basic mechanism. Shortly, jr is implemented like below. ```ruby json_enumerator.instance_eval { your_jr_filter_runs_here } # And print its result ``` ## Contributing 1. Fork it ( https://github.com/yuya-takeyama/jr/fork ) 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create a new Pull Request

近期下载者

相关文件


收藏者