yobi-http 0.1.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/README.md +36 -0
- data/Rakefile +8 -0
- data/exe/yobi +142 -0
- data/lib/views/output.md.erb +26 -0
- data/lib/yobi/http.rb +8 -0
- data/lib/yobi.rb +28 -0
- metadata +67 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 9e47c9cf6b60d6ccc40c4e679132f68da6a792500723c18d6b9779c7d350fb0b
|
|
4
|
+
data.tar.gz: b22cc75a93e412033c498548e82f1cf373d9ab9720dafea4c44efcc88228a20b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 407e2e909630c3243efa467fb63ce4742895140331158decc77544da3151c11d1d2b4720debe9f2c03ad8205b86f944c8bde1f58df547ac893034143bf7ea279
|
|
7
|
+
data.tar.gz: d233b846b87e54ec3a4467fd3df0460ba14e6c0ad27cad2c3789cd5d5995abe99eb8efc739821022306d803fa6e6fc51bd1c4ef38172d9675ad221a10ff5b2b4
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
"yobi-http" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
|
+
|
|
5
|
+
* Participants will be tolerant of opposing views.
|
|
6
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
9
|
+
|
|
10
|
+
If you have any concerns about behaviour within this project, please contact us at ["daniel.vinciguerra@bivee.com.br"](mailto:"daniel.vinciguerra@bivee.com.br").
|
data/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Yobi(呼び) Http Client
|
|
2
|
+
|
|
3
|
+
Yobi is a Ruby gem that provides a simple and efficient way to make HTTP requests. It is designed to be easy to use
|
|
4
|
+
and flexible, allowing you to customize your requests as needed.
|
|
5
|
+
|
|
6
|
+
Its a lightweight implementation of the HTTPie tool, which is a command-line HTTP client that allows you to make
|
|
7
|
+
HTTP requests and view the responses in a human-friendly format.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
gem install yobi-http
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
Some examples of how to use yobi:
|
|
19
|
+
|
|
20
|
+
- `yobi GET https://jsonplaceholder.typicode.com/posts/1` - Makes a GET request to the specified URL and prints the response.
|
|
21
|
+
- `yobi POST https://jsonplaceholder.typicode.com/posts title="foo" body="bar" userId=1` - Makes a POST request to the specified URL with the given data and prints the response.
|
|
22
|
+
- `yobi GET https://jsonplaceholder.typicode.com/posts/1 Authorization:"Bearer <token>"` - Makes a GET request to the specified URL with the given header and prints the response.
|
|
23
|
+
|
|
24
|
+
## Development
|
|
25
|
+
|
|
26
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
27
|
+
|
|
28
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
29
|
+
|
|
30
|
+
## Contributing
|
|
31
|
+
|
|
32
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/dvinciguerra/yobi-http. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/dvinciguerra/yobi-http/blob/main/CODE_OF_CONDUCT.md).
|
|
33
|
+
|
|
34
|
+
## Code of Conduct
|
|
35
|
+
|
|
36
|
+
Everyone interacting in the yobi-http project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/dvinciguerra/yobi-http/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/exe/yobi
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
#!env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
|
|
5
|
+
|
|
6
|
+
require "base64"
|
|
7
|
+
require "erb"
|
|
8
|
+
require "json"
|
|
9
|
+
require "net/http"
|
|
10
|
+
require "optparse"
|
|
11
|
+
require "tty-markdown"
|
|
12
|
+
require "uri"
|
|
13
|
+
|
|
14
|
+
require "yobi"
|
|
15
|
+
|
|
16
|
+
# parsing arguments
|
|
17
|
+
@options = { print: "HB", auth: nil, auth_type: "basic", verbose: false, raw: false }
|
|
18
|
+
|
|
19
|
+
parser = OptionParser.new do |opts|
|
|
20
|
+
opts.banner = %(
|
|
21
|
+
#{Yobi.name} - #{Yobi.description}
|
|
22
|
+
|
|
23
|
+
Usage:
|
|
24
|
+
yobi [METHOD] <url> [HEADER:VALUE] [key=value]
|
|
25
|
+
|
|
26
|
+
Examples:
|
|
27
|
+
yobi https://api.example.com/users
|
|
28
|
+
yobi POST https://api.example.com/users name=John age=30
|
|
29
|
+
yobi GET :8080/api/items Authorization:"Bearer $TOKEN"
|
|
30
|
+
yobi -A basic -a user:pass https://api.example.com/secure-data
|
|
31
|
+
yobi --raw --print B POST https://httpbin.org/anything | jq '.headers["User-Agent"]'
|
|
32
|
+
|
|
33
|
+
Options:
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
opts.on("-p", "--print FORMAT", "Specify the output format (e.g., 'H' for headers, 'B' for body)") do |format|
|
|
37
|
+
@options[:print] = format.upcase || "HB" # default to printing both headers and body
|
|
38
|
+
warn "[Argument Error] Unsupported format: #{format}" unless format.match?(/\A[HB]+\z/i)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
opts.on("-a", "--auth USER:PASS", "Specify basic authentication credentials") do |auth|
|
|
42
|
+
@options[:auth] = auth
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
opts.on("-A", "--auth-type TYPE", "Specify authentication type (e.g., basic, digest, bearer)") do |auth_type|
|
|
46
|
+
@options[:auth_type] = auth_type
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
opts.on("--raw", "Print raw request and response data") do
|
|
50
|
+
@options[:raw] = true
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
opts.on("-h", "--help", "Print this help message") do
|
|
54
|
+
puts opts
|
|
55
|
+
exit
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
opts.on("-v", "--verbose", "Print detailed request and response information") do
|
|
59
|
+
@options[:verbose] = true
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
opts.on("--version", "Print app version") do
|
|
63
|
+
puts "#{Yobi.name} v#{Yobi::VERSION}"
|
|
64
|
+
exit
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
parser.parse!(ARGV)
|
|
69
|
+
|
|
70
|
+
# resolve the HTTP method
|
|
71
|
+
method = Yobi::Http::METHODS.include?(ARGV[0]) ? ARGV.shift : "GET"
|
|
72
|
+
|
|
73
|
+
# resolve the URL
|
|
74
|
+
url = case ARGV[0]
|
|
75
|
+
when %r{\Ahttps?://}
|
|
76
|
+
ARGV.shift
|
|
77
|
+
when /\A:\d+/
|
|
78
|
+
"http://localhost#{ARGV.shift}"
|
|
79
|
+
else
|
|
80
|
+
"http://#{ARGV.shift}"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
data =
|
|
84
|
+
ARGV.select { |arg| arg.include?("=") }.map.to_h { |arg| arg.split("=", 2).map(&:strip) }
|
|
85
|
+
|
|
86
|
+
headers =
|
|
87
|
+
{ "Content-Type" => "application/json", "User-Agent" => "#{Yobi.name}/#{Yobi::VERSION}" }
|
|
88
|
+
.merge(ARGV.select { |arg| arg.include?(":") }.map.to_h { |arg| arg.split(":", 2).map(&:strip) })
|
|
89
|
+
|
|
90
|
+
# prepare authentication header if auth is provided
|
|
91
|
+
if @options[:auth]
|
|
92
|
+
auth_type = @options[:auth_type] || "basic"
|
|
93
|
+
case auth_type.downcase
|
|
94
|
+
when "basic"
|
|
95
|
+
headers["Authorization"] = "Basic #{Base64.strict_encode64(@options[:auth])}"
|
|
96
|
+
when "bearer"
|
|
97
|
+
headers["Authorization"] = "Bearer #{@options[:auth]}"
|
|
98
|
+
else
|
|
99
|
+
warn "Unsupported authentication type: #{auth_type}"
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
pp [method, url, data, headers, @options, ARGV] if ENV["YOBI_DEBUG"]
|
|
104
|
+
|
|
105
|
+
Net::HTTP.start(URI(url).host, URI(url).port, use_ssl: URI(url).scheme == "https") do |http|
|
|
106
|
+
options = @options
|
|
107
|
+
request_class = Net::HTTP.const_get(method.capitalize)
|
|
108
|
+
request = request_class.new(URI(url))
|
|
109
|
+
|
|
110
|
+
headers.each { |key, value| request[key] = value }
|
|
111
|
+
request["Content-Type"] ||= "application/json"
|
|
112
|
+
|
|
113
|
+
request.body = data.to_json unless data.empty?
|
|
114
|
+
|
|
115
|
+
response = http.request(request)
|
|
116
|
+
|
|
117
|
+
body = nil
|
|
118
|
+
if response["Content-Type"]&.include?("application/json") && response.body
|
|
119
|
+
begin
|
|
120
|
+
body ||= JSON.pretty_generate(JSON.parse(response.body))
|
|
121
|
+
rescue StandardError
|
|
122
|
+
body ||= response.body
|
|
123
|
+
end
|
|
124
|
+
else
|
|
125
|
+
body ||= response.body
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
if options[:raw]
|
|
129
|
+
if options[:print].include?("H")
|
|
130
|
+
puts "HTTP/#{response.http_version} #{response.code} #{response.message}"
|
|
131
|
+
response.each_header { |key, value| puts "#{key}: #{value}" }
|
|
132
|
+
puts
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
puts body if options[:print].include?("B") && body
|
|
136
|
+
|
|
137
|
+
break
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
view = Yobi.view(:output)
|
|
141
|
+
puts TTY::Markdown.parse(ERB.new(view).result(binding), color: :always)
|
|
142
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
<% if options[:verbose] %>
|
|
2
|
+
# <%= request.method %> <%= request.path %> HTTP/1.1
|
|
3
|
+
|
|
4
|
+
```yaml
|
|
5
|
+
<%= request.each_capitalized.to_h.sort.map { |key, value| "#{key}: #{value}" }.join("\n") %>
|
|
6
|
+
```
|
|
7
|
+
|
|
8
|
+
```json
|
|
9
|
+
<%= JSON.pretty_generate(JSON.parse(request.body)) if request.body && !request.body.empty? %>
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
<% end %>
|
|
13
|
+
|
|
14
|
+
# HTTP/<%= response.http_version %> <%= response.code %> <%= response.message %>
|
|
15
|
+
|
|
16
|
+
<% if options[:print].include?('H') %>
|
|
17
|
+
```yaml
|
|
18
|
+
<%= response.each_capitalized.to_h.sort.map { |key, value| "#{key}: #{value}" }.join("\n") %>
|
|
19
|
+
```
|
|
20
|
+
<% end %>
|
|
21
|
+
|
|
22
|
+
<% if options[:print].include?('B') %>
|
|
23
|
+
```<%= /json/.match?(response['Content-Type']) ? 'json' : 'html' %>
|
|
24
|
+
<%= body %>
|
|
25
|
+
```
|
|
26
|
+
<% end %>
|
data/lib/yobi/http.rb
ADDED
data/lib/yobi.rb
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
|
|
4
|
+
|
|
5
|
+
require "yobi/http"
|
|
6
|
+
|
|
7
|
+
# Yobi Http CLI client namespace
|
|
8
|
+
module Yobi
|
|
9
|
+
# Standard Yobi error class
|
|
10
|
+
class Error < StandardError; end
|
|
11
|
+
|
|
12
|
+
# Yobi gem version
|
|
13
|
+
VERSION = "0.1.0"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def self.name
|
|
17
|
+
"yobi"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.description
|
|
21
|
+
"A simple HTTP client for testing APIs from the command line."
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Load project templates
|
|
25
|
+
def self.view(name)
|
|
26
|
+
File.read(File.join(__dir__, "views/#{name}.md.erb"))
|
|
27
|
+
end
|
|
28
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: yobi-http
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Daniel Vinciguerra
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 2026-02-11 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: tty-markdown
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0.7'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0.7'
|
|
26
|
+
description: Yobi is a simple HTTP client for testing APIs from the command line built
|
|
27
|
+
with Ruby
|
|
28
|
+
email:
|
|
29
|
+
- daniel.vinciguerra@bivee.com.br
|
|
30
|
+
executables:
|
|
31
|
+
- yobi
|
|
32
|
+
extensions: []
|
|
33
|
+
extra_rdoc_files: []
|
|
34
|
+
files:
|
|
35
|
+
- CHANGELOG.md
|
|
36
|
+
- CODE_OF_CONDUCT.md
|
|
37
|
+
- README.md
|
|
38
|
+
- Rakefile
|
|
39
|
+
- exe/yobi
|
|
40
|
+
- lib/views/output.md.erb
|
|
41
|
+
- lib/yobi.rb
|
|
42
|
+
- lib/yobi/http.rb
|
|
43
|
+
homepage: https://github.com/dvinciguerra/yobi-http
|
|
44
|
+
licenses: []
|
|
45
|
+
metadata:
|
|
46
|
+
homepage_uri: https://github.com/dvinciguerra/yobi-http
|
|
47
|
+
source_code_uri: https://github.com/dvinciguerra/yobi-http
|
|
48
|
+
changelog_uri: https://github.com/dvinciguerra/yobi-http/blob/main/CHANGELOG.md
|
|
49
|
+
rdoc_options: []
|
|
50
|
+
require_paths:
|
|
51
|
+
- lib
|
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: 3.0.0
|
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
requirements: []
|
|
63
|
+
rubygems_version: 3.6.2
|
|
64
|
+
specification_version: 4
|
|
65
|
+
summary: Yobi is a simple HTTP client for testing APIs from the command line built
|
|
66
|
+
with Ruby
|
|
67
|
+
test_files: []
|