curlify 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +39 -8
  3. data/lib/curlify.rb +55 -8
  4. metadata +23 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f8603bf909c7334127104c8fab5861f012831d6fa81da078babdabbc57c4fd8
4
- data.tar.gz: '088456a7f8dacbbd4a9907117fc0d2fa804a4947e29f0de85d18860364be9c29'
3
+ metadata.gz: 76a3fea5ff081b12d6de36265e700ca9c6d527672b21be0cd1f4877682e6757b
4
+ data.tar.gz: 6ccab146f35e8649debeb1f04633bae4a32a9e50758247e5fc1926f595f0f9fd
5
5
  SHA512:
6
- metadata.gz: 11f6723c766d88aae5d04034bab585e699abd729282ab6f0b520bd177f7d8e519cb50eb03f2cd4de70303a3e5663e0b593f702378aac6aa267da82c7eb9dcb5d
7
- data.tar.gz: 2ff5487bba2d8491e1295be94f37dcef6fe1dff2a52b977c614d7dda06887774e73856b55ba812003e2f8d2cc6d502454649c5efce5bf0368becb3d6d9879224
6
+ metadata.gz: ffb9587d3dad61225f2b2c55498da7605f4a497cb12b35360346421ffa761eac3136b392e6e5db62a6bbf589a087c78284636642b2a08c8781aed367bd193cdf
7
+ data.tar.gz: 3456eb8c4efcd1f5321a7a8656e2fea962f5ed8461cae8ac837093166d91ab6bff0f2388db2bcd40abf63acc05b8ae10bac1e1d2059dc50d37637a5aa8907261
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- The gem convert ruby requests(net/http) into curl command.
1
+ The gem converts ruby requests to curl
2
2
 
3
3
  ## Installation
4
4
 
@@ -14,11 +14,27 @@ or bundle:
14
14
  $ bundle add curlify
15
15
  ```
16
16
 
17
- ## Usage
17
+ ## Usage with faraday
18
18
 
19
- Import `curlify`, `uri` and `net/http` gems and execute curlify, see:
19
+ Import `curlify` and `faraday` and perform curlify, see:
20
20
 
21
- ```python
21
+ ```ruby
22
+ require 'faraday'
23
+ require 'curlify'
24
+
25
+ request = Faraday.new.build_request(:post) do |req|
26
+ req.url 'http://127.0.0.1'
27
+ end
28
+
29
+ Curlify.new(request).to_curl # "curl -X POST -H 'User-Agent: Faraday v2.9.0' http://127.0.0.1"
30
+ ```
31
+
32
+ ## Usage with net/http
33
+
34
+ Import `curlify`, `uri` and `net/http` and perform curlify, see:
35
+
36
+ ```ruby
37
+ require 'json'
22
38
  require 'uri'
23
39
  require 'net/http'
24
40
  require 'curlify'
@@ -27,14 +43,29 @@ uri = URI('https://httpbin.org/post')
27
43
  request = Net::HTTP::Post.new(uri, { 'content-type': 'application/json' })
28
44
  request.body = { title: 'Ruby is great :)' }.to_json
29
45
 
30
- curlify = Curlify.new(request)
46
+ Curlify.new(request).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
47
+ ```
48
+
49
+ ## Clipboard support
50
+
51
+ Curlify can copy the generated curl command directly to the operating system clipboard. To enable this behavior, pass `clipboard: true` when creating the `Curlify` instance. The method still returns the curl string.
31
52
 
32
- puts curlify.to_curl
53
+ Supported platforms:
54
+ - macOS: uses `pbcopy`
55
+ - Windows: uses `clip`
56
+ - Linux: uses `xclip` (must be installed and available in `PATH`)
33
57
 
34
- # 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
58
+ Example:
59
+
60
+ ```ruby
61
+ # copy to clipboard and return the curl string
62
+ Curlify.new(request, clipboard: true).to_curl
35
63
  ```
36
64
 
37
- Running this command generated, we have the following result:
65
+ If `xclip` is not available on Linux, Curlify will print a warning: `Curlify Warning: 'xclip' is required for clipboard support on Linux.`
66
+
67
+
68
+ Performing this curl command, we can see the following result:
38
69
 
39
70
  ```bash
40
71
  {
data/lib/curlify.rb CHANGED
@@ -1,32 +1,79 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'faraday'
4
+ require 'byebug'
5
+
3
6
  class Curlify
4
- attr_reader :request, :verify, :compressed
7
+ attr_reader :request, :verify, :compressed, :clipboard
5
8
 
6
- def initialize(request, compressed: false, verify: true)
9
+ def initialize(request, compressed: false, verify: true, clipboard: false)
7
10
  @request = request
8
11
  @compressed = compressed
9
12
  @verify = verify
13
+ @clipboard = clipboard
10
14
  end
11
15
 
12
16
  def to_curl
13
- return curl_request << ' --compressed' if compressed
14
- return curl_request << ' --insecure' unless verify
15
-
16
- curl_request
17
+ command = build_curl_command
18
+ copy_to_clipboard(command)
19
+ command
17
20
  end
18
21
 
19
22
  private
20
23
 
24
+ def build_curl_command
25
+ [
26
+ curl_request,
27
+ verify ? nil : '--insecure',
28
+ compressed ? '--compressed' : nil
29
+ ].compact.join(' ')
30
+ end
31
+
21
32
  def curl_request
22
- "curl -X #{request.method} #{headers} #{body} #{request.uri}"
33
+ "curl -X #{http_method.upcase} #{headers} #{body} #{url}".strip
23
34
  end
24
35
 
25
36
  def headers
26
- request.each_header.map { |key, value| "-H '#{key}: #{value}'" }.join(' ')
37
+ if request.is_a?(Faraday::Request)
38
+ context_headers(request.headers)
39
+ else
40
+ context_headers(request.each_header)
41
+ end
42
+ end
43
+
44
+ def http_method
45
+ request.is_a?(Faraday::Request) ? request.http_method : request.method
46
+ end
47
+
48
+ def url
49
+ request.is_a?(Faraday::Request) ? request.path : request.uri
27
50
  end
28
51
 
29
52
  def body
30
53
  "-d '#{request.body}'" unless request.body.nil?
31
54
  end
55
+
56
+ def context_headers(headers)
57
+ headers.map { |k, v| "-H '#{k}: #{v}'" }.join(' ')
58
+ end
59
+
60
+ def copy_to_clipboard(string)
61
+ return unless clipboard
62
+
63
+ command = clipboard_command
64
+ return warn("Curlify Warning: 'xclip' is required for clipboard support on Linux.") unless command
65
+
66
+ IO.popen(command, 'w') { |f| f << string }
67
+ end
68
+
69
+ def clipboard_command
70
+ case RUBY_PLATFORM
71
+ when /darwin/
72
+ 'pbcopy'
73
+ when /mswin|mingw|cygwin/
74
+ 'clip'
75
+ when /linux/
76
+ 'xclip -selection clipboard' if system('which xclip > /dev/null 2>&1')
77
+ end
78
+ end
32
79
  end
metadata CHANGED
@@ -1,22 +1,36 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: curlify
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcus Almeida
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-09-02 00:00:00.000000000 Z
12
- dependencies: []
13
- description: The gem convert python requests object in curl command.
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: faraday
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '2.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '2.0'
26
+ description: Convert Ruby HTTP request and client objects into their equivalent curl
27
+ command. Useful for debugging and sharing HTTP requests.
14
28
  email: mpereirassa@gmail.com
15
29
  executables: []
16
30
  extensions: []
17
31
  extra_rdoc_files:
18
- - README.md
19
32
  - LICENSE
33
+ - README.md
20
34
  files:
21
35
  - LICENSE
22
36
  - README.md
@@ -29,7 +43,6 @@ metadata:
29
43
  bug_tracker_uri: https://github.com/marcuxyz/curlify/issues
30
44
  changelog_uri: https://github.com/marcuxyz/curlify/releases
31
45
  rubygems_mfa_required: 'true'
32
- post_install_message:
33
46
  rdoc_options:
34
47
  - "--charset=UTF-8"
35
48
  require_paths:
@@ -38,15 +51,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
51
  requirements:
39
52
  - - ">="
40
53
  - !ruby/object:Gem::Version
41
- version: 3.2.0
54
+ version: 2.7.8
42
55
  required_rubygems_version: !ruby/object:Gem::Requirement
43
56
  requirements:
44
57
  - - ">="
45
58
  - !ruby/object:Gem::Version
46
59
  version: '0'
47
60
  requirements: []
48
- rubygems_version: 3.2.22
49
- signing_key:
61
+ rubygems_version: 3.6.7
50
62
  specification_version: 4
51
- summary: Hola!
63
+ summary: Convert Ruby HTTP request objects into curl commands
52
64
  test_files: []