├── .github └── workflows │ └── test.yml ├── .gitignore ├── Gemfile ├── Gemfile.lock ├── LICENSE.txt ├── README.md ├── index.html ├── package.json ├── resources ├── css │ ├── color-brewer.css │ └── common.css └── js │ ├── curl-to-ruby.js │ └── default.js ├── src ├── curl-to-ruby.js ├── curlToRuby.js ├── default.js ├── highlight.pack.js ├── jsonToRuby.js └── parseCommand.js ├── test.rb ├── webpack.config.js └── yarn.lock /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | push: 5 | branches: 6 | - gh-pages 7 | pull_request: 8 | branches: 9 | - gh-pages 10 | 11 | jobs: 12 | test: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v1 16 | - name: Set up Ruby 2.6 17 | uses: actions/setup-ruby@v1 18 | with: 19 | ruby-version: 2.6.x 20 | - name: Set up Node.js 8.x 21 | uses: actions/setup-node@v1 22 | with: 23 | node-version: 8.x 24 | - name: Install dependencies 25 | run: | 26 | gem install bundler 27 | bundle install --jobs 4 --retry 3 28 | yarn 29 | - name: Build and run tests 30 | run: | 31 | yarn run compile 32 | bundle exec ruby test.rb 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | Thumbs.db 3 | _gitignore 4 | node_modules 5 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | gem 'execjs' 3 | gem 'execjs-fastnode' 4 | gem 'minitest' 5 | gem 'pry' 6 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | coderay (1.1.2) 5 | execjs (2.7.0) 6 | execjs-fastnode (0.2.1) 7 | execjs (~> 2.0) 8 | method_source (0.9.2) 9 | minitest (5.11.3) 10 | pry (0.12.2) 11 | coderay (~> 1.1.0) 12 | method_source (~> 0.9.0) 13 | 14 | PLATFORMS 15 | ruby 16 | 17 | DEPENDENCIES 18 | execjs 19 | execjs-fastnode 20 | minitest 21 | pry 22 | 23 | BUNDLED WITH 24 | 1.17.3 25 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Matthew Holt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | curl-to-ruby 2 | ============ 3 | 4 | curl-to-ruby is a tool to instantly convert [curl](http://curl.haxx.se) commands to ruby code using [net/http](http://ruby-doc.org/stdlib-2.1.1/libdoc/net/http/rdoc/Net/HTTP.html) in the browser. It does *not* guarantee high-fidelity conversions, but it's good enough for most API docs that have curl samples. 5 | 6 | ### Try it 7 | 8 | **[Check it out!](https://jhawthorn.github.io/curl-to-ruby)** It works inside your browser. 9 | 10 | 11 | ### FAQ 12 | 13 | #### Does any curl command work? 14 | 15 | Any curl command should work, but only certain flags are understood and converted into ruby code. The rest of the flags will be ignored. 16 | 17 | #### Which kinds of curl commands are understood? 18 | 19 | Mostly simple HTTP commands (headers, basic auth, body, etc). 20 | 21 | #### Will you consider supporting *this-or-that* flag? 22 | 23 | curl has like a bajillion options, so don't expect all of them to be implemented; just the most common/important ones to stub out code from API samples and docs, etc. But feel free to open an issue or submit a pull request! 24 | 25 | 26 | 27 | ### Credits 28 | 29 | Updated to ruby by John Hawthorn ([jhawthorn](https://twitter.com/jhawthorn)) 30 | 31 | Based on [curl-to-Go](https://github.com/mholt/curl-to-go) by Matt Holt ([mholt6](https://twitter.com/mholt6)). 32 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |
16 | Ruby's net/http
is notorious for not having the friendliest API, but it isn't all that bad.
17 | Ruby has great gems like faraday, but in libraries and small utilities it's better to kill your dependencies and use what the stdlib provides.
18 |
20 | This tool turns a curl command into ruby (2.0+) code using net/http
. Currently, it knows the following options: -d
/--data
, -H
/--header
, -I
/--head
, -u
/--user
, -k
/--insecure
, --url
, and -X
/--request
.
21 |
23 | There's probably bugs; please contribute on GitHub. Based on curl-to-go. 24 |
25 | 26 |27 | Simple · 28 | Basic Auth · 29 | JSON · 30 | Complex JSON · 31 | Form Data 32 |
33 | 34 |0)a(s.substring(0,s.indexOf("=")),s.substring(s.indexOf("=")+1,s.length));else a(s,t.shift());else o(s)?r[s]=!0:s.length>1?a(s[0],s.substring(1,s.length)):a(s[0],t.shift());o(s)&&(r[s]=!0)}else a("_",s)}return r;function o(e){if(Array.isArray(n.boolFlags))for(var t=0;t
":l.tabReplace?n.replace(/\t/g,l.tabReplace):""}):e}function w(e){var n,r,a,i,c,u=function(e){var n,t,r,a,s=e.className+" ";if(s+=e.parentNode?e.parentNode.className:"",t=o.exec(s))return R(t[1])?t[1]:"no-highlight";for(n=0,r=(s=s.split(/\s+/)).length;n