macaw_framework 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: 20f895377b3f0eea2494e091fab22b81734f6625d840ed2414c557db4db29ac2
4
+ data.tar.gz: 0ce55bbbbb41a5da4ab1622c72ced7300bfc4cb8b75091702fffb21822415ce2
5
+ SHA512:
6
+ metadata.gz: 033b20344e924fb6cf66a75df40dd18fcee89967738392529b7f6dbab02e7301af275156d81a5601222428a03ae9796af751b048ab09632debfe87fcc5c86516
7
+ data.tar.gz: c2c09a27334e8904f2b7e714a6ac71df95140fe7e088ddcf23e71a29a125c0cc72cce2395939ce18d2e5ebb3998fb28c8a8a0a912de99c9e070004a0b78483a9
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-12-05
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 aria.diniz.dev@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/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in macaw_framework.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Aria Diniz
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,62 @@
1
+ # MacawFramework
2
+
3
+ This is a framework for developing web applications. Please have in mind that this is still a work in progress and
4
+ it is strongly advised to not use it for production purposes for now. Anyone who wishes to contribute is welcome.
5
+
6
+ ## Installation
7
+
8
+ Install the gem and add to the application's Gemfile by executing:
9
+
10
+ $ bundle add macaw_framework
11
+
12
+ If bundler is not being used to manage dependencies, install the gem by executing:
13
+
14
+ $ gem install macaw_framework
15
+
16
+ ## Usage
17
+
18
+ The usage of the framework still very simple. Actually it support 5 HTTP verbs: GET, POST, PUT, PATCH and DELETE.
19
+ For now, the framework can't resolve client request body and headers. The support for this will be included soon.
20
+
21
+ The default server port is 8080. To choose a different port, create a file with the name `application.json`
22
+ in the same directory of the script that will start the application with the following content:
23
+
24
+ ```json
25
+ {
26
+ "macaw": {
27
+ "port": 80
28
+ }
29
+ }
30
+ ```
31
+
32
+ Example of usage:
33
+
34
+ ```ruby
35
+ require 'macaw_framework'
36
+ require 'json'
37
+
38
+ m = Macaw.new
39
+
40
+ m.get('/hello_world') do
41
+ return JSON.pretty_generate({ hello_message: 'Hello World!' }), 200
42
+ end
43
+
44
+ m.start!
45
+ ```
46
+
47
+ The above example will start a server and will create a GET endpoint at localhost/hello_world.
48
+
49
+ The verb methods must always return a String or nil (Used as response) and a number corresponding the
50
+ HTTP Status Code to be returned to the client.
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ariasdiniz/macaw_framework. 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/ariasdiniz/macaw_framework/blob/main/CODE_OF_CONDUCT.md).
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
59
+
60
+ ## Code of Conduct
61
+
62
+ Everyone interacting in the MacawFramework project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ariasdiniz/macaw_framework/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # Error raised when the client calls
5
+ # for a path that doesn't exist.
6
+ class EndpointNotMappedError < StandardError
7
+ def initialize(msg = 'Undefined endpoint')
8
+ super
9
+ end
10
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # Module containing a map for HTTP Status Codes
5
+ # and their respective messages.
6
+ module HttpStatusCode
7
+ ##
8
+ # Http Status Code Map
9
+ HTTP_STATUS_CODE_MAP = {
10
+ 100 => 'Continue',
11
+ 101 => 'Switching Protocols',
12
+ 102 => 'Processing',
13
+ 103 => 'Early Hints',
14
+ 200 => 'OK',
15
+ 201 => 'Created',
16
+ 202 => 'Accepted',
17
+ 203 => 'Non-Authoritative Information',
18
+ 204 => 'No Content',
19
+ 205 => 'Reset Content',
20
+ 206 => 'Partial Content',
21
+ 207 => 'Multi-Status',
22
+ 208 => 'Already Reported',
23
+ 226 => 'IM Used',
24
+ 300 => 'Multiple Choices',
25
+ 301 => 'Moved Permanently',
26
+ 302 => 'Found',
27
+ 303 => 'See Other',
28
+ 304 => 'Not Modified',
29
+ 305 => 'Use Proxy',
30
+ 307 => 'Temporary Redirect',
31
+ 308 => 'Permanent Redirect',
32
+ 400 => 'Bad Request',
33
+ 401 => 'Unauthorized',
34
+ 402 => 'Payment Required',
35
+ 403 => 'Forbidden',
36
+ 404 => 'Not Found',
37
+ 405 => 'Method Not Allowed',
38
+ 406 => 'Not Acceptable',
39
+ 407 => 'Proxy Authentication Required',
40
+ 408 => 'Request Timeout',
41
+ 409 => 'Conflict',
42
+ 410 => 'Gone',
43
+ 411 => 'Length Required',
44
+ 412 => 'Precondition Failed',
45
+ 413 => 'Content Too Large',
46
+ 414 => 'URI Too Long',
47
+ 415 => 'Unsupported Media Type',
48
+ 416 => 'Range Not Satisfiable',
49
+ 417 => 'Expectation Failed',
50
+ 421 => 'Misdirected Request',
51
+ 422 => 'Unprocessable Content',
52
+ 423 => 'Locked',
53
+ 424 => 'Failed Dependency',
54
+ 425 => 'Too Early',
55
+ 426 => 'Upgrade Required',
56
+ 427 => 'Unassigned',
57
+ 428 => 'Precondition Required',
58
+ 429 => 'Too Many Requests',
59
+ 430 => 'Unassigned',
60
+ 431 => 'Request Header Fields Too Large',
61
+ 451 => 'Unavailable For Legal Reasons',
62
+ 500 => 'Internal Server Error',
63
+ 501 => 'Not Implemented',
64
+ 502 => 'Bad Gateway',
65
+ 503 => 'Service Unavailable',
66
+ 504 => 'Gateway Timeout',
67
+ 505 => 'HTTP Version Not Supported',
68
+ 506 => 'Variant Also Negotiates',
69
+ 507 => 'Insufficient Storage',
70
+ 508 => 'Loop Detected',
71
+ 509 => 'Unassigned',
72
+ 510 => 'Not Extended (OBSOLETED)',
73
+ 511 => 'Network Authentication Required'
74
+ }.freeze
75
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MacawFramework
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'macaw_framework/endpoint_not_mapped_error'
4
+ require_relative 'macaw_framework/http_status_code'
5
+ require_relative 'macaw_framework/version'
6
+ require 'socket'
7
+ require 'json'
8
+
9
+ module MacawFramework
10
+ ##
11
+ # Class responsible for creating endpoints and
12
+ # starting the web server.
13
+ class Macaw
14
+ include(HttpStatusCode)
15
+ def initialize
16
+ begin
17
+ config = JSON.parse(File.read('application.json'))
18
+ @port = config['macaw']['port']
19
+ rescue StandardError
20
+ @port ||= 8080
21
+ end
22
+ @port ||= 8080
23
+ end
24
+
25
+ ##
26
+ # Creates a GET endpoint associated
27
+ # with the respective path.
28
+ # @param {String} path
29
+ # @param {Proc} block
30
+ # @return {Integer, String}
31
+ def get(path, &block)
32
+ path_clean = path[0] == '/' ? path[1..].gsub('/', '_') : path.gsub('/', '_')
33
+ define_singleton_method("get_#{path_clean}", block)
34
+ end
35
+
36
+ ##
37
+ # Creates a POST endpoint associated
38
+ # with the respective path.
39
+ # @param {String} path
40
+ # @param {Proc} block
41
+ # @return {String, Integer}
42
+ def post(path, &block)
43
+ path_clean = path[0] == '/' ? path[1..].gsub('/', '_') : path.gsub('/', '_')
44
+ define_singleton_method("post_#{path_clean}", block)
45
+ end
46
+
47
+ ##
48
+ # Creates a PUT endpoint associated
49
+ # with the respective path.
50
+ # @param {String} path
51
+ # @param {Proc} block
52
+ # @return {String, Integer}
53
+ def put(path, &block)
54
+ path_clean = path[0] == '/' ? path[1..].gsub('/', '_') : path.gsub('/', '_')
55
+ define_singleton_method("put_#{path_clean}", block)
56
+ end
57
+
58
+ ##
59
+ # Creates a PATCH endpoint associated
60
+ # with the respective path.
61
+ # @param {String} path
62
+ # @param {Proc} block
63
+ # @return {String, Integer}
64
+ def patch(path, &block)
65
+ path_clean = path[0] == '/' ? path[1..].gsub('/', '_') : path.gsub('/', '_')
66
+ define_singleton_method("patch_#{path_clean}", block)
67
+ end
68
+
69
+ ##
70
+ # Creates a DELETE endpoint associated
71
+ # with the respective path.
72
+ # @param {String} path
73
+ # @param {Proc} block
74
+ # @return {String, Integer}
75
+ def delete(path, &block)
76
+ path_clean = path[0] == '/' ? path[1..].gsub('/', '_') : path.gsub('/', '_')
77
+ define_singleton_method("delete_#{path_clean}", block)
78
+ end
79
+
80
+ ##
81
+ # Starts the web server
82
+ def start!
83
+ server = TCPServer.open(@port)
84
+ puts "Starting server at port #{@port}"
85
+ loop do
86
+ Thread.start(server.accept) do |client|
87
+ method_name = extract_client_info(client)
88
+ raise EndpointNotMappedError unless respond_to?(method_name)
89
+
90
+ message, status = send(method_name)
91
+ status ||= 200
92
+ message ||= 'Ok'
93
+ client.puts "HTTP/1.1 #{status} #{HTTP_STATUS_CODE_MAP[status]} \r\n\r\n#{message}"
94
+ client.close
95
+ rescue EndpointNotMappedError
96
+ client.print "HTTP/1.1 404 Not Found\r\n\r\n"
97
+ client.close
98
+ rescue StandardError
99
+ client.print "HTTP/1.1 500 Internal Server Error\r\n\r\n"
100
+ client.close
101
+ end
102
+ end
103
+ end
104
+
105
+ private
106
+
107
+ ##
108
+ # Method for extracting headers and body from client request
109
+ # STILL IN DEVELOPMENT
110
+ # @todo finish implementation
111
+ def extract_client_info(client)
112
+ method_name = client.gets.gsub('HTTP/1.1', '').gsub('/', '_').strip!.downcase
113
+ method_name.gsub!(' ', '')
114
+ method_name
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,21 @@
1
+ module MacawFramework
2
+ class Macaw
3
+ @port: int
4
+
5
+ def delete: -> nil
6
+
7
+ def get: -> nil
8
+
9
+ def patch: -> nil
10
+
11
+ def post: -> nil
12
+
13
+ def put: -> nil
14
+
15
+ def start!: -> nil
16
+
17
+ private
18
+
19
+ def extract_client_info: -> nil
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ module MacawFramework
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: macaw_framework
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aria Diniz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-12-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A project started for study purpose that I intend to keep working on.
14
+ email:
15
+ - aria.diniz.dev@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rubocop.yml"
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/macaw_framework.rb
28
+ - lib/macaw_framework/endpoint_not_mapped_error.rb
29
+ - lib/macaw_framework/http_status_code.rb
30
+ - lib/macaw_framework/version.rb
31
+ - sig/macaw_framework.rbs
32
+ - sig/macaw_framework/macaw.rbs
33
+ homepage: https://github.com/ariasdiniz/macaw_framework
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ homepage_uri: https://github.com/ariasdiniz/macaw_framework
38
+ source_code_uri: https://github.com/ariasdiniz/macaw_framework
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.6.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.3.26
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: A web framework still in development.
58
+ test_files: []