Skip to content

Commit

Permalink
Fix(curlify.rb): Generate curl command correctly (#3)
Browse files Browse the repository at this point in the history
* 🔧 fix(curlify_spec.rb): fix indentation and formatting issues in curlify_spec.rb
✨ feat(curlify_spec.rb): refactor tests to use shared response variable for better readability and maintainability

* 🐛 fix(curlify.rb): wrap request body in single quotes to handle special characters properly
✨ feat(curlify_spec.rb): add support for POST, PUT, and DELETE requests in curlify gem
📝 docs(README.md): update usage example to include POST request and response output
🔧 chore(curlify.gemspec): bump version to 1.0.1
  • Loading branch information
marcuxyz authored Sep 2, 2023
1 parent b87b970 commit 9fa55d4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 36 deletions.
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```
2 changes: 1 addition & 1 deletion curlify.gemspec
Original file line number Diff line number Diff line change
@@ -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']
Expand Down
2 changes: 1 addition & 1 deletion lib/curlify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
63 changes: 33 additions & 30 deletions spec/curlify_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9fa55d4

Please sign in to comment.