diff --git a/README.md b/README.md index f324517..24d2238 100644 --- a/README.md +++ b/README.md @@ -16,16 +16,45 @@ $ bundle add curlify ## Usage -Import 'curlify', 'uri' and 'net/http' gems and execute curlify, see: +Import `curlify`, `uri` and `net/http` gems and execute curlify, see: ```python require 'uri' require 'net/http' require 'curlify' -uri = URI('https://run.mocky.io/v3/b0f4ffd8-6696-4f90-8bab-4a3bcad9ef3f') -request = Net::HTTP::Get.new(uri, { 'content-type': 'application/json' }) +uri = URI('https://httpbin.org/post') +request = Net::HTTP::Post.new(uri, { 'content-type': 'application/json' }) +request.body = { title: 'Ruby is great :)' }.to_json + curlify = Curlify.new(request) -puts curlify.to_curl # curl -X GET -H 'content-type: application/json' -H 'accept-encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3' -H 'accept: */*' -H 'user-agent: Ruby' -H 'host: run.mocky.io' https://run.mocky.io/v3/b0f4ffd8-6696-4f90-8bab-4a3bcad9ef3f +puts curlify.to_curl + +# curl -X POST -H 'content-type: application/json' -H 'accept-encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3' -H 'accept: */*' -H 'user-agent: Ruby' -H 'host: httpbin.org' -d '{"title":"Ruby is great :)"}' https://httpbin.org/post +``` + +Running this command generated, we have the following result: + +```bash +{ + "args": {}, + "data": "{\"title\":\"Ruby is great :)\"}", + "files": {}, + "form": {}, + "headers": { + "Accept": "*/*", + "Accept-Encoding": "gzip;q=1.0,deflate;q=0.6,identity;q=0.3", + "Content-Length": "28", + "Content-Type": "application/json", + "Host": "httpbin.org", + "User-Agent": "Ruby", + "X-Amzn-Trace-Id": "Root=1-64f38ad0-444dbe403f03082e14ba1d62" + }, + "json": { + "title": "Ruby is great :)" + }, + "origin": "xxx.xxx.xx.xx", + "url": "https://httpbin.org/post" +} ``` diff --git a/curlify.gemspec b/curlify.gemspec index 0318ea8..df09830 100644 --- a/curlify.gemspec +++ b/curlify.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'curlify' - s.version = '1.0.0' + s.version = '1.0.1' s.summary = 'Hola!' s.description = 'The gem convert python requests object in curl command.' s.authors = ['Marcus Almeida'] diff --git a/lib/curlify.rb b/lib/curlify.rb index 684ed95..072d6d2 100644 --- a/lib/curlify.rb +++ b/lib/curlify.rb @@ -27,6 +27,6 @@ def headers end def body - "-d #{request.body}" unless request.body.nil? + "-d '#{request.body}'" unless request.body.nil? end end diff --git a/spec/curlify_spec.rb b/spec/curlify_spec.rb index 1b517f0..7340042 100644 --- a/spec/curlify_spec.rb +++ b/spec/curlify_spec.rb @@ -7,51 +7,54 @@ describe Curlify do let(:context) { described_class.new(request) } let(:uri) { URI('http://127.0.0.1') } + let(:response) do + <<~CURL + curl -X #{request.method} \ + -H 'content-type: application/json' \ + -H 'accept-encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3' \ + -H 'accept: */*' \ + -H 'user-agent: Ruby' \ + -H 'host: 127.0.0.1' #{body} http://127.0.0.1 + CURL + end + + before { request.body = payload } describe 'must transform request into curl command' do - context '#get' do + context 'when is a GET request' do let(:request) { Net::HTTP::Get.new(uri, { 'content-type': 'application/json' }) } + let(:body) { nil } + let(:payload) { nil } - it 'should return curl command to GET request' do - expect(context.to_curl).to eq "curl -X GET -H 'content-type: application/json' -H 'accept-encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3' -H 'accept: */*' -H 'user-agent: Ruby' -H 'host: 127.0.0.1' http://127.0.0.1" - end + it { expect(context.to_curl).to eq response.strip } end - context '#post' do - let(:request) do - Net::HTTP::Post.new(uri, { 'content-type': 'application/json' }) - end + context 'when is a POST request' do + let(:request) { Net::HTTP::Post.new(uri, { 'content-type': 'application/json' }) } + let(:payload) { { name: 'John' }.to_json } + let(:body) { "-d '#{payload}'" } - before do - request.body = { name: 'John' }.to_json - end - - it 'should return curl command to Post request' do - expect(context.to_curl).to eq "curl -X POST -H 'content-type: application/json' -H 'accept-encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3' -H 'accept: */*' -H 'user-agent: Ruby' -H 'host: 127.0.0.1' -d {\"name\":\"John\"} http://127.0.0.1" - end + it { expect(context.to_curl).to eq response.strip } end - context '#put' do - let(:request) { Net::HTTP::Put.new(uri, { 'content-type': 'application/json' }) } + context 'when is a PUT request' do + let(:request) { Net::HTTP::Put.new(uri, { 'content-type': 'application/json' }) } + let(:payload) { { name: 'John', userId: 1 }.to_json } + let(:body) { "-d '#{payload}'" } - before do - request.body = { name: 'John', userId: 1 }.to_json - end + before { request.body = payload } - it 'should return curl command to Put request' do - expect(context.to_curl).to eq "curl -X PUT -H 'content-type: application/json' -H 'accept-encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3' -H 'accept: */*' -H 'user-agent: Ruby' -H 'host: 127.0.0.1' -d {\"name\":\"John\",\"userId\":1} http://127.0.0.1" - end + it { expect(context.to_curl).to eq response.strip } end - context '#delete' do + + context 'when is a DELETE request' do let(:request) { Net::HTTP::Delete.new(uri, { 'content-type': 'application/json' }) } + let(:body) { "-d '#{payload}'" } + let(:payload) { { userId: 1 }.to_json } - before do - request.body = { userId: 1 }.to_json - end + before { request.body = payload } - it 'should return curl command to Delete request' do - expect(context.to_curl).to eq "curl -X DELETE -H 'content-type: application/json' -H 'accept-encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3' -H 'accept: */*' -H 'user-agent: Ruby' -H 'host: 127.0.0.1' -d {\"userId\":1} http://127.0.0.1" - end + it { expect(context.to_curl).to eq response.strip } end end end