sofia 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.
Files changed (99) hide show
  1. checksums.yaml +7 -0
  2. data/.byebug_history +6 -0
  3. data/.rubocop.yml +179 -0
  4. data/.ruby-version +1 -0
  5. data/CHANGELOG.md +5 -0
  6. data/CODE_OF_CONDUCT.md +132 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +21 -0
  9. data/Rakefile +16 -0
  10. data/lib/sofia/adapter/net_http.rb +67 -0
  11. data/lib/sofia/adapter.rb +20 -0
  12. data/lib/sofia/client.rb +55 -0
  13. data/lib/sofia/error/argument_error.rb +8 -0
  14. data/lib/sofia/error/connection_failed.rb +8 -0
  15. data/lib/sofia/error/invalid_json.rb +8 -0
  16. data/lib/sofia/error/ssl_error.rb +8 -0
  17. data/lib/sofia/error/timeout_error.rb +8 -0
  18. data/lib/sofia/error.rb +14 -0
  19. data/lib/sofia/helper.rb +9 -0
  20. data/lib/sofia/request.rb +56 -0
  21. data/lib/sofia/response.rb +57 -0
  22. data/lib/sofia/sorbet_types.rb +19 -0
  23. data/lib/sofia/types/adapter.rb +39 -0
  24. data/lib/sofia/types/base_url.rb +33 -0
  25. data/lib/sofia/types/body.rb +60 -0
  26. data/lib/sofia/types/headers.rb +56 -0
  27. data/lib/sofia/types/params.rb +56 -0
  28. data/lib/sofia/types/path.rb +28 -0
  29. data/lib/sofia/types.rb +13 -0
  30. data/lib/sofia/version.rb +5 -0
  31. data/lib/sofia.rb +25 -0
  32. data/sig/sofia.rbs +4 -0
  33. data/sorbet/config +6 -0
  34. data/sorbet/rbi/annotations/.gitattributes +1 -0
  35. data/sorbet/rbi/annotations/activesupport.rbi +495 -0
  36. data/sorbet/rbi/annotations/minitest.rbi +120 -0
  37. data/sorbet/rbi/annotations/mocha.rbi +34 -0
  38. data/sorbet/rbi/annotations/rainbow.rbi +269 -0
  39. data/sorbet/rbi/dsl/.gitattributes +1 -0
  40. data/sorbet/rbi/dsl/active_support/callbacks.rbi +21 -0
  41. data/sorbet/rbi/gems/.gitattributes +1 -0
  42. data/sorbet/rbi/gems/activesupport@8.1.2.rbi +20360 -0
  43. data/sorbet/rbi/gems/ast@2.4.3.rbi +585 -0
  44. data/sorbet/rbi/gems/base64@0.3.0.rbi +545 -0
  45. data/sorbet/rbi/gems/benchmark@0.4.1.rbi +619 -0
  46. data/sorbet/rbi/gems/bigdecimal@4.0.1.rbi +239 -0
  47. data/sorbet/rbi/gems/concurrent-ruby@1.3.6.rbi +11674 -0
  48. data/sorbet/rbi/gems/connection_pool@3.0.2.rbi +9 -0
  49. data/sorbet/rbi/gems/date@3.4.1.rbi +75 -0
  50. data/sorbet/rbi/gems/drb@2.2.3.rbi +1661 -0
  51. data/sorbet/rbi/gems/erb@5.0.2.rbi +878 -0
  52. data/sorbet/rbi/gems/erubi@1.13.1.rbi +155 -0
  53. data/sorbet/rbi/gems/factory_bot@6.5.6.rbi +2748 -0
  54. data/sorbet/rbi/gems/i18n@1.14.8.rbi +2359 -0
  55. data/sorbet/rbi/gems/io-console@0.8.1.rbi +9 -0
  56. data/sorbet/rbi/gems/json@2.12.2.rbi +2051 -0
  57. data/sorbet/rbi/gems/language_server-protocol@3.17.0.5.rbi +9 -0
  58. data/sorbet/rbi/gems/lint_roller@1.1.0.rbi +86 -0
  59. data/sorbet/rbi/gems/logger@1.7.0.rbi +963 -0
  60. data/sorbet/rbi/gems/minitest@5.25.5.rbi +1547 -0
  61. data/sorbet/rbi/gems/mocha@3.0.1.rbi +28 -0
  62. data/sorbet/rbi/gems/netrc@0.11.0.rbi +159 -0
  63. data/sorbet/rbi/gems/openssl@4.0.0.rbi +1908 -0
  64. data/sorbet/rbi/gems/parallel@1.27.0.rbi +291 -0
  65. data/sorbet/rbi/gems/parser@3.3.8.0.rbi +5535 -0
  66. data/sorbet/rbi/gems/pp@0.6.2.rbi +368 -0
  67. data/sorbet/rbi/gems/prettyprint@0.2.0.rbi +477 -0
  68. data/sorbet/rbi/gems/prism@1.5.1.rbi +42049 -0
  69. data/sorbet/rbi/gems/psych@5.2.6.rbi +2469 -0
  70. data/sorbet/rbi/gems/racc@1.8.1.rbi +160 -0
  71. data/sorbet/rbi/gems/rainbow@3.1.1.rbi +403 -0
  72. data/sorbet/rbi/gems/rake@13.2.1.rbi +3033 -0
  73. data/sorbet/rbi/gems/rbi@0.3.3.rbi +6742 -0
  74. data/sorbet/rbi/gems/rbs@3.9.5.rbi +6978 -0
  75. data/sorbet/rbi/gems/rdoc@6.14.2.rbi +12688 -0
  76. data/sorbet/rbi/gems/regexp_parser@2.10.0.rbi +3795 -0
  77. data/sorbet/rbi/gems/reline@0.6.2.rbi +2441 -0
  78. data/sorbet/rbi/gems/rexml@3.4.1.rbi +5240 -0
  79. data/sorbet/rbi/gems/rubocop-ast@1.45.1.rbi +7721 -0
  80. data/sorbet/rbi/gems/rubocop@1.76.1.rbi +62517 -0
  81. data/sorbet/rbi/gems/ruby-progressbar@1.13.0.rbi +1318 -0
  82. data/sorbet/rbi/gems/ruby2_keywords@0.0.5.rbi +9 -0
  83. data/sorbet/rbi/gems/securerandom@0.4.1.rbi +75 -0
  84. data/sorbet/rbi/gems/spoom@1.6.3.rbi +6985 -0
  85. data/sorbet/rbi/gems/stringio@3.1.7.rbi +9 -0
  86. data/sorbet/rbi/gems/tapioca@0.16.11.rbi +3628 -0
  87. data/sorbet/rbi/gems/thor@1.4.0.rbi +4399 -0
  88. data/sorbet/rbi/gems/tzinfo@2.0.6.rbi +5918 -0
  89. data/sorbet/rbi/gems/unicode-display_width@3.1.4.rbi +132 -0
  90. data/sorbet/rbi/gems/unicode-emoji@4.0.4.rbi +251 -0
  91. data/sorbet/rbi/gems/uri@1.1.1.rbi +2405 -0
  92. data/sorbet/rbi/gems/yard-sorbet@0.9.0.rbi +435 -0
  93. data/sorbet/rbi/gems/yard@0.9.37.rbi +18379 -0
  94. data/sorbet/rbi/shims/byebug.rbi +6 -0
  95. data/sorbet/rbi/shims/client.rbi +22 -0
  96. data/sorbet/rbi/todo.rbi +7 -0
  97. data/sorbet/tapioca/config.yml +13 -0
  98. data/sorbet/tapioca/require.rb +4 -0
  99. metadata +137 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7a0700857a43b841018595dea20d578f1f77a59f3c5dc625836ac44faca893e7
4
+ data.tar.gz: 5641b7640ee6e1225b920ad683c3f3172a394b755b23740d5600659d15128371
5
+ SHA512:
6
+ metadata.gz: 2ce0f5a2fc2cd5a0b06107465723399f650c1ec4cfb4a311b5a8196a4f2ca10eeffd73362e4c6bd0ac1c0a97fd29dbef173f765488e620ee4ceb33c5343e063f
7
+ data.tar.gz: 8f062bd2075032d046892799ae82ad336dccbee9dab7227c5b0150469fd69e67edf24b7243e764cff3127e69818f79ed0281c573589fe9e81bc93df40ba749b6
data/.byebug_history ADDED
@@ -0,0 +1,6 @@
1
+ c
2
+ response.status
3
+ response.success?
4
+ response.headers
5
+ response.headears
6
+ response
data/.rubocop.yml ADDED
@@ -0,0 +1,179 @@
1
+ inherit_mode:
2
+ merge:
3
+ - Exclude
4
+ - Include
5
+
6
+ AllCops:
7
+ EnabledByDefault: true
8
+ SuggestExtensions: false
9
+ Exclude:
10
+ - bin/*
11
+ - db/schema.rb
12
+ - vendor/bundle/**/*
13
+ - tmp/**/*
14
+ - Gemfile.lock
15
+ - "**/*.rbi"
16
+ - sorbet/**/*
17
+
18
+ Layout/EmptyLines:
19
+ Enabled: false
20
+ Layout/EmptyLinesAroundBlockBody:
21
+ Enabled: false
22
+ Layout/EmptyLinesAroundClassBody:
23
+ Enabled: false
24
+ Layout/EmptyLinesAroundModuleBody:
25
+ Enabled: false
26
+ Layout/EmptyLinesAroundExceptionHandlingKeywords:
27
+ Enabled: false
28
+ Layout/LeadingCommentSpace:
29
+ Enabled: false
30
+
31
+ Layout/LineLength:
32
+ Enabled: false
33
+ Layout/ClassStructure:
34
+ Enabled: false
35
+ Layout/ExtraSpacing:
36
+ Enabled: false
37
+ Layout/FirstMethodArgumentLineBreak:
38
+ Enabled: false
39
+ Layout/RedundantLineBreak:
40
+ Enabled: false
41
+ Layout/MultilineAssignmentLayout:
42
+ Enabled: false
43
+
44
+ Lint/ConstantResolution:
45
+ Enabled: false
46
+
47
+ Metrics/AbcSize:
48
+ Enabled: false
49
+ Metrics/BlockLength:
50
+ Enabled: false
51
+ Metrics/ClassLength:
52
+ Max: 200
53
+ Exclude:
54
+ - db/migrate/*
55
+ - test/**/*
56
+ - config/**/*
57
+ Metrics/ModuleLength:
58
+ Max: 400
59
+ Exclude:
60
+ - db/migrate/*
61
+ - test/**/*
62
+ - config/**/*
63
+ Metrics/CyclomaticComplexity:
64
+ Max: 12
65
+ Metrics/MethodLength:
66
+ Max: 40
67
+ Metrics/PerceivedComplexity:
68
+ Max: 12
69
+
70
+ Style/Send:
71
+ Enabled: false
72
+ Style/Documentation:
73
+ Enabled: false
74
+ Style/ArrayCoercion:
75
+ Enabled: false
76
+ Style/Alias:
77
+ Enabled: false
78
+ Style/ClassAndModuleChildren:
79
+ Enabled: false
80
+ Style/ClassMethodsDefinitions:
81
+ EnforcedStyle: self_class
82
+ Style/ConstantVisibility:
83
+ Enabled: false
84
+ Style/Copyright:
85
+ Enabled: false
86
+ Style/DisableCopsWithinSourceCodeDirective:
87
+ Enabled: false
88
+ Style/InlineComment:
89
+ Enabled: false
90
+ Style/IpAddresses:
91
+ Exclude:
92
+ - test/**/*
93
+ Style/MethodCallWithArgsParentheses:
94
+ Enabled: false
95
+ Style/ModuleFunction:
96
+ EnforcedStyle: extend_self
97
+ Style/RedundantArgument:
98
+ Enabled: false
99
+ Style/StringHashKeys:
100
+ Enabled: false
101
+ Style/RedundantConstantBase:
102
+ Enabled: false
103
+ Metrics/ParameterLists:
104
+ Enabled: false
105
+ Style/MissingElse:
106
+ Enabled: false
107
+ Style/RequireOrder:
108
+ Enabled: false
109
+ Lint/NumberConversion:
110
+ Enabled: false
111
+ Style/Lambda:
112
+ EnforcedStyle: literal
113
+ Style/AsciiComments:
114
+ Enabled: false
115
+ Style/TrailingCommaInHashLiteral:
116
+ EnforcedStyleForMultiline: consistent_comma
117
+ Style/TrailingCommaInArrayLiteral:
118
+ EnforcedStyleForMultiline: consistent_comma
119
+ Style/TrailingCommaInArguments:
120
+ EnforcedStyleForMultiline: consistent_comma
121
+ Lint/AmbiguousOperatorPrecedence:
122
+ Enabled: false
123
+ Style/ClassVars:
124
+ Enabled: false
125
+ Layout/SpaceAroundMethodCallOperator:
126
+ Enabled: false
127
+ Style/LambdaCall:
128
+ Enabled: false
129
+ Style/DateTime:
130
+ Enabled: false
131
+ Style/EndlessMethod:
132
+ Enabled: false
133
+ Lint/MissingSuper:
134
+ Enabled: false
135
+ Style/ArgumentsForwarding:
136
+ Enabled: false
137
+ Style/RedundantBegin:
138
+ Enabled: false
139
+ Style/Semicolon:
140
+ AllowAsExpressionSeparator: true
141
+ Layout/EmptyLinesAroundAttributeAccessor:
142
+ Enabled: false
143
+ Style/IfUnlessModifier:
144
+ Enabled: false
145
+ Layout/EmptyLinesAroundMethodBody:
146
+ Enabled: false
147
+ Naming/AccessorMethodName:
148
+ Enabled: false
149
+ Style/CommentedKeyword:
150
+ Enabled: false
151
+ Bundler/DuplicatedGem:
152
+ Enabled: false
153
+ Style/DocumentationMethod:
154
+ Enabled: false
155
+ Style/NumericLiterals:
156
+ Enabled: false
157
+ Layout/MultilineMethodArgumentLineBreaks:
158
+ Enabled: false
159
+ Layout/FirstArrayElementLineBreak:
160
+ Enabled: false
161
+ Naming/MethodParameterName:
162
+ Enabled: false
163
+ Layout/HashAlignment:
164
+ EnforcedHashRocketStyle: table
165
+ EnforcedColonStyle: table
166
+ Style/NumericPredicate:
167
+ Enabled: false
168
+ Lint/EmptyWhen:
169
+ Enabled: false
170
+ Style/ArrayFirstLast:
171
+ Enabled: false
172
+ Bundler/GemComment:
173
+ Enabled: false
174
+ Bundler/GemVersion:
175
+ Enabled: false
176
+ Style/MethodCalledOnDoEndBlock:
177
+ Enabled: false
178
+ Lint/SuppressedException:
179
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.4.5
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-09-25
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 KubaJadrzak
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,21 @@
1
+ # Sofia
2
+
3
+ This is a personal project created for self-learning purposes. The goal is to create a simplified version of Faraday gem. For now, Sofia will support only NetHTTP as adapter, but I have plans to write my own adapter in the future. Initially wanted to name this project alice but there is already an alice gem :(
4
+
5
+ ## Development
6
+
7
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
8
+
9
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
10
+
11
+ ## Contributing
12
+
13
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sofia. 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/[USERNAME]/sofia/blob/master/CODE_OF_CONDUCT.md).
14
+
15
+ ## License
16
+
17
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
18
+
19
+ ## Code of Conduct
20
+
21
+ Everyone interacting in the Sofia project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/sofia/blob/master/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 << 'lib'
8
+ t.libs << 'test'
9
+ t.pattern = ENV['TEST'] || 'test/**/*_test.rb'
10
+ end
11
+
12
+ require 'rubocop/rake_task'
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test]
@@ -0,0 +1,67 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Sofia
5
+ module Adapter
6
+ class NetHTTP < Base
7
+ class << self
8
+
9
+ NET_HTTP_EXCEPTIONS = [
10
+ IOError, Errno::ECONNREFUSED, Errno::ECONNRESET, SocketError,
11
+ ].freeze
12
+
13
+ TIMEOUT_EXCEPTIONS = [
14
+ Net::OpenTimeout, Net::ReadTimeout, Errno::ETIMEDOUT,
15
+ ].freeze
16
+
17
+ # @override
18
+ #: (Sofia::Request request) -> Sofia::Response
19
+ def call(request)
20
+ uri = URI.parse(request.url)
21
+
22
+ http = Net::HTTP.new(uri.host, uri.port)
23
+ http.use_ssl = uri.scheme == 'https'
24
+
25
+ klass = Net::HTTP.const_get(request.http_method.capitalize)
26
+
27
+ raise Sofia::Error::ArgumentError, 'only HTTP(S) URLs are supported' unless uri.is_a?(URI::HTTP)
28
+
29
+ net_req = klass.new(uri.request_uri)
30
+
31
+ request.headers.each { |k, v| net_req[k] = v }
32
+ body_hash = request.body.to_h
33
+ net_req.body = JSON.dump(body_hash) unless body_hash.empty? || request.http_method == :get
34
+
35
+
36
+ response = perform_request(http, net_req)
37
+
38
+ adapt_response(response, request)
39
+ end
40
+
41
+ private
42
+
43
+ #: (Net::HTTPResponse response, Sofia::Request request) -> Sofia::Response
44
+ def adapt_response(response, request)
45
+ Sofia::Response.new(
46
+ status: response.code.to_i,
47
+ headers: response.each_header.to_h,
48
+ raw_body: response.body,
49
+ request: request,
50
+ )
51
+ end
52
+
53
+ #: (Net::HTTP http, Net::HTTPRequest net_req) -> Net::HTTPResponse
54
+ def perform_request(http, net_req)
55
+ http.request(net_req)
56
+ rescue OpenSSL::SSL::SSLError => e
57
+ raise Sofia::Error::SSLError, e
58
+ rescue *NET_HTTP_EXCEPTIONS => e
59
+ raise Sofia::Error::ConnectionFailed, e
60
+ rescue *TIMEOUT_EXCEPTIONS => e
61
+ raise Sofia::Error::TimeoutError, e
62
+ end
63
+
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,20 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Sofia
5
+ module Adapter
6
+ # @abstract
7
+ class Base
8
+ class << self
9
+ extend T::Sig
10
+ extend T::Helpers
11
+ abstract!
12
+
13
+ sig { abstract.params(request: Sofia::Request).returns(Sofia::Response) }
14
+ def call(request); end
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ require_relative 'adapter/net_http'
@@ -0,0 +1,55 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Sofia
5
+ class Client
6
+ HTTP_METHODS = %w[get post put patch delete].freeze
7
+
8
+ #: (base_url: untyped, adapter: untyped) -> void
9
+ def initialize(base_url:, adapter:)
10
+ @base_url = Sofia::Types::BaseUrl.new(base_url) #: Sofia::Types::BaseUrl
11
+ @adapter = Sofia::Types::Adapter.new(adapter) #: Sofia::Types::Adapter
12
+ end
13
+
14
+ #: -> String
15
+ def base_url
16
+ @base_url.to_s
17
+ end
18
+
19
+ #: -> Symbol
20
+ def adapter
21
+ @adapter.to_sym
22
+ end
23
+
24
+ #: ?{ (Request req) -> untyped } -> Response
25
+ def get(&) = request(:get, &)
26
+
27
+ #: ?{ (Request req) -> untyped } -> Response
28
+ def post(&) = request(:post, &)
29
+
30
+ #: ?{ (Request req) -> untyped } -> Response
31
+ def put(&) = request(:put, &)
32
+
33
+ #: ?{ (Request req) -> untyped } -> Response
34
+ def patch(&) = request(:patch, &)
35
+
36
+ #: ?{ (Request req) -> untyped } -> Response
37
+ def delete(&) = request(:delete, &)
38
+
39
+ private
40
+
41
+ #: (Symbol http_method) ?{ ( Request req ) -> untyped } -> Response
42
+ def request(http_method, &block)
43
+ raise Sofia::Error::ArgumentError, 'configuration of the request must be provided via block' unless block
44
+
45
+ req = Request.new(
46
+ http_method: http_method,
47
+ base_url: @base_url,
48
+ )
49
+
50
+ block.call(req)
51
+
52
+ @adapter.klass.call(req)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,8 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Sofia
5
+ module Error
6
+ class ArgumentError < Base; end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Sofia
5
+ module Error
6
+ class ConnectionFailed < Base; end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Sofia
5
+ module Error
6
+ class InvalidJSON < Base; end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Sofia
5
+ module Error
6
+ class SSLError < Base; end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Sofia
5
+ module Error
6
+ class TimeoutError < Base; end
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Sofia
5
+ module Error
6
+ class Base < StandardError; end
7
+ end
8
+ end
9
+
10
+ require_relative 'error/connection_failed'
11
+ require_relative 'error/ssl_error'
12
+ require_relative 'error/timeout_error'
13
+ require_relative 'error/invalid_json'
14
+ require_relative 'error/argument_error'
@@ -0,0 +1,9 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Sofia
5
+ module Helper
6
+ end
7
+ end
8
+
9
+ require_relative 'helper/params'
@@ -0,0 +1,56 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Sofia
5
+ class Request
6
+
7
+ #: Symbol
8
+ attr_reader :http_method
9
+
10
+ #: Sofia::Types::Headers
11
+ attr_reader :headers
12
+
13
+ #: Sofia::Types::Params
14
+ attr_reader :params
15
+
16
+ #: Sofia::Types::Body
17
+ attr_reader :body
18
+
19
+ #: (http_method: Symbol, base_url: Sofia::Types::BaseUrl) -> void
20
+ def initialize(http_method:, base_url:)
21
+ @http_method = http_method
22
+ @base_url = base_url
23
+ @path = Sofia::Types::Path.new #: Sofia::Types::Path
24
+ @params = Sofia::Types::Params.new #: Sofia::Types::Params
25
+ @headers = Sofia::Types::Headers.new #: Sofia::Types::Headers
26
+ @body = Sofia::Types::Body.new #: Sofia::Types::Body
27
+ end
28
+
29
+ #: (untyped path) -> void
30
+ def path=(path)
31
+ @path = Sofia::Types::Path.new(path)
32
+ end
33
+
34
+ #: (untyped params) -> void
35
+ def params=(params)
36
+ @params = Sofia::Types::Params.new(params)
37
+ end
38
+
39
+ #: (untyped headers) -> void
40
+ def headers=(headers)
41
+ @headers = Sofia::Types::Headers.new(headers)
42
+ end
43
+
44
+ #: (untyped? body) -> void
45
+ def body=(body)
46
+ @body = Sofia::Types::Body.new(body)
47
+ end
48
+
49
+ #: -> String
50
+ def url
51
+ qs = @params.to_s
52
+ base = @base_url.to_s + @path.to_s
53
+ qs.empty? ? base : "#{base}?#{qs}"
54
+ end
55
+ end
56
+ end