dummy_oscar 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5584d49e06bc4edcee5e792298e85a72069935a162125bbb59cb0d3d2ae3cb73
4
+ data.tar.gz: f739356d1a5eea86e3acb3acb0967a49e0aeff9f994e6f35d97aafa1467f5f3d
5
+ SHA512:
6
+ metadata.gz: 5f7a7b14cd5983b628b03c75b9e53b1032d354a06e92704a56565934f80f957a560a52ed2a4e5269ab0e47fccdf6d34516cbbd2b78860dd4e1d56f2a7a6bf181
7
+ data.tar.gz: 9108b0208db1eeefa880f082bc59fbc9be98c334ec399fc52d83a5a7e667b3261b6777fe55b53be320655df9f42e818a6e58050083ada550261e939d35cb4bf2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-04-04
4
+
5
+ - Initial release
@@ -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 yuuji.yaginuma@gmail.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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Yuji Yaginuma
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,81 @@
1
+ # DummyOscar
2
+
3
+ DummyOscar is tool for creating a dummy HTTP server and client.
4
+
5
+ ## Server
6
+
7
+ You can define a path that returns a dummy response by YAML file. You can use ERB inside the file.
8
+
9
+ Example:
10
+
11
+ ```yml
12
+ # sample.yaml
13
+ paths:
14
+ /:
15
+ get:
16
+ response:
17
+ status_code: 200
18
+ body: "hello,world"
19
+ /users:
20
+ post:
21
+ response:
22
+ status_code: 204
23
+ get:
24
+ response:
25
+ status_code: 200
26
+ body: '<%= File.read('test/fixtures/users.json') %>'
27
+ /users/dummy.json:
28
+ get:
29
+ response:
30
+ status_code: 200
31
+ body: <%= {name: 'dummy'}.to_json %>
32
+ content_type: 'application/json'
33
+ /users/.+/books:
34
+ get:
35
+ response:
36
+ status_code: 200
37
+ body: '[{"title":"Abc"},{"title":"deF"}]'
38
+ content_type: 'application/json'
39
+ ```
40
+
41
+ You can now start the dummy server.
42
+
43
+ ```bash
44
+ $ dummy_oscar s -C sample.yaml
45
+ ```
46
+
47
+ ```bash
48
+ $ curl http://localhost:8282
49
+ hello,world
50
+ $ curl http://localhost:8282/users/1/books
51
+ [{"title":"Abc"},{"title":"deF"}]
52
+ ```
53
+
54
+ If you want to use custom methods inside the YAML, you can pass a Ruby file that will load during the parsing of the YAML file.
55
+
56
+ ```bash
57
+ $ dummy_oscar s -C sample.yaml -r ./library_for_config.rb
58
+ ```
59
+
60
+ ## Client
61
+
62
+ You can define a path and request body by YAML file. You can use ERB inside the file.
63
+
64
+ Example:
65
+
66
+ ```yml
67
+ requests:
68
+ hello:
69
+ path: "/"
70
+ method: "get"
71
+ create_user:
72
+ path: "/users"
73
+ method: "post"
74
+ body: <%= {name: 'dummy'}.to_json %>
75
+ ```
76
+
77
+ You can send request like the following.
78
+
79
+ ```bash
80
+ $ dummy_oscar c -C sample.yml -h http://localhost:5252 -c hello
81
+ ```
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create do |t|
7
+ t.verbose = true
8
+ end
9
+
10
+ task default: :test
data/exe/dummy_oscar ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "dummy_oscar"
6
+
7
+ DummyOscar::Cli.start
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require "erb"
5
+
6
+ # NOTE: These requires are for using some methods in a config file.
7
+ require "json"
8
+
9
+ class DummyOscar::App
10
+ private_class_method :new
11
+
12
+ class << self
13
+ def build(config_file, library:)
14
+ new(config_file, library: library)
15
+ end
16
+ end
17
+
18
+ def initialize(config_file, library:)
19
+ @router = DummyOscar::Router.new
20
+ parse_config_file(config_file, library: library)
21
+ end
22
+
23
+ def app(env)
24
+ input = "'#{env['rack.input'].read}'"
25
+ $stdout.puts "Started #{env["REQUEST_METHOD"]} #{env["PATH_INFO"]} #{input}"
26
+
27
+ route = @router.find(path: env["PATH_INFO"], method: env["REQUEST_METHOD"])
28
+ if route
29
+ headers = {}
30
+ headers["Content-Type"] = route.response["content_type"] if route.response["content_type"]
31
+ return [route.response["status_code"], headers, [route.response["body"].to_s]]
32
+ end
33
+
34
+ [404, {}, ["Not found"]]
35
+ end
36
+
37
+ private
38
+
39
+ def parse_config_file(config_file, library:)
40
+ require(library) if library
41
+
42
+ source = ERB.new(File.read(config_file)).result(binding)
43
+ config = YAML.load(source)
44
+ config["paths"].each do |path, list|
45
+ list.each do |method, response|
46
+ raise DummyOscar::ParseConfigError, "You need to specify `:response` in the config file." if response["response"].nil?
47
+ @router.add(path: path, method: method, response: response["response"])
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+
5
+ class DummyOscar::Cli
6
+ class << self
7
+ def start(argv = ARGV)
8
+ cli = new(argv)
9
+ options = cli.parse
10
+
11
+ if options[:mode] == "s"
12
+ DummyOscar::Server.new(config_file: options[:config], port: options[:port], library: options[:library]).start
13
+ else
14
+ DummyOscar::Client.new(config_file: options[:config], command: options[:command], library: options[:library], host: options[:host]).start
15
+ end
16
+ end
17
+ end
18
+
19
+ CMD = 'dummy_oscar'
20
+ USAGE = <<~USAGE
21
+ sub commands are:
22
+ s : run with server mode
23
+ c : run with client mode
24
+ See '#{CMD} COMMAND --help' for more information on a specific command.
25
+ USAGE
26
+
27
+ def initialize(argv)
28
+ @argv = argv
29
+ @options = {}
30
+ end
31
+
32
+ def parse
33
+ global_command.order!(@argv)
34
+ @options[:mode] = @argv.shift
35
+ subcommand = subcommands[@options[:mode]]
36
+ unless subcommand
37
+ puts global_command.help
38
+ exit!
39
+ end
40
+
41
+ subcommand.order!(@argv)
42
+ raise OptionParser::MissingArgument.new("-C") if @options[:config].nil?
43
+ @options
44
+ end
45
+
46
+ private
47
+
48
+ def global_command
49
+ @global_command ||= OptionParser.new do |opts|
50
+ opts.banner = "Usage: #{CMD} [options] [subcommand [options]]"
51
+ opts.separator ""
52
+ opts.separator USAGE
53
+ end
54
+ end
55
+
56
+ def subcommands
57
+ {
58
+ 's' => OptionParser.new do |opts|
59
+ opts.banner = "Usage: dummy_oscar s [options]"
60
+
61
+ opts.on("-p", "--port NUMBER", "Port number", Integer) do |v|
62
+ @options[:port] = v.to_i
63
+ end
64
+
65
+ opts.on("-C", "--config PATH", "Config file path") do |v|
66
+ @options[:config] = v
67
+ end
68
+
69
+ opts.on("-r", "--require PATH", "require the library before parse a config file") do |v|
70
+ @options[:library] = v
71
+ end
72
+ end,
73
+ 'c' => OptionParser.new do |opts|
74
+ opts.banner = "Usage: dummy_oscar c [options]"
75
+
76
+ opts.on("-C", "--config PATH", "Config file path") do |v|
77
+ @options[:config] = v
78
+ end
79
+
80
+ opts.on("-r", "--require PATH", "require the library before parse a config file") do |v|
81
+ @options[:library] = v
82
+ end
83
+
84
+ opts.on("-c", "--command NAME", "Command") do |v|
85
+ @options[:command] = v
86
+ end
87
+
88
+ opts.on("-h", "--host NAME", "Host name") do |v|
89
+ @options[:host] = v
90
+ end
91
+ end
92
+ }
93
+ end
94
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "httparty"
4
+ require "uri"
5
+
6
+ class DummyOscar::Client
7
+ def initialize(config_file:, host:, command:, library: nil)
8
+ raise DummyOscar::ArgumentError, "Pleaes specify command argument." if command.nil?
9
+
10
+ @command = command
11
+ @commands = {}
12
+ @host = host
13
+ parse_config_file(config_file: config_file, library: library)
14
+ end
15
+
16
+ def start
17
+ request = @commands[@command]
18
+ raise DummyOscar::ArgumentError, "'#{@command}' did not find." if request.nil?
19
+
20
+ request_options = {}
21
+ request_options[:body] = request["body"] unless request["body"].nil?
22
+ request_options[:query] = request["query"] unless request["query"].nil?
23
+
24
+ response = HTTParty.public_send(request["method"].downcase, URI.join(@host, request["path"]).to_s, request_options)
25
+ $stdout.puts response.body
26
+ end
27
+
28
+ private
29
+
30
+ def parse_config_file(config_file:, library:)
31
+ require(library) if library
32
+
33
+ source = ERB.new(File.read(config_file)).result(binding)
34
+ config = YAML.load(source)
35
+
36
+ config["requests"].each do |name, request|
37
+ if request["path"].nil? || request["method"].nil?
38
+ raise DummyOscar::ParseConfigError, "You need to specify both `:path` and `:method` in the config file."
39
+ end
40
+ @commands[name] = request
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DummyOscar
4
+ class Error < StandardError; end
5
+ class ParseConfigError < Error; end
6
+ class ArgumentError < Error; end
7
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DummyOscar::Router
4
+ Route = Struct.new(:path, :method, :response)
5
+
6
+ def initialize
7
+ @routes = []
8
+ end
9
+
10
+ def add(path:, method:, response:)
11
+ @routes << Route.new(path, method.downcase, response)
12
+ end
13
+
14
+ def find(path:, method:)
15
+ @routes.detect do |route|
16
+ /\A(?:#{route.path})\z/.match?(path) && route.method == method.downcase
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "puma"
4
+ require "puma/launcher"
5
+
6
+ class DummyOscar::Server
7
+ DEFAULT_PORT = 8282
8
+
9
+ def initialize(config_file:, port: nil, library: nil)
10
+ @port = port || DEFAULT_PORT
11
+ @app_builder = DummyOscar::App.build(config_file, library: library)
12
+ end
13
+
14
+ def start
15
+ conf = Puma::Configuration.new do |user_config|
16
+ user_config.threads(1, 4)
17
+ user_config.port(@port)
18
+ user_config.app { |env| @app_builder.app(env) }
19
+ end
20
+ $stdout.puts("Listening on http://localhost:#{@port}")
21
+ Puma::Launcher.new(conf, log_writer: Puma::LogWriter.null).run
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DummyOscar
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "dummy_oscar/version"
4
+ require_relative "dummy_oscar/errors"
5
+ require_relative "dummy_oscar/server"
6
+ require_relative "dummy_oscar/client"
7
+ require_relative "dummy_oscar/app"
8
+ require_relative "dummy_oscar/router"
9
+ require_relative "dummy_oscar/cli"
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dummy_oscar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuji Yaginuma
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: puma
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httpparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - yuuji.yaginuma@gmail.com
44
+ executables:
45
+ - dummy_oscar
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - CHANGELOG.md
50
+ - CODE_OF_CONDUCT.md
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - exe/dummy_oscar
55
+ - lib/dummy_oscar.rb
56
+ - lib/dummy_oscar/app.rb
57
+ - lib/dummy_oscar/cli.rb
58
+ - lib/dummy_oscar/client.rb
59
+ - lib/dummy_oscar/errors.rb
60
+ - lib/dummy_oscar/router.rb
61
+ - lib/dummy_oscar/server.rb
62
+ - lib/dummy_oscar/version.rb
63
+ homepage: https://github.com/y-yagi/dummy_oscar
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ homepage_uri: https://github.com/y-yagi/dummy_oscar
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 3.0.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.5.3
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: The tool for creating a dummy HTTP server and client.
87
+ test_files: []