modern 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +8 -0
  3. data/.gitignore +10 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +173 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +10 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +36 -0
  10. data/Rakefile +10 -0
  11. data/TODOS.md +4 -0
  12. data/bin/console +9 -0
  13. data/bin/setup +8 -0
  14. data/example/Gemfile +9 -0
  15. data/example/Gemfile.lock +102 -0
  16. data/example/config.ru +11 -0
  17. data/example/example.rb +19 -0
  18. data/lib/modern/app/error_handling.rb +45 -0
  19. data/lib/modern/app/request_handling/input_handling.rb +65 -0
  20. data/lib/modern/app/request_handling/output_handling.rb +54 -0
  21. data/lib/modern/app/request_handling/request_container.rb +55 -0
  22. data/lib/modern/app/request_handling.rb +70 -0
  23. data/lib/modern/app/router.rb +27 -0
  24. data/lib/modern/app/trie_router.rb +37 -0
  25. data/lib/modern/app.rb +82 -0
  26. data/lib/modern/capsule.rb +17 -0
  27. data/lib/modern/configuration.rb +16 -0
  28. data/lib/modern/core_ext/array.rb +23 -0
  29. data/lib/modern/core_ext/hash.rb +17 -0
  30. data/lib/modern/descriptor/content.rb +14 -0
  31. data/lib/modern/descriptor/converters/input/base.rb +29 -0
  32. data/lib/modern/descriptor/converters/input/json.rb +21 -0
  33. data/lib/modern/descriptor/converters/output/base.rb +25 -0
  34. data/lib/modern/descriptor/converters/output/json.rb +48 -0
  35. data/lib/modern/descriptor/converters/output/yaml.rb +21 -0
  36. data/lib/modern/descriptor/converters.rb +4 -0
  37. data/lib/modern/descriptor/core.rb +63 -0
  38. data/lib/modern/descriptor/info.rb +27 -0
  39. data/lib/modern/descriptor/parameters.rb +149 -0
  40. data/lib/modern/descriptor/request_body.rb +13 -0
  41. data/lib/modern/descriptor/response.rb +26 -0
  42. data/lib/modern/descriptor/route.rb +93 -0
  43. data/lib/modern/descriptor/security.rb +104 -0
  44. data/lib/modern/descriptor/server.rb +12 -0
  45. data/lib/modern/descriptor.rb +15 -0
  46. data/lib/modern/doc_generator/open_api3/operations.rb +114 -0
  47. data/lib/modern/doc_generator/open_api3/paths.rb +24 -0
  48. data/lib/modern/doc_generator/open_api3/schema_default_types.rb +50 -0
  49. data/lib/modern/doc_generator/open_api3/schemas.rb +171 -0
  50. data/lib/modern/doc_generator/open_api3/security_schemes.rb +15 -0
  51. data/lib/modern/doc_generator/open_api3.rb +141 -0
  52. data/lib/modern/dsl/info.rb +39 -0
  53. data/lib/modern/dsl/response_builder.rb +41 -0
  54. data/lib/modern/dsl/root.rb +38 -0
  55. data/lib/modern/dsl/route_builder.rb +130 -0
  56. data/lib/modern/dsl/scope.rb +144 -0
  57. data/lib/modern/dsl/scope_settings.rb +39 -0
  58. data/lib/modern/dsl.rb +14 -0
  59. data/lib/modern/errors/error.rb +7 -0
  60. data/lib/modern/errors/setup_errors.rb +11 -0
  61. data/lib/modern/errors/web_errors.rb +83 -0
  62. data/lib/modern/errors.rb +3 -0
  63. data/lib/modern/redirect.rb +30 -0
  64. data/lib/modern/request.rb +34 -0
  65. data/lib/modern/response.rb +39 -0
  66. data/lib/modern/services.rb +17 -0
  67. data/lib/modern/struct.rb +25 -0
  68. data/lib/modern/types.rb +41 -0
  69. data/lib/modern/util/header_parsing.rb +27 -0
  70. data/lib/modern/util/trie_node.rb +53 -0
  71. data/lib/modern/version.rb +6 -0
  72. data/lib/modern.rb +8 -0
  73. data/manual/01-why_modern.md +115 -0
  74. data/modern.gemspec +54 -0
  75. metadata +439 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2758b166b5113cad1819574884d04dafe166f70033e87cfcf54fc0615a33b7f6
4
+ data.tar.gz: df1d24226b92fbfb1ec37a06e802390857a5a86d0cfc2ab56ac20dc669d49dc6
5
+ SHA512:
6
+ metadata.gz: b3fdfd253b7f161a2f850a4a71dcd4effa39d0561b0ebbd1d2203a2fa7801c250d333fc4f9e69d7072f2dfa41d9937a112b97052f08bd030ec64a122dd4a0458
7
+ data.tar.gz: 23291ac933a23779d4d5b762020f1fc67a42e5585b05593801f760aee8b2ed6cd7f850b3a21bd6d26f8ed0080dfbdbf7db7fef0ea94ed197c169cf9497543fc9
data/.editorconfig ADDED
@@ -0,0 +1,8 @@
1
+ [*]
2
+ root = true
3
+ tab_width = 2
4
+ end_of_line = lf
5
+
6
+ [*.rb]
7
+ trim_trailing_whitespace = true
8
+ insert_final_newline = true
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --format documentation
data/.rubocop.yml ADDED
@@ -0,0 +1,173 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ Exclude:
4
+ - "*.gemspec"
5
+
6
+ Style/RegexpLiteral:
7
+ EnforcedStyle: percent_r
8
+
9
+ Metrics/BlockLength:
10
+ Enabled: false
11
+
12
+ # Relaxed.Ruby.Style
13
+ ## Version 2.1
14
+
15
+ Style/Alias:
16
+ Enabled: false
17
+ StyleGuide: http://relaxed.ruby.style/#stylealias
18
+
19
+ Style/AsciiComments:
20
+ Enabled: false
21
+ StyleGuide: http://relaxed.ruby.style/#styleasciicomments
22
+
23
+ Style/BeginBlock:
24
+ Enabled: false
25
+ StyleGuide: http://relaxed.ruby.style/#stylebeginblock
26
+
27
+ Style/BlockDelimiters:
28
+ Enabled: false
29
+ StyleGuide: http://relaxed.ruby.style/#styleblockdelimiters
30
+
31
+ Style/CommentAnnotation:
32
+ Enabled: false
33
+ StyleGuide: http://relaxed.ruby.style/#stylecommentannotation
34
+
35
+ Style/Documentation:
36
+ Enabled: false
37
+ StyleGuide: http://relaxed.ruby.style/#styledocumentation
38
+
39
+ Layout/DotPosition:
40
+ Enabled: false
41
+ StyleGuide: http://relaxed.ruby.style/#layoutdotposition
42
+
43
+ Style/DoubleNegation:
44
+ Enabled: false
45
+ StyleGuide: http://relaxed.ruby.style/#styledoublenegation
46
+
47
+ Style/EndBlock:
48
+ Enabled: false
49
+ StyleGuide: http://relaxed.ruby.style/#styleendblock
50
+
51
+ Style/FormatString:
52
+ Enabled: false
53
+ StyleGuide: http://relaxed.ruby.style/#styleformatstring
54
+
55
+ Style/IfUnlessModifier:
56
+ Enabled: false
57
+ StyleGuide: http://relaxed.ruby.style/#styleifunlessmodifier
58
+
59
+ Style/Lambda:
60
+ Enabled: false
61
+ StyleGuide: http://relaxed.ruby.style/#stylelambda
62
+
63
+ Style/ModuleFunction:
64
+ Enabled: false
65
+ StyleGuide: http://relaxed.ruby.style/#stylemodulefunction
66
+
67
+ Style/MultilineBlockChain:
68
+ Enabled: false
69
+ StyleGuide: http://relaxed.ruby.style/#stylemultilineblockchain
70
+
71
+ Style/NegatedIf:
72
+ Enabled: false
73
+ StyleGuide: http://relaxed.ruby.style/#stylenegatedif
74
+
75
+ Style/NegatedWhile:
76
+ Enabled: false
77
+ StyleGuide: http://relaxed.ruby.style/#stylenegatedwhile
78
+
79
+ Style/ParallelAssignment:
80
+ Enabled: false
81
+ StyleGuide: http://relaxed.ruby.style/#styleparallelassignment
82
+
83
+ Style/PercentLiteralDelimiters:
84
+ Enabled: false
85
+ StyleGuide: http://relaxed.ruby.style/#stylepercentliteraldelimiters
86
+
87
+ Style/PerlBackrefs:
88
+ Enabled: false
89
+ StyleGuide: http://relaxed.ruby.style/#styleperlbackrefs
90
+
91
+ Style/Semicolon:
92
+ Enabled: false
93
+ StyleGuide: http://relaxed.ruby.style/#stylesemicolon
94
+
95
+ Style/SignalException:
96
+ Enabled: false
97
+ StyleGuide: http://relaxed.ruby.style/#stylesignalexception
98
+
99
+ Style/SingleLineBlockParams:
100
+ Enabled: false
101
+ StyleGuide: http://relaxed.ruby.style/#stylesinglelineblockparams
102
+
103
+ Style/SingleLineMethods:
104
+ Enabled: false
105
+ StyleGuide: http://relaxed.ruby.style/#stylesinglelinemethods
106
+
107
+ Layout/SpaceBeforeBlockBraces:
108
+ Enabled: false
109
+ StyleGuide: http://relaxed.ruby.style/#layoutspacebeforeblockbraces
110
+
111
+ Layout/SpaceInsideParens:
112
+ Enabled: false
113
+ StyleGuide: http://relaxed.ruby.style/#layoutspaceinsideparens
114
+
115
+ Style/SpecialGlobalVars:
116
+ Enabled: false
117
+ StyleGuide: http://relaxed.ruby.style/#stylespecialglobalvars
118
+
119
+ Style/StringLiterals:
120
+ Enabled: false
121
+ StyleGuide: http://relaxed.ruby.style/#stylestringliterals
122
+
123
+ Style/TrailingCommaInArguments:
124
+ Enabled: false
125
+ StyleGuide: http://relaxed.ruby.style/#styletrailingcommainarguments
126
+
127
+ Style/TrailingCommaInLiteral:
128
+ Enabled: false
129
+ StyleGuide: http://relaxed.ruby.style/#styletrailingcommainliteral
130
+
131
+ Style/WhileUntilModifier:
132
+ Enabled: false
133
+ StyleGuide: http://relaxed.ruby.style/#stylewhileuntilmodifier
134
+
135
+ Style/WordArray:
136
+ Enabled: false
137
+ StyleGuide: http://relaxed.ruby.style/#stylewordarray
138
+
139
+ Lint/AmbiguousRegexpLiteral:
140
+ Enabled: false
141
+ StyleGuide: http://relaxed.ruby.style/#lintambiguousregexpliteral
142
+
143
+ Lint/AssignmentInCondition:
144
+ Enabled: false
145
+ StyleGuide: http://relaxed.ruby.style/#lintassignmentincondition
146
+
147
+ Metrics/AbcSize:
148
+ Enabled: false
149
+
150
+ Metrics/BlockNesting:
151
+ Enabled: false
152
+
153
+ Metrics/ClassLength:
154
+ Enabled: false
155
+
156
+ Metrics/ModuleLength:
157
+ Enabled: false
158
+
159
+ Metrics/CyclomaticComplexity:
160
+ Enabled: false
161
+
162
+ Metrics/LineLength:
163
+ Enabled: false
164
+
165
+ Metrics/MethodLength:
166
+ Enabled: false
167
+
168
+ Metrics/ParameterLists:
169
+ Enabled: false
170
+
171
+ Metrics/PerceivedComplexity:
172
+ Enabled: false
173
+
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at ed@edropple.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in modern.gemspec
6
+ gemspec
7
+
8
+ group :development do
9
+ gem "openapi3_parser", git: "https://github.com/kevindew/openapi3_parser"
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Ed Ropple
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,36 @@
1
+ # Modern #
2
+
3
+ ## Using Modern ##
4
+ A detailed description is on the way. For now, please feel free to peruse our
5
+ [test cases] and the beginnings of our [manual] to get an idea of Modern and how
6
+ it works.
7
+
8
+ ## Versioning ##
9
+ This project follows the [Semantic Versioning], version 2.0.0, with one
10
+ addendum: until the project reaches the 1.0.0 mark, the minor version number
11
+ (i.e., 0.y.*) denotes an API-breaking change.
12
+
13
+ ## Contributing ##
14
+ Bug reports and pull requests are welcome [on GitHub]. This project is intended
15
+ to be a safe, welcoming space for collaboration, and contributors are expected
16
+ to adhere to the [Contributor Covenant] code of conduct.
17
+
18
+ The best way to start contributing is to look for `TODO` comments in the code;
19
+ they're sprinkled liberally wherever it didn't quite make sense to do something
20
+ yet but some ideas might be lurking that describe how it could be handled. If
21
+ not? File an issue and let's chat about it!
22
+
23
+ ## License ##
24
+ The gem is available as open source under the terms of the [MIT License].
25
+
26
+ ## Code of Conduct ##
27
+ Everyone interacting in the Modern project’s codebases, issue trackers, chat
28
+ rooms and mailing lists is expected to follow the [code of conduct].
29
+
30
+ [test cases]: https://github.com/eropple/modern/tree/master/spec/modern
31
+ [manual]: https://github.com/eropple/modern/tree/master/manual
32
+ [Semantic Versioning]: https://semver.org/
33
+ [on GitHub]: https://github.com/eropple/modern
34
+ [Contributor Covenant]: http://contributor-covenant.org
35
+ [MIT License]: https://opensource.org/licenses/MIT
36
+ [code of conduct]: https://github.com/eropple/modern/blob/master/CODE_OF_CONDUCT.md
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: :spec
5
+
6
+ task spec: [] do
7
+ Dir.chdir __dir__ do
8
+ sh "rspec --require spec_helper spec/**/*.rb"
9
+ end
10
+ end
data/TODOS.md ADDED
@@ -0,0 +1,4 @@
1
+ TODO: investigate the `oj` gem as a JSON replacement
2
+ Supposedly, if you use Ruby 2.4 and up, `oj` is a drop-in replacement for
3
+ the stock JSON gem. We already have it pulled in from a dependency, so
4
+ it's free.
data/bin/console ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "modern"
6
+ require "modern/app"
7
+
8
+ require "pry"
9
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/example/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "puma"
6
+
7
+ gem "rack-ougai"
8
+
9
+ gem "modern", path: File.expand_path("#{__dir__}}/..")
@@ -0,0 +1,102 @@
1
+ PATH
2
+ remote: /home/ed/Development/oapi3/modern
3
+ specs:
4
+ modern (0.2.0)
5
+ content-type (~> 0.0.1)
6
+ deep_dup (~> 0.0.3)
7
+ diff-lcs (~> 1.3)
8
+ docile (~> 1.3)
9
+ dry-struct (~> 0.4)
10
+ dry-validation (~> 0.11)
11
+ http (~> 3.0)
12
+ ice_nine (~> 0.11.2)
13
+ mime-types (~> 3.1)
14
+ mime-types-data (~> 3.2016)
15
+ ougai (~> 1.5)
16
+ rack (~> 2.0)
17
+ snake_camel (~> 1.1)
18
+
19
+ GEM
20
+ remote: https://rubygems.org/
21
+ specs:
22
+ addressable (2.5.2)
23
+ public_suffix (>= 2.0.2, < 4.0)
24
+ concurrent-ruby (1.0.5)
25
+ content-type (0.0.1)
26
+ parslet (~> 1.5)
27
+ deep_dup (0.0.3)
28
+ diff-lcs (1.3)
29
+ docile (1.3.0)
30
+ domain_name (0.5.20170404)
31
+ unf (>= 0.0.5, < 1.0.0)
32
+ dry-configurable (0.7.0)
33
+ concurrent-ruby (~> 1.0)
34
+ dry-container (0.6.0)
35
+ concurrent-ruby (~> 1.0)
36
+ dry-configurable (~> 0.1, >= 0.1.3)
37
+ dry-core (0.4.4)
38
+ concurrent-ruby (~> 1.0)
39
+ dry-equalizer (0.2.0)
40
+ dry-logic (0.4.2)
41
+ dry-container (~> 0.2, >= 0.2.6)
42
+ dry-core (~> 0.2)
43
+ dry-equalizer (~> 0.2)
44
+ dry-struct (0.4.0)
45
+ dry-core (~> 0.4, >= 0.4.1)
46
+ dry-equalizer (~> 0.2)
47
+ dry-types (~> 0.12, >= 0.12.2)
48
+ ice_nine (~> 0.11)
49
+ dry-types (0.12.2)
50
+ concurrent-ruby (~> 1.0)
51
+ dry-configurable (~> 0.1)
52
+ dry-container (~> 0.3)
53
+ dry-core (~> 0.2, >= 0.2.1)
54
+ dry-equalizer (~> 0.2)
55
+ dry-logic (~> 0.4, >= 0.4.2)
56
+ inflecto (~> 0.0.0, >= 0.0.2)
57
+ dry-validation (0.11.1)
58
+ concurrent-ruby (~> 1.0)
59
+ dry-configurable (~> 0.1, >= 0.1.3)
60
+ dry-core (~> 0.2, >= 0.2.1)
61
+ dry-equalizer (~> 0.2)
62
+ dry-logic (~> 0.4, >= 0.4.0)
63
+ dry-types (~> 0.12.0)
64
+ http (3.0.0)
65
+ addressable (~> 2.3)
66
+ http-cookie (~> 1.0)
67
+ http-form_data (>= 2.0.0.pre.pre2, < 3)
68
+ http_parser.rb (~> 0.6.0)
69
+ http-cookie (1.0.3)
70
+ domain_name (~> 0.5)
71
+ http-form_data (2.0.0)
72
+ http_parser.rb (0.6.0)
73
+ ice_nine (0.11.2)
74
+ inflecto (0.0.2)
75
+ mime-types (3.1)
76
+ mime-types-data (~> 3.2015)
77
+ mime-types-data (3.2016.0521)
78
+ oj (3.4.0)
79
+ ougai (1.6.1)
80
+ oj (~> 3.4)
81
+ parslet (1.8.1)
82
+ public_suffix (3.0.2)
83
+ puma (3.11.2)
84
+ rack (2.0.4)
85
+ rack-ougai (0.2.1)
86
+ ougai (~> 1.5)
87
+ rack (~> 2.0)
88
+ snake_camel (1.1.0)
89
+ unf (0.1.4)
90
+ unf_ext
91
+ unf_ext (0.0.7.5)
92
+
93
+ PLATFORMS
94
+ ruby
95
+
96
+ DEPENDENCIES
97
+ modern!
98
+ puma
99
+ rack-ougai
100
+
101
+ BUNDLED WITH
102
+ 1.16.1
data/example/config.ru ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rack/ougai'
4
+
5
+ require 'modern/app'
6
+ require_relative './example'
7
+
8
+ use Rack::Ougai::Logger, Logger::INFO
9
+ use Rack::Ougai::LogRequests
10
+
11
+ run Modern::App.new(Example.descriptor)
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'modern/descriptor'
4
+
5
+ module Example
6
+ # TODO: implement routes into the example descriptor
7
+ # Modern was built specifically for a project I'm working on,
8
+ # so an end-to-end example is still TBD(eveloped). However,
9
+ # you can check out the tests for examples of how to write
10
+ # routes and security schemes; all internal objects are built
11
+ # with `dry-struct`
12
+ # TODO: implement with modern-dsl when that's done (or maybe have a
13
+ # separate example for that?)
14
+ def self.descriptor
15
+ desc = Modern::Descriptor.new
16
+
17
+ desc
18
+ end
19
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Modern
4
+ class App
5
+ module ErrorHandling
6
+ private
7
+
8
+ def catch_web_error(response, err)
9
+ ret = {
10
+ message: err.message,
11
+ request_id: response.headers["X-Request-Id"]
12
+ }
13
+
14
+ # TODO: We maybe shouldn't hard-code a default to a JSON retval from a
15
+ # 404. On the other hand...maybe we should. Knowledgeable clients
16
+ # shouldn't be parsing a 404 unless the 404 is specified in a route's
17
+ # responses, and so this could just be garbage data except during
18
+ # development/debugging.
19
+ response.status = err.status;
20
+ response.json(ret)
21
+ end
22
+
23
+ def catch_unhandled_error(response, err)
24
+ ret = {
25
+ message: @configuration.show_errors ? err.message : "An error occurred.",
26
+ request_id: response.headers["X-Request-Id"]
27
+ }
28
+
29
+ if @configuration.show_errors
30
+ ret[:backtrace] = err.backtrace
31
+ end
32
+
33
+ logger.error "Unhandled exception caught by main loop.", err
34
+
35
+ # TODO: We maybe shouldn't hard-code a default to a JSON retval from a
36
+ # 404. On the other hand...maybe we should. Knowledgeable clients
37
+ # shouldn't be parsing a 404 unless the 404 is specified in a route's
38
+ # responses, and so this could just be garbage data except during
39
+ # development/debugging.
40
+ response.status = err.respond_to?(:status) ? err.status : 500;
41
+ response.json(ret)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'modern/errors/web_errors'
4
+
5
+ module Modern
6
+ class App
7
+ module RequestHandling
8
+ module InputHandling
9
+ private
10
+
11
+ def parse_parameters(request, route)
12
+ match = route.path_matcher.match(request.path_info)
13
+ if match.nil?
14
+ {}
15
+ else
16
+ route_captures = match.names.map{ |n| [n.to_s, match[n]] }.to_h
17
+
18
+ route.parameters.map { |p| [p.name.to_sym, p.retrieve(request, route_captures)] }.to_h
19
+ end
20
+ end
21
+
22
+ def parse_request_body(request, route)
23
+ input_converter = determine_input_converter(request, route)
24
+ raise Modern::Errors::UnsupportedMediaTypeError if input_converter.nil?
25
+
26
+ raw = input_converter.converter.call(request.body)
27
+
28
+ t = route.request_body.type
29
+
30
+ if raw.nil?
31
+ nil
32
+ elsif t.nil?
33
+ raw
34
+ else
35
+ begin
36
+ if raw.is_a?(Hash)
37
+ raw = raw.map { |k, v| [k.respond_to?(:to_sym) ? k.to_sym : k, v] }.to_h
38
+ end
39
+
40
+ t[raw]
41
+ rescue Dry::Types::ConstraintError,
42
+ Dry::Types::MissingKeyError,
43
+ Dry::Struct::Error => err
44
+ request.logger.info("Struct parse error for route '#{route.id}'", err) \
45
+ if @configuration.log_input_converter_errors
46
+ raise Modern::Errors::UnprocessableEntity, err.message
47
+ rescue StandardError => err
48
+ request.logger.warn("Unprocessable body for route '#{route.id}'", err) \
49
+ if @configuration.log_input_converter_errors
50
+ raise Modern::Errors::UnprocessableEntity
51
+ end
52
+ end
53
+ end
54
+
55
+ def determine_input_converter(request, route)
56
+ # RFC 2616; you MAY sniff content but SHOULD return application/octet-stream
57
+ # if the input Content-Type remains unknown. However, it seems like Rack may
58
+ # default to application/x-www-form-urlencoded; Rack::Test definitely does.
59
+ content_type = (request.content_type || "application/octet-stream").downcase.strip
60
+ route.input_converters_by_type[content_type]
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end