yapi-cli 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +108 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +95 -0
- data/LICENSE.txt +21 -0
- data/README.md +94 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/yapi +13 -0
- data/lib/yapi.rb +10 -0
- data/lib/yapi/cli.rb +37 -0
- data/lib/yapi/client.rb +48 -0
- data/lib/yapi/commands/fetch.rb +61 -0
- data/lib/yapi/commands/init.rb +53 -0
- data/lib/yapi/config.rb +43 -0
- data/lib/yapi/fetcher.rb +28 -0
- data/lib/yapi/request.rb +29 -0
- data/lib/yapi/response.rb +31 -0
- data/lib/yapi/version.rb +3 -0
- data/yapi.gemspec +32 -0
- data/yapi.yml +13 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 531f4468f3940d5da91036493f9b2792d1ce28be16cc6b84af9c276ab0cf284f
|
4
|
+
data.tar.gz: bfd52d9d1d60fc38da0073a9310f4af301133a83e3823d0d9a851c45194e86b8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a5ec5257d9f66ab7b610b21855f520b8859f99e8d9a0ccb5534de087bce5feb55a599cbdad24f31e6309e76b4b0540d54b64617655717f0381c3d308c5c419eb
|
7
|
+
data.tar.gz: 42c9d08ddcae99b7b26878f8b2dbe930bee2f3f22b2409f868da51f2c26fa859c97363a1b7201ae86654efa131792f411dfbe037e7411807e4142beada7a9062
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
|
3
|
+
Documentation:
|
4
|
+
Enabled: false
|
5
|
+
|
6
|
+
Layout/IndentArray:
|
7
|
+
EnforcedStyle: consistent
|
8
|
+
|
9
|
+
Layout/IndentHash:
|
10
|
+
EnforcedStyle: consistent
|
11
|
+
|
12
|
+
Layout/AlignHash:
|
13
|
+
EnforcedColonStyle: table
|
14
|
+
|
15
|
+
Layout/MultilineOperationIndentation:
|
16
|
+
EnforcedStyle: indented
|
17
|
+
|
18
|
+
Metrics/ModuleLength:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Metrics/AbcSize:
|
22
|
+
Description: >-
|
23
|
+
A calculated magnitude based on number of assignments,
|
24
|
+
branches, and conditions.
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Metrics/BlockLength:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
Metrics/LineLength:
|
31
|
+
Max: 100
|
32
|
+
|
33
|
+
RSpec/DescribedClass:
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
RSpec/ExampleLength:
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
RSpec/MultipleExpectations:
|
40
|
+
Enabled: false
|
41
|
+
|
42
|
+
StringLiterals:
|
43
|
+
EnforcedStyle: double_quotes
|
44
|
+
Enabled: true
|
45
|
+
|
46
|
+
Style/AccessorMethodName:
|
47
|
+
Description: "Check the naming of accessor methods for get_/set_."
|
48
|
+
Enabled: false
|
49
|
+
|
50
|
+
Style/Alias:
|
51
|
+
Description: "Use alias_method instead of alias."
|
52
|
+
StyleGuide: "https://github.com/bbatsov/ruby-style-guide#alias-method"
|
53
|
+
Enabled: false
|
54
|
+
|
55
|
+
Style/CollectionMethods:
|
56
|
+
Enabled: true
|
57
|
+
PreferredMethods:
|
58
|
+
find: detect
|
59
|
+
inject: reduce
|
60
|
+
collect: map
|
61
|
+
find_all: select
|
62
|
+
|
63
|
+
Style/DoubleNegation:
|
64
|
+
Description: "Checks for uses of double negation (!!)."
|
65
|
+
StyleGuide: "https://github.com/bbatsov/ruby-style-guide#no-bang-bang"
|
66
|
+
Enabled: false
|
67
|
+
|
68
|
+
Style/EmptyMethod:
|
69
|
+
Description: "Checks for the formatting of empty method definitions."
|
70
|
+
Enabled: false
|
71
|
+
|
72
|
+
Style/GuardClause:
|
73
|
+
Enabled: false
|
74
|
+
|
75
|
+
Style/IfUnlessModifier:
|
76
|
+
Enabled: false
|
77
|
+
|
78
|
+
Style/NumericLiterals:
|
79
|
+
Enabled: false
|
80
|
+
|
81
|
+
Style/SymbolArray:
|
82
|
+
MinSize: 3
|
83
|
+
Enabled: true
|
84
|
+
|
85
|
+
Style/WordArray:
|
86
|
+
MinSize: 3
|
87
|
+
Enabled: true
|
88
|
+
|
89
|
+
AllCops:
|
90
|
+
Exclude:
|
91
|
+
- bin/**/*
|
92
|
+
- config/**/*
|
93
|
+
- db/**/*
|
94
|
+
- script/**/*
|
95
|
+
- spec/rails_helper.rb
|
96
|
+
- spec/spec_helper.rb
|
97
|
+
- spec/teaspoon_env.rb
|
98
|
+
- vendor/**/*
|
99
|
+
- config.ru
|
100
|
+
- Gemfile
|
101
|
+
- Rakefile
|
102
|
+
TargetRubyVersion: 2.3
|
103
|
+
|
104
|
+
Rails:
|
105
|
+
Enabled: true
|
106
|
+
|
107
|
+
Rails/HasAndBelongsToMany:
|
108
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
yapi-cli (0.1.0)
|
5
|
+
http (~> 4.4.1)
|
6
|
+
thor (~> 1.0.1)
|
7
|
+
tty-prompt (~> 0.21.0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
addressable (2.7.0)
|
13
|
+
public_suffix (>= 2.0.2, < 5.0)
|
14
|
+
ast (2.4.0)
|
15
|
+
diff-lcs (1.3)
|
16
|
+
domain_name (0.5.20190701)
|
17
|
+
unf (>= 0.0.5, < 1.0.0)
|
18
|
+
equatable (0.6.1)
|
19
|
+
ffi (1.12.2)
|
20
|
+
ffi-compiler (1.0.1)
|
21
|
+
ffi (>= 1.0.0)
|
22
|
+
rake
|
23
|
+
http (4.4.1)
|
24
|
+
addressable (~> 2.3)
|
25
|
+
http-cookie (~> 1.0)
|
26
|
+
http-form_data (~> 2.2)
|
27
|
+
http-parser (~> 1.2.0)
|
28
|
+
http-cookie (1.0.3)
|
29
|
+
domain_name (~> 0.5)
|
30
|
+
http-form_data (2.3.0)
|
31
|
+
http-parser (1.2.1)
|
32
|
+
ffi-compiler (>= 1.0, < 2.0)
|
33
|
+
jaro_winkler (1.5.4)
|
34
|
+
necromancer (0.5.1)
|
35
|
+
parallel (1.19.1)
|
36
|
+
parser (2.7.1.2)
|
37
|
+
ast (~> 2.4.0)
|
38
|
+
pastel (0.7.3)
|
39
|
+
equatable (~> 0.6)
|
40
|
+
tty-color (~> 0.5)
|
41
|
+
public_suffix (4.0.4)
|
42
|
+
rainbow (3.0.0)
|
43
|
+
rake (12.3.3)
|
44
|
+
rexml (3.2.4)
|
45
|
+
rspec (3.9.0)
|
46
|
+
rspec-core (~> 3.9.0)
|
47
|
+
rspec-expectations (~> 3.9.0)
|
48
|
+
rspec-mocks (~> 3.9.0)
|
49
|
+
rspec-core (3.9.2)
|
50
|
+
rspec-support (~> 3.9.3)
|
51
|
+
rspec-expectations (3.9.1)
|
52
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
53
|
+
rspec-support (~> 3.9.0)
|
54
|
+
rspec-mocks (3.9.1)
|
55
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
56
|
+
rspec-support (~> 3.9.0)
|
57
|
+
rspec-support (3.9.3)
|
58
|
+
rubocop (0.82.0)
|
59
|
+
jaro_winkler (~> 1.5.1)
|
60
|
+
parallel (~> 1.10)
|
61
|
+
parser (>= 2.7.0.1)
|
62
|
+
rainbow (>= 2.2.2, < 4.0)
|
63
|
+
rexml
|
64
|
+
ruby-progressbar (~> 1.7)
|
65
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
66
|
+
ruby-progressbar (1.10.1)
|
67
|
+
thor (1.0.1)
|
68
|
+
tty-color (0.5.1)
|
69
|
+
tty-cursor (0.7.1)
|
70
|
+
tty-prompt (0.21.0)
|
71
|
+
necromancer (~> 0.5.0)
|
72
|
+
pastel (~> 0.7.0)
|
73
|
+
tty-reader (~> 0.7.0)
|
74
|
+
tty-reader (0.7.0)
|
75
|
+
tty-cursor (~> 0.7)
|
76
|
+
tty-screen (~> 0.7)
|
77
|
+
wisper (~> 2.0.0)
|
78
|
+
tty-screen (0.7.1)
|
79
|
+
unf (0.1.4)
|
80
|
+
unf_ext
|
81
|
+
unf_ext (0.0.7.7)
|
82
|
+
unicode-display_width (1.7.0)
|
83
|
+
wisper (2.0.1)
|
84
|
+
|
85
|
+
PLATFORMS
|
86
|
+
ruby
|
87
|
+
|
88
|
+
DEPENDENCIES
|
89
|
+
rake (~> 12.0)
|
90
|
+
rspec (~> 3.0)
|
91
|
+
rubocop (~> 0.82.0)
|
92
|
+
yapi-cli!
|
93
|
+
|
94
|
+
BUNDLED WITH
|
95
|
+
2.1.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Daniel Bridges
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all 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,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# yapi
|
2
|
+
|
3
|
+
`yapi` is a very simple API client (a la Postman) which uses a yaml file for configuration.
|
4
|
+
|
5
|
+
Start with a yapi.yml file:
|
6
|
+
|
7
|
+
```yaml
|
8
|
+
# routes.yapi.yml
|
9
|
+
---
|
10
|
+
root: http://localhost:3000
|
11
|
+
session: my_project
|
12
|
+
headers:
|
13
|
+
Content-Type: application/json
|
14
|
+
Accept: application/json
|
15
|
+
output:
|
16
|
+
headers: true
|
17
|
+
|
18
|
+
# Routes
|
19
|
+
|
20
|
+
users.index:
|
21
|
+
path: /users
|
22
|
+
params:
|
23
|
+
page: 2
|
24
|
+
per: 100
|
25
|
+
|
26
|
+
users.show:
|
27
|
+
path: /users/1
|
28
|
+
|
29
|
+
users.create:
|
30
|
+
path: /users
|
31
|
+
method: POST
|
32
|
+
body: |
|
33
|
+
{
|
34
|
+
"name": "Test User",
|
35
|
+
"email": "testuser@example.com",
|
36
|
+
}
|
37
|
+
```
|
38
|
+
|
39
|
+
Run `yapi` for an interactive prompt:
|
40
|
+
|
41
|
+
```sh
|
42
|
+
$ yapi routes.yapi.yml
|
43
|
+
```
|
44
|
+
|
45
|
+
or specify a line number and the nearest request specification will be run:
|
46
|
+
|
47
|
+
```sh
|
48
|
+
$ yapi routes.yapi.yml:20
|
49
|
+
```
|
50
|
+
|
51
|
+
|
52
|
+
## Installation
|
53
|
+
|
54
|
+
```sh
|
55
|
+
gem install yapi-cli
|
56
|
+
```
|
57
|
+
|
58
|
+
## Details
|
59
|
+
|
60
|
+
Cookies will be used if a `session` name is provided in the project settings.
|
61
|
+
|
62
|
+
### Available Project Settings
|
63
|
+
|
64
|
+
`root`: The root path for requests in this project.
|
65
|
+
|
66
|
+
`headers`: Headers to apply to every request. Requests can overwrite these when needed.
|
67
|
+
|
68
|
+
`session`: A name to store cookies for. Session cookies are saved to `~/.cache/yapi/session_name.jar`
|
69
|
+
|
70
|
+
`output`: Toggles whether to print various items in the output. Currently only `headers: true|false` is supported.
|
71
|
+
|
72
|
+
### Available Request Options
|
73
|
+
|
74
|
+
`path`: Path to append to `root` path defined in project settings.
|
75
|
+
|
76
|
+
`method`: HTTP method type.
|
77
|
+
|
78
|
+
`headers`: Define additional headers here, they will be merged with any headers defined on the project level.
|
79
|
+
|
80
|
+
`params`, `body`, `json`, `form`: See [http.rb documentation](https://github.com/httprb/http/wiki/Passing-Parameters)
|
81
|
+
|
82
|
+
## Development
|
83
|
+
|
84
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
85
|
+
|
86
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
87
|
+
|
88
|
+
## Contributing
|
89
|
+
|
90
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/dbridges/yapi.
|
91
|
+
|
92
|
+
## License
|
93
|
+
|
94
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "yapi"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/yapi
ADDED
data/lib/yapi.rb
ADDED
data/lib/yapi/cli.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "thor"
|
4
|
+
|
5
|
+
require "yapi/commands/fetch"
|
6
|
+
require "yapi/commands/init"
|
7
|
+
|
8
|
+
module YAPI
|
9
|
+
class CLI < Thor
|
10
|
+
class << self
|
11
|
+
def exit_on_failure?
|
12
|
+
true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "fetch [filename]", "Fetch a request"
|
17
|
+
long_desc <<-LONGDESC
|
18
|
+
Fetches and prints the output of a request from a yapi.yml file.
|
19
|
+
|
20
|
+
`yapi fetch path/to/yapi.yml:12`
|
21
|
+
|
22
|
+
When a line number is not specified, `yapi` will prompt for a request name.
|
23
|
+
|
24
|
+
`yapi fetch path/to/yapi.yml:12`
|
25
|
+
|
26
|
+
When a line number is specified, `yapi` will run the nearest request to that line.
|
27
|
+
LONGDESC
|
28
|
+
def fetch(file_desc)
|
29
|
+
YAPI::Commands::Fetch.new(file_desc).perform
|
30
|
+
end
|
31
|
+
|
32
|
+
desc "init", "Initialize a new YAPI project file"
|
33
|
+
def init
|
34
|
+
YAPI::Commands::Init.new.perform
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/yapi/client.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "http"
|
4
|
+
require "fileutils"
|
5
|
+
|
6
|
+
module YAPI
|
7
|
+
class Client
|
8
|
+
def initialize(session_name)
|
9
|
+
@session_name = session_name
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform(req)
|
13
|
+
YAPI::Response.new(HTTP.cookies(cookies)
|
14
|
+
.headers(req.headers)
|
15
|
+
.request(req.method, req.url, req.opts)).tap do |resp|
|
16
|
+
update_cookies(resp.cookies)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def cookies
|
23
|
+
@cookies ||= HTTP::CookieJar.new.tap do |jar|
|
24
|
+
jar.load(cookie_path) if @session_name && File.exist?(cookie_path)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def load_cookies
|
29
|
+
if File.exist?(cookie_path)
|
30
|
+
@cookies.load(cookie_path)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def update_cookies(cookies)
|
35
|
+
@cookies = cookies
|
36
|
+
FileUtils.mkdir_p(cookie_dir)
|
37
|
+
cookies.save(cookie_path)
|
38
|
+
end
|
39
|
+
|
40
|
+
def cookie_path
|
41
|
+
File.expand_path("~/.cache/yapi/#{@session_name}.jar")
|
42
|
+
end
|
43
|
+
|
44
|
+
def cookie_dir
|
45
|
+
File.dirname cookie_path
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "tty-prompt"
|
4
|
+
|
5
|
+
require "yapi/fetcher"
|
6
|
+
|
7
|
+
module YAPI
|
8
|
+
module Commands
|
9
|
+
class Fetch
|
10
|
+
def initialize(arg)
|
11
|
+
match = arg.match(/(.*?):?(\d+)?$/)
|
12
|
+
if match
|
13
|
+
_, @filename, @line_number = match.to_a
|
14
|
+
else
|
15
|
+
@filename = arg
|
16
|
+
end
|
17
|
+
rescue StandardError => e
|
18
|
+
puts e
|
19
|
+
end
|
20
|
+
|
21
|
+
def perform
|
22
|
+
YAPI::Fetcher.new(config, route_name).perform
|
23
|
+
end
|
24
|
+
|
25
|
+
def route_name
|
26
|
+
if @line_number
|
27
|
+
find_route_name
|
28
|
+
else
|
29
|
+
prompt_route_name
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def find_route_name
|
34
|
+
row = @line_number.to_i
|
35
|
+
while row > 0 do
|
36
|
+
match = lines[row - 1].match(/(^\S.*):$/)
|
37
|
+
return match[1] if match
|
38
|
+
row -= 1
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def prompt_route_name
|
43
|
+
prompt.select("Choose request to run (Ctrl-C to exit):",
|
44
|
+
config.requests,
|
45
|
+
filter: true)
|
46
|
+
end
|
47
|
+
|
48
|
+
def lines
|
49
|
+
@lines ||= File.readlines(@filename)
|
50
|
+
end
|
51
|
+
|
52
|
+
def config
|
53
|
+
@config ||= YAPI::Config.new(@filename)
|
54
|
+
end
|
55
|
+
|
56
|
+
def prompt
|
57
|
+
@prompt ||= TTY::Prompt.new(active_color: :bold, interrupt: :exit)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "tty-prompt"
|
4
|
+
|
5
|
+
module YAPI
|
6
|
+
module Commands
|
7
|
+
class Init
|
8
|
+
def perform
|
9
|
+
create_config
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def create_config
|
15
|
+
ask_filename
|
16
|
+
ask_root
|
17
|
+
write_file
|
18
|
+
prompt.ok("Created #{@filename}")
|
19
|
+
end
|
20
|
+
|
21
|
+
def ask_filename
|
22
|
+
@filename = prompt.ask("Filename:",
|
23
|
+
convert: :string,
|
24
|
+
default: "#{File.basename(Dir.getwd)}.yapi.yml")
|
25
|
+
if File.exist? @filename
|
26
|
+
prompt.warn("#{@filename} already exists, please choose another.")
|
27
|
+
ask_filename
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def ask_root
|
32
|
+
@root_path = prompt.ask("Root Path:",
|
33
|
+
convert: :string,
|
34
|
+
default: "http://localhost:3000")
|
35
|
+
end
|
36
|
+
|
37
|
+
def write_file
|
38
|
+
contents = <<~HEREDOC
|
39
|
+
---
|
40
|
+
root: #{@root_path}
|
41
|
+
|
42
|
+
sample_route:
|
43
|
+
path: /user
|
44
|
+
HEREDOC
|
45
|
+
File.write(@filename, contents)
|
46
|
+
end
|
47
|
+
|
48
|
+
def prompt
|
49
|
+
@prompt ||= TTY::Prompt.new(interrupt: :exit, enable_color: false)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/yapi/config.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "delegate"
|
4
|
+
require "uri"
|
5
|
+
require "yaml"
|
6
|
+
|
7
|
+
module YAPI
|
8
|
+
class Config < ::SimpleDelegator
|
9
|
+
|
10
|
+
SETTINGS_KEYS = %w[root headers output].freeze
|
11
|
+
|
12
|
+
def initialize(filename)
|
13
|
+
super(YAML.load(File.read(filename)))
|
14
|
+
end
|
15
|
+
|
16
|
+
def output_headers?
|
17
|
+
fetch("output", {}).fetch("headers", true)
|
18
|
+
end
|
19
|
+
|
20
|
+
def session
|
21
|
+
self["session"]
|
22
|
+
end
|
23
|
+
|
24
|
+
def root
|
25
|
+
self["root"]
|
26
|
+
end
|
27
|
+
|
28
|
+
def headers
|
29
|
+
fetch("headers", {})
|
30
|
+
end
|
31
|
+
|
32
|
+
def request(route_name)
|
33
|
+
req = self[route_name]
|
34
|
+
req.merge!("url" => URI.join(root, req["path"]))
|
35
|
+
req["headers"] = headers.merge(req.fetch("headers", {}))
|
36
|
+
YAPI::Request.new(req)
|
37
|
+
end
|
38
|
+
|
39
|
+
def requests
|
40
|
+
keys - SETTINGS_KEYS
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/yapi/fetcher.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module YAPI
|
4
|
+
class Fetcher
|
5
|
+
attr_reader :config
|
6
|
+
attr_reader :route_name
|
7
|
+
|
8
|
+
def initialize(config, route_name)
|
9
|
+
@config = config
|
10
|
+
@route_name = route_name
|
11
|
+
end
|
12
|
+
|
13
|
+
def perform
|
14
|
+
puts "#{request.method.upcase} #{request.url}"
|
15
|
+
puts client.perform(request).to_s(show_headers: config.output_headers?)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def client
|
21
|
+
@client ||= YAPI::Client.new(config.session)
|
22
|
+
end
|
23
|
+
|
24
|
+
def request
|
25
|
+
@request ||= config.request(route_name)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/yapi/request.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module YAPI
|
4
|
+
class Request
|
5
|
+
def initialize(attrs)
|
6
|
+
@attrs = attrs
|
7
|
+
end
|
8
|
+
|
9
|
+
def url
|
10
|
+
@attrs["url"]
|
11
|
+
end
|
12
|
+
|
13
|
+
def method
|
14
|
+
@attrs.fetch("method", "GET")
|
15
|
+
end
|
16
|
+
|
17
|
+
def path
|
18
|
+
@attrs["path"]
|
19
|
+
end
|
20
|
+
|
21
|
+
def headers
|
22
|
+
@attrs.fetch("headers", {})
|
23
|
+
end
|
24
|
+
|
25
|
+
def opts
|
26
|
+
@attrs.slice("params", "form", "json", "body")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "delegate"
|
4
|
+
|
5
|
+
module YAPI
|
6
|
+
class Response < ::SimpleDelegator
|
7
|
+
def to_s(show_headers: true)
|
8
|
+
String.new.tap do |out|
|
9
|
+
out << "\n#{status}\n\n"
|
10
|
+
out << header_str + "\n" if show_headers
|
11
|
+
out << body_str
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def header_str
|
16
|
+
String.new.tap do |out|
|
17
|
+
headers.each do |k, v|
|
18
|
+
out << "#{k}: #{v}\n"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def body_str
|
24
|
+
if self["Content-Type"] =~ /application\/json/
|
25
|
+
JSON.pretty_generate(JSON.parse(body)).to_s
|
26
|
+
else
|
27
|
+
body.to_s
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/yapi/version.rb
ADDED
data/yapi.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative 'lib/yapi/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "yapi-cli"
|
5
|
+
spec.version = YAPI::VERSION
|
6
|
+
spec.authors = ["Daniel Bridges"]
|
7
|
+
spec.email = ["dan@danbridges.org"]
|
8
|
+
|
9
|
+
spec.summary = "A YAML based API Client"
|
10
|
+
spec.homepage = "https://github.com/dbridges/yapi-cli"
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
spec.metadata["source_code_uri"] = "https://github.com/dbridges/yapi-cli"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = ["yapi"]
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "rubocop", "~> 0.82.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.9.0"
|
28
|
+
|
29
|
+
spec.add_runtime_dependency "http", "~> 4.4.1"
|
30
|
+
spec.add_runtime_dependency "thor", "~> 1.0.1 "
|
31
|
+
spec.add_runtime_dependency "tty-prompt", "~> 0.21.0"
|
32
|
+
end
|
data/yapi.yml
ADDED
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yapi-cli
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Bridges
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-05-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rubocop
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.82.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.82.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.9.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.9.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: http
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 4.4.1
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 4.4.1
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: thor
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.0.1
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.0.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: tty-prompt
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.21.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.21.0
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- dan@danbridges.org
|
86
|
+
executables:
|
87
|
+
- yapi
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".rubocop.yml"
|
94
|
+
- ".travis.yml"
|
95
|
+
- Gemfile
|
96
|
+
- Gemfile.lock
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- exe/yapi
|
103
|
+
- lib/yapi.rb
|
104
|
+
- lib/yapi/cli.rb
|
105
|
+
- lib/yapi/client.rb
|
106
|
+
- lib/yapi/commands/fetch.rb
|
107
|
+
- lib/yapi/commands/init.rb
|
108
|
+
- lib/yapi/config.rb
|
109
|
+
- lib/yapi/fetcher.rb
|
110
|
+
- lib/yapi/request.rb
|
111
|
+
- lib/yapi/response.rb
|
112
|
+
- lib/yapi/version.rb
|
113
|
+
- yapi.gemspec
|
114
|
+
- yapi.yml
|
115
|
+
homepage: https://github.com/dbridges/yapi-cli
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata:
|
119
|
+
homepage_uri: https://github.com/dbridges/yapi-cli
|
120
|
+
source_code_uri: https://github.com/dbridges/yapi-cli
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: 2.3.0
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubygems_version: 3.1.2
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: A YAML based API Client
|
140
|
+
test_files: []
|