poke-your-api 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 013bffa8e1fd7cc08a67690af8a31338f81f625d21e2fdb768c414f1b5b50332
4
+ data.tar.gz: 71372e8e4c8cce9846d264d4dd047f772d807b6a9f5f08d66d92c0ede5531211
5
+ SHA512:
6
+ metadata.gz: da14a06ee7ced9ab757c6745ff669db496ff331f7cf5414186605db9d06196bc7be867a6bbf295556dceea27649d99de09953086365d60e63991b3a228c840c8
7
+ data.tar.gz: 7287edb8ee100e11176ba9b66c71c57374b2c304e91d50787af79da43edb3a5d8b710527d6f1182fd626a37a08a5c6cf1a45ff4be4f1822ea1929acc00316b37
data/.editorconfig ADDED
@@ -0,0 +1,9 @@
1
+ root = true
2
+
3
+ [*.rb]
4
+ charset = utf-8
5
+ end_of_line = lf
6
+ insert_final_newline = true
7
+ indent_style = space
8
+ indent_size = 2
9
+ trim_trailing_whitespace = true
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ Style/Documentation:
2
+ Enabled: false
3
+ Style/FrozenStringLiteralComment:
4
+ Enabled: true
5
+ SafeAutoCorrect: true
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.0
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at jan@bloomandwild.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in poke.gemspec
6
+ gemspec
7
+
8
+ gem "rake"
9
+ gem "rake-compiler"
10
+ gem "rspec"
11
+
12
+ gem "tty"#, path: "/Users/janbator/Projects/tty" # That's with some of my "fixes" :D
13
+ gem "pry", require: true
14
+ gem "pry-nav", require: true
data/Gemfile.lock ADDED
@@ -0,0 +1,89 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ poke (0.1.0)
5
+ pastel (~> 0.8)
6
+ thor (~> 1.0)
7
+ tty-box (~> 0.7)
8
+ tty-command (~> 0.10)
9
+ tty-cursor (~> 0.7)
10
+ tty-editor (~> 0.6)
11
+ tty-table (~> 0.12)
12
+
13
+ GEM
14
+ remote: https://rubygems.org/
15
+ specs:
16
+ coderay (1.1.3)
17
+ diff-lcs (1.5.0)
18
+ method_source (1.0.0)
19
+ pastel (0.8.0)
20
+ tty-color (~> 0.5)
21
+ pry (0.14.2)
22
+ coderay (~> 1.1)
23
+ method_source (~> 1.0)
24
+ pry-nav (1.0.0)
25
+ pry (>= 0.9.10, < 0.15)
26
+ rake (13.0.6)
27
+ rake-compiler (1.2.1)
28
+ rake
29
+ rspec (3.12.0)
30
+ rspec-core (~> 3.12.0)
31
+ rspec-expectations (~> 3.12.0)
32
+ rspec-mocks (~> 3.12.0)
33
+ rspec-core (3.12.2)
34
+ rspec-support (~> 3.12.0)
35
+ rspec-expectations (3.12.3)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.12.0)
38
+ rspec-mocks (3.12.5)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.12.0)
41
+ rspec-support (3.12.0)
42
+ strings (0.2.1)
43
+ strings-ansi (~> 0.2)
44
+ unicode-display_width (>= 1.5, < 3.0)
45
+ unicode_utils (~> 1.4)
46
+ strings-ansi (0.2.0)
47
+ thor (1.2.2)
48
+ tty (0.1.0)
49
+ tty-box (0.7.0)
50
+ pastel (~> 0.8)
51
+ strings (~> 0.2.0)
52
+ tty-cursor (~> 0.7)
53
+ tty-color (0.6.0)
54
+ tty-command (0.10.1)
55
+ pastel (~> 0.8)
56
+ tty-cursor (0.7.1)
57
+ tty-editor (0.7.0)
58
+ tty-prompt (~> 0.22)
59
+ tty-prompt (0.23.1)
60
+ pastel (~> 0.8)
61
+ tty-reader (~> 0.8)
62
+ tty-reader (0.9.0)
63
+ tty-cursor (~> 0.7)
64
+ tty-screen (~> 0.8)
65
+ wisper (~> 2.0)
66
+ tty-screen (0.8.1)
67
+ tty-table (0.12.0)
68
+ pastel (~> 0.8)
69
+ strings (~> 0.2.0)
70
+ tty-screen (~> 0.8)
71
+ unicode-display_width (2.4.2)
72
+ unicode_utils (1.4.0)
73
+ wisper (2.0.1)
74
+
75
+ PLATFORMS
76
+ arm64-darwin-22
77
+ x86_64-linux
78
+
79
+ DEPENDENCIES
80
+ poke!
81
+ pry
82
+ pry-nav
83
+ rake
84
+ rake-compiler
85
+ rspec
86
+ tty
87
+
88
+ BUNDLED WITH
89
+ 2.4.3
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 MrBananaLord
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # Poke
2
+ Manage your curl requests with ease
3
+
4
+ ## Copyright
5
+
6
+ Copyright (c) 2023 MrBananaLord. See [MIT License](LICENSE.txt) for further details.
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rake/extensiontask"
9
+
10
+ task build: :compile
11
+
12
+ Rake::ExtensionTask.new("poke") do |ext|
13
+ ext.lib_dir = "lib/poke"
14
+ end
15
+
16
+ task default: %i[clobber compile spec]
data/exe/poke ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ lib_path = File.expand_path('../lib', __dir__)
5
+ $:.unshift(lib_path) if !$:.include?(lib_path)
6
+ require 'poke/cli'
7
+
8
+ Signal.trap('INT') do
9
+ warn("\n#{caller.join("\n")}: interrupted")
10
+ exit(1)
11
+ end
12
+
13
+ begin
14
+ # run default command if the first command is not recognised
15
+ Poke::CLI.start(ARGV)
16
+ rescue Poke::CLI::Error => err
17
+ puts "ERROR: #{err.message}"
18
+ exit 1
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+
5
+ create_makefile("poke/poke")
data/ext/poke/poke.c ADDED
@@ -0,0 +1,9 @@
1
+ #include "poke.h"
2
+
3
+ VALUE rb_mPoke;
4
+
5
+ void
6
+ Init_poke(void)
7
+ {
8
+ rb_mPoke = rb_define_module("Poke");
9
+ }
data/ext/poke/poke.h ADDED
@@ -0,0 +1,6 @@
1
+ #ifndef POKE_H
2
+ #define POKE_H 1
3
+
4
+ #include "ruby.h"
5
+
6
+ #endif /* POKE_H */
data/lib/poke/cli.rb ADDED
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+
5
+ module Poke
6
+ # Handle the application command line parsing
7
+ # and the dispatch to various command objects
8
+ #
9
+ # @api public
10
+ class CLI < Thor
11
+ desc 'version', 'poke version'
12
+ def version
13
+ require_relative 'version'
14
+ puts "v#{Poke::VERSION}"
15
+ end
16
+ map %w[--version -v] => :version
17
+
18
+ desc 'env', 'Display and edit environments'
19
+ method_option :help, aliases: '-h', type: :boolean,
20
+ desc: 'Display usage information'
21
+ def env(*)
22
+ if options[:help]
23
+ invoke :help, ['env']
24
+ else
25
+ require_relative 'commands/env'
26
+ Poke::Commands::Env.new(options).execute
27
+ end
28
+ end
29
+
30
+ desc 'curl', 'Find and execute a request'
31
+ method_option :help, aliases: '-h', type: :boolean, desc: 'Display usage information'
32
+ method_option :env, aliases: '-e', type: :string, desc: 'Set target environment'
33
+ method_option :open, aliases: '-o', type: :string, desc: 'Open request in the editor'
34
+ method_option :set_name, aliases: '-N', type: :string, desc: 'Set request name'
35
+ method_option :name, aliases: '-n', type: :string, desc: 'Find request by name'
36
+ def curl(*)
37
+ if options[:help]
38
+ invoke :help, ['curl']
39
+ else
40
+ require_relative 'commands/curl'
41
+ Poke::Commands::Curl.new(options).execute
42
+ end
43
+ end
44
+
45
+ desc 'response', 'Print out last response'
46
+ method_option :help, aliases: '-h', type: :boolean, desc: 'Display usage information'
47
+ def response(*)
48
+ if options[:help]
49
+ invoke :help, ['response']
50
+ else
51
+ require_relative 'commands/response'
52
+ Poke::Commands::Response.new(options).execute
53
+ end
54
+ end
55
+
56
+ desc 'config', 'Manage poke config'
57
+ method_option :help, aliases: '-h', type: :boolean, desc: 'Display usage information'
58
+ method_option :open, aliases: '-o', type: :string, desc: 'Open config in the editor'
59
+ def config(*)
60
+ if options[:help]
61
+ invoke :help, ['response']
62
+ else
63
+ require_relative 'commands/config'
64
+ Poke::Commands::Config.new(options).execute
65
+ end
66
+ end
67
+
68
+ require_relative 'commands/lru'
69
+ register Poke::Commands::Lru, 'lru', 'lru [SUBCOMMAND]', 'Manage usage statistics'
70
+
71
+ default_task :curl
72
+
73
+ def self.exit_on_failure?
74
+ true
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Poke
4
+ class Command
5
+ class NotImplementedError < StandardError; end
6
+
7
+ def initialize(options = {})
8
+ @options = options
9
+ end
10
+
11
+ def execute(args = {})
12
+ run(*args)
13
+ end
14
+
15
+ private
16
+
17
+ def run
18
+ raise NotImplementedError
19
+ end
20
+ end
21
+ end
@@ -0,0 +1 @@
1
+ #
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../command'
4
+ require_relative '../config'
5
+
6
+ require 'tty-command'
7
+ require 'tty-editor'
8
+
9
+ module Poke
10
+ module Commands
11
+ class Config < Poke::Command
12
+ private
13
+
14
+ def run(output: $stdout)
15
+ return TTY::Editor.open(Poke::Config::PATH) if @options.fetch(:open, nil)
16
+
17
+ out, _err = TTY::Command.new(printer: :null).run("cat #{Poke::Config::PATH}")
18
+ output << "#{out}\n"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../command'
4
+ require_relative '../group'
5
+ require_relative '../config'
6
+ require_relative '../paint'
7
+ require_relative '../last_recently_used'
8
+
9
+ require 'pathname'
10
+ require 'pastel'
11
+
12
+ require 'tty-command'
13
+ require 'tty-editor'
14
+ require 'tty-table'
15
+
16
+ module Poke
17
+ module Commands
18
+ class Curl < Poke::Command
19
+ WRITE_OUT_FIELDS = {
20
+ 'response_code' => ->(e) { e },
21
+ 'size_download' => ->(e) { format('%.2fkB', (e / 1000)) },
22
+ 'time_appconnect' => ->(e) { format('%.2fms', (e * 1000)) },
23
+ 'time_connect' => ->(e) { format('%.2fms', (e * 1000)) },
24
+ 'time_namelookup' => ->(e) { format('%.2fms', (e * 1000)) },
25
+ 'time_pretransfer' => ->(e) { format('%.2fms', (e * 1000)) },
26
+ 'time_redirect' => ->(e) { format('%.2fms', (e * 1000)) },
27
+ 'time_starttransfer' => ->(e) { format('%.2fms', (e * 1000)) },
28
+ 'time_total' => ->(e) { format('%.2fms', (e * 1000)) }
29
+ }.freeze
30
+
31
+ private
32
+
33
+ def run(output: $stdout, errors: $stderr)
34
+ if (name = @options.fetch(:name, nil))
35
+ request = Request.find_by_name(Config.find_request_name_by_alias(name))
36
+ else
37
+ choices = Request.all.sort_by(&:position).map(&:name)
38
+ name = TTY::Prompt.new.select('Select the endpoint', choices, filter: true, quiet: true)
39
+ request = Request.find_by_name(name)
40
+ end
41
+
42
+ request.use!
43
+ request.group.use!
44
+
45
+ if (name = @options.fetch(:set_name, nil))
46
+ Config.set_alias!(name, request.name)
47
+ output << "#{request.name} aliased to #{name}\n\n"
48
+ return
49
+ end
50
+
51
+ return TTY::Editor.open(request.path) if @options.fetch(:open, nil)
52
+
53
+ env = @options.fetch(:env, request.group.config.default_env)
54
+ raise Poke::GroupConfig::InvalidEnv unless request.group.config.valid_env?(env)
55
+
56
+ curl_command, comments = build_command(request.path)
57
+
58
+ table = TTY::Table.new(
59
+ [['ENV', env]] +
60
+ request.group.config.variables(env).to_a +
61
+ [['CMD', curl_command]] +
62
+ [['COMMENTS', comments]]
63
+ )
64
+ errors << table.render(:unicode, multiline: true, padding: [0, 1, 0, 1])
65
+ errors << "\n\n"
66
+
67
+ command = TTY::Command.new(printer: :null).run!(
68
+ request.group.config.variables(env),
69
+ curl_command
70
+ )
71
+ if command.failure?
72
+ output << Pastel.new.decorate(command.err, :red)
73
+ output << Poke::Paint.farewell
74
+ return
75
+ else
76
+ errors << Pastel.new.decorate(command.err, :green)
77
+ errors << "\n"
78
+ stats = JSON.parse(command.out)
79
+
80
+ table = TTY::Table.new(WRITE_OUT_FIELDS.map { |field, lambda| [field, lambda.call(stats[field])] }.to_a)
81
+ errors << table.render(:unicode, padding: [0, 1, 0, 1])
82
+ errors << "\n\n"
83
+
84
+ errors << " source file: #{Pastel.new.decorate(request.path.to_s, :magenta)}\n"
85
+ errors << " request url: #{Pastel.new.decorate(stats['url'], :blue)}\n"
86
+ errors << " response path: #{Pastel.new.decorate(stats['filename_effective'], :magenta)}\n"
87
+ end
88
+
89
+ output << "\n#{File.read(Poke::Config.response_path)}\n"
90
+ end
91
+
92
+ def build_command(path)
93
+ out = TTY::Command.new(printer: :null).run("cat #{path}").out
94
+ out << "\n" unless out[-1] == "\n"
95
+
96
+ command_lines = out.split(" \\\n").map(&:strip)
97
+
98
+ comments = command_lines.filter { |line| line.start_with?('#') }.join(" \\\n")
99
+ command_lines = command_lines.reject { |line| line.start_with?('#') }
100
+ command_lines += additional_curl_params
101
+
102
+ [command_lines.join(" \\\n "), comments]
103
+ end
104
+
105
+ # rubocop:disable Style/FormatStringToken
106
+ def additional_curl_params
107
+ [
108
+ "-o #{Poke::Config.response_path}",
109
+ '-w "%{json}"'
110
+ ]
111
+ end
112
+ # rubocop:enable Style/FormatStringToken
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../command'
4
+ require_relative '../group'
5
+ require_relative '../paint'
6
+
7
+ require 'pastel'
8
+
9
+ require 'tty-box'
10
+ require 'tty-command'
11
+ require 'tty-cursor'
12
+ require 'tty-editor'
13
+ require 'tty-prompt'
14
+ require 'tty-table'
15
+
16
+ module Poke
17
+ module Commands
18
+ class Env < Poke::Command
19
+ private
20
+
21
+ def run(output: $stdout)
22
+ groups = Poke::Group.all
23
+ group_name = TTY::Prompt.new.select('Select the group', groups.map(&:name), filter: true, quiet: true)
24
+ group = groups.find { |g| g.name == group_name }
25
+
26
+ manage_group_config(output:, group:)
27
+ end
28
+
29
+ def cursor
30
+ @cursor ||= TTY::Cursor
31
+ end
32
+
33
+ def pastel
34
+ @pastel ||= Pastel.new
35
+ end
36
+
37
+ def manage_group_config(output:, group:)
38
+ output << cursor.save
39
+
40
+ render(output, group)
41
+
42
+ loop do
43
+ case TTY::Prompt.new.keypress('', quiet: true)
44
+ when 'c'
45
+ group.config.default_env = TTY::Prompt.new.select(
46
+ 'Select new default environment', group.config.envs,
47
+ filter: true, quiet: true
48
+ )
49
+ group.save_config!
50
+
51
+ render(output, group)
52
+ when 'e'
53
+ TTY::Editor.open(group.config_path)
54
+ break
55
+ when 'q'
56
+ break
57
+ end
58
+ end
59
+ end
60
+
61
+ def render(output, group)
62
+ output << cursor.restore
63
+ output << cursor.clear_screen_down
64
+
65
+ title = ['| ', pastel.decorate(group.name.upcase, :yellow, :underline, :bold), ' |'].join
66
+ footer = '| q → quit | c → change default env | e → edit config file |'
67
+ table = TTY::Table.new(
68
+ header: ['env \\ var', *group.config.variables(group.config.default_env).keys],
69
+ rows: group.config.envs.map do |name|
70
+ [name, *group.config.variables(name).values]
71
+ end
72
+ )
73
+
74
+ # output << "\n"
75
+ output << TTY::Box.frame(align: :center, title: { top_center: title, bottom_center: footer }) do
76
+ [
77
+ "(default env: #{group.config.default_env})\n\n",
78
+ table.render(:unicode, multiline: true, padding: [0, 1, 0, 1])
79
+ ].join('')
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../command'
4
+ require_relative '../../config'
5
+
6
+ require 'tty-command'
7
+
8
+ module Poke
9
+ module Commands
10
+ class Lru
11
+ class Cat < Poke::Command
12
+ private
13
+
14
+ def run(output: $stdout)
15
+ out, _err = TTY::Command.new(printer: :null).run("cat #{Poke::Config.root_path}/lru.json")
16
+ output << out.to_s
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../command'
4
+ require_relative '../../config'
5
+
6
+ require 'tty-command'
7
+
8
+ module Poke
9
+ module Commands
10
+ class Lru
11
+ class Reset < Poke::Command
12
+ private
13
+
14
+ def run
15
+ TTY::Command.new(printer: :null).run("echo {} > #{Poke::Config.root_path}/lru.json")
16
+
17
+ require_relative 'cat'
18
+ Poke::Commands::Lru::Cat.new(@options).execute
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../config'
4
+
5
+ module Poke
6
+ module Commands
7
+ class Lru < Thor
8
+ namespace :lru
9
+
10
+ desc 'reset', 'Reset usage statistics'
11
+ def reset(*)
12
+ if options[:help]
13
+ invoke :help, ['reset']
14
+ else
15
+ require_relative 'lru/reset'
16
+ Poke::Commands::Lru::Reset.new(options).execute
17
+ end
18
+ end
19
+
20
+ desc 'cat', 'Display usage statistics'
21
+ def cat(*)
22
+ if options[:help]
23
+ invoke :help, ['cat']
24
+ else
25
+ require_relative 'lru/cat'
26
+ Poke::Commands::Lru::Cat.new(options).execute
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../command'
4
+
5
+ require 'tty-command'
6
+
7
+ module Poke
8
+ module Commands
9
+ class Response < Poke::Command
10
+ private
11
+
12
+ def run(output: $stdout)
13
+ out, _err = TTY::Command.new(printer: :null).run("cat #{Poke::Config.root_path}/response.json")
14
+ output << "#{out}\n"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Poke
6
+ class Config
7
+ class NotFound < StandardError; end
8
+
9
+ PATH = "#{Dir.home}/.poke.json".freeze
10
+ DEFAULT_ROOT_PATH = "#{Dir.home}/.poke".freeze
11
+
12
+ def self.all
13
+ @all ||= begin
14
+ config = File.exist?(PATH) ? JSON.parse(File.read(PATH)) : {}
15
+ config['root_path'] ||= DEFAULT_ROOT_PATH
16
+ config
17
+ end
18
+ end
19
+
20
+ def self.root_path
21
+ all['root_path']
22
+ end
23
+
24
+ def self.response_path
25
+ [root_path, 'response.json'].join('/')
26
+ end
27
+
28
+ def self.lru_path
29
+ [root_path, 'lru.json'].join('/')
30
+ end
31
+
32
+ def self.find_request_name_by_alias(value)
33
+ result = all.dig('aliases', value)
34
+ raise NotFound unless result
35
+
36
+ result
37
+ end
38
+
39
+ def self.set_alias!(value, path)
40
+ all['aliases'] ||= {}
41
+ all['aliases'][value] = path.to_s
42
+ File.write(PATH, all.to_json)
43
+ end
44
+ end
45
+ end
data/lib/poke/group.rb ADDED
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './group_config'
4
+ require_relative './request'
5
+
6
+ require 'pathname'
7
+ require 'json'
8
+
9
+ require 'tty-command'
10
+
11
+ module Poke
12
+ class Group
13
+ def self.all
14
+ @all ||= begin
15
+ config_paths = TTY::Command.new(printer: :null).run("find #{Dir.home}/.poke -name 'config.json'").out.split("\n")
16
+
17
+ config_paths.map do |config_path|
18
+ name = ::Pathname.new(config_path).parent.basename.to_s
19
+ new(name:, config_path:)
20
+ end
21
+ end
22
+ end
23
+
24
+ def self.from_request_path(path)
25
+ group_name = path.to_s.gsub(%r{#{Config.root_path}/([^\/]+)/.*}, '\1')
26
+ new(name: group_name, config_path: "#{Config.root_path}/#{group_name}/config.json")
27
+ end
28
+
29
+ attr_reader :name, :config_path, :config
30
+
31
+ def initialize(name:, config_path:)
32
+ @name = name
33
+ @config_path = config_path
34
+ @config = Poke::GroupConfig.from_path(config_path)
35
+ end
36
+
37
+ def save_config!
38
+ @config.save_to(@config_path)
39
+ end
40
+
41
+ def requests
42
+ @requests ||= Request.all.filter { |request| request.group_name == name }
43
+ end
44
+
45
+ def position
46
+ LastRecentlyUsed.position(namespace: 'groups', key: name.to_s)
47
+ end
48
+
49
+ def use!
50
+ LastRecentlyUsed.use!(namespace: 'groups', key: name.to_s)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Poke
6
+ class GroupConfig
7
+ class InvalidEnv < StandardError; end
8
+
9
+ def self.from_path(path)
10
+ new(JSON.parse(File.read(path)))
11
+ end
12
+
13
+ def initialize(values = {})
14
+ @values = values
15
+ end
16
+
17
+ def save_to(path)
18
+ File.write(path, @values.to_json)
19
+ end
20
+
21
+ def valid_env?(env)
22
+ @values['envs'].key?(env)
23
+ end
24
+
25
+ def default_env
26
+ @values.fetch('default_env', @values['envs'].keys.first)
27
+ end
28
+
29
+ def default_env=(value)
30
+ raise InvalidEnv unless valid_env?(value)
31
+
32
+ @values['default_env'] = value
33
+ end
34
+
35
+ def envs
36
+ @values['envs'].keys
37
+ end
38
+
39
+ def variables(env)
40
+ @values['envs'][env]
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './config'
4
+
5
+ require 'json'
6
+
7
+ module Poke
8
+ class LastRecentlyUsed
9
+ def self.all
10
+ @all ||= if File.exist?(Poke::Config.lru_path)
11
+ JSON.parse(File.read(Poke::Config.lru_path))
12
+ else
13
+ {}
14
+ end
15
+ end
16
+
17
+ def self.groups
18
+ all['groups'] ||= {}
19
+ end
20
+
21
+ def self.requests
22
+ all['requests'] ||= {}
23
+ end
24
+
25
+ def self.use!(namespace:, key:)
26
+ all[namespace] ||= {}
27
+ all[namespace]['lru'] ||= []
28
+ all[namespace]['lru'].delete(key.to_s)
29
+ all[namespace]['lru'].unshift(key.to_s)
30
+
31
+ File.write(Poke::Config.lru_path, all.to_json)
32
+ end
33
+
34
+ def self.position(namespace:, key:)
35
+ all.dig(namespace, 'lru')&.index(key) || Float::INFINITY
36
+ end
37
+ end
38
+ end
data/lib/poke/paint.rb ADDED
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pastel'
4
+
5
+ require 'tty-cursor'
6
+
7
+ module Poke
8
+ class Paint
9
+ def self.welcome
10
+ [
11
+ Pastel.new.decorate(' POKE > ', :black, :on_yellow),
12
+ "\n\n"
13
+ ].join
14
+ end
15
+
16
+ def self.farewell
17
+ [
18
+ "\n",
19
+ Pastel.new.decorate(' < kthxbye ', :black, :on_yellow),
20
+ "\n"
21
+ ].join
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './group'
4
+
5
+ require 'pathname'
6
+
7
+ require 'tty-command'
8
+
9
+ module Poke
10
+ class Request
11
+ def self.all
12
+ @all ||= begin
13
+ request_paths = TTY::Command.new(printer: :null).run("find #{Dir.home}/.poke -name '*.curl'").out.split("\n")
14
+
15
+ request_paths.map do |path|
16
+ path = Pathname.new(path)
17
+
18
+ group_name = path.to_s.gsub(%r{#{Config.root_path}/([^\/]+)/.*}, '\1')
19
+ name = path.to_s.gsub(%r{#{Config.root_path}/(.*)\.curl}, '\1')
20
+
21
+ new(group_name:, name:, path:)
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.find_by_name(name)
27
+ all.find { |request| request.name == name }
28
+ end
29
+
30
+ attr_reader :path, :name, :group_name
31
+
32
+ def initialize(path:, name:, group_name:)
33
+ @path = path
34
+ @name = name
35
+ @group_name = group_name
36
+ end
37
+
38
+ def position
39
+ LastRecentlyUsed.position(namespace: 'requests', key: name.to_s)
40
+ end
41
+
42
+ def use!
43
+ LastRecentlyUsed.use!(namespace: 'requests', key: name.to_s)
44
+ end
45
+
46
+ def group
47
+ Group.all.find { |g| g.name == group_name }
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Poke
4
+ VERSION = '0.1.0'
5
+ end
data/lib/poke.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'poke/config'
4
+ require_relative 'poke/paint'
5
+ require_relative 'poke/group'
6
+ require_relative 'poke/version'
7
+
8
+ module Poke
9
+ end
data/sig/poke.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Poke
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: poke-your-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jan Bator
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pastel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tty-box
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: tty-command
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: tty-cursor
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.7'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: tty-editor
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.6'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: tty-table
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.12'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.12'
111
+ description:
112
+ email:
113
+ - jan.bator@gmail.com
114
+ executables:
115
+ - poke
116
+ extensions:
117
+ - ext/poke/extconf.rb
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".editorconfig"
121
+ - ".rspec"
122
+ - ".rubocop.yml"
123
+ - ".ruby-version"
124
+ - CODE_OF_CONDUCT.md
125
+ - Gemfile
126
+ - Gemfile.lock
127
+ - LICENSE.txt
128
+ - README.md
129
+ - Rakefile
130
+ - exe/poke
131
+ - ext/poke/extconf.rb
132
+ - ext/poke/poke.c
133
+ - ext/poke/poke.h
134
+ - lib/poke.rb
135
+ - lib/poke/cli.rb
136
+ - lib/poke/command.rb
137
+ - lib/poke/commands/.gitkeep
138
+ - lib/poke/commands/config.rb
139
+ - lib/poke/commands/curl.rb
140
+ - lib/poke/commands/env.rb
141
+ - lib/poke/commands/lru.rb
142
+ - lib/poke/commands/lru/cat.rb
143
+ - lib/poke/commands/lru/reset.rb
144
+ - lib/poke/commands/response.rb
145
+ - lib/poke/config.rb
146
+ - lib/poke/group.rb
147
+ - lib/poke/group_config.rb
148
+ - lib/poke/last_recently_used.rb
149
+ - lib/poke/paint.rb
150
+ - lib/poke/request.rb
151
+ - lib/poke/version.rb
152
+ - sig/poke.rbs
153
+ homepage: https://github.com/MrBananaLord/poke
154
+ licenses:
155
+ - MIT
156
+ metadata:
157
+ homepage_uri: https://github.com/MrBananaLord/poke
158
+ source_code_uri: https://github.com/MrBananaLord/poke
159
+ post_install_message:
160
+ rdoc_options: []
161
+ require_paths:
162
+ - lib
163
+ required_ruby_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: 2.6.0
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ requirements: []
174
+ rubygems_version: 3.4.14
175
+ signing_key:
176
+ specification_version: 4
177
+ summary: manage curl requests
178
+ test_files: []