active_jobject 0.1.1
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 +7 -0
- data/.solargraph.yml +22 -0
- data/LICENSE.txt +21 -0
- data/README.md +38 -0
- data/Rakefile +12 -0
- data/lib/active_jobject/base.rb +23 -0
- data/lib/active_jobject/collection.rb +6 -0
- data/lib/active_jobject/parser.rb +69 -0
- data/lib/active_jobject/request/engine/base.rb +55 -0
- data/lib/active_jobject/request/engine/net_http.rb +127 -0
- data/lib/active_jobject/request/engine/oauth2.rb +108 -0
- data/lib/active_jobject/request/error.rb +30 -0
- data/lib/active_jobject/request.rb +60 -0
- data/lib/active_jobject/version.rb +5 -0
- data/lib/active_jobject.rb +21 -0
- data/sig/active_jobject.rbs +4 -0
- metadata +60 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 25a4e56c18e49c5151fef9020b24fa6ff7e9c5f753e7bc31035cfd906fb27c41
|
|
4
|
+
data.tar.gz: f39f850277672136d0d87ef6685088f480c09218a7a27879e4d79ad1665016b6
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c64b52c489aeb0e289fa348ae08583a5ba8f523a2cfec7222f1bbf265ca28e557d76f0fe6bd0a24e711c64321f6c0366503b87c185358caf31ea58f3ac94aab4
|
|
7
|
+
data.tar.gz: a906370c3219908a1b1ca5dbd9c3b5270b05a9b9326eaf72398bfdc4b856135332202546ccb38877a3b393e0e8254ed5bfb86f5f9e6ecd71e3fff16e4d3e5f33
|
data/.solargraph.yml
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
---
|
|
2
|
+
include:
|
|
3
|
+
- "**/*.rb"
|
|
4
|
+
- "**/*.erb"
|
|
5
|
+
exclude:
|
|
6
|
+
- spec/**/*
|
|
7
|
+
- test/**/*
|
|
8
|
+
- vendor/**/*
|
|
9
|
+
- ".bundle/**/*"
|
|
10
|
+
require: []
|
|
11
|
+
domains: []
|
|
12
|
+
reporters:
|
|
13
|
+
- rubocop
|
|
14
|
+
- require_not_found
|
|
15
|
+
formatter:
|
|
16
|
+
rubocop:
|
|
17
|
+
cops: safe
|
|
18
|
+
except: []
|
|
19
|
+
only: []
|
|
20
|
+
extra_args: []
|
|
21
|
+
require_paths: []
|
|
22
|
+
max_files: 5000
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Guilherme Theodoro
|
|
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,38 @@
|
|
|
1
|
+
# ActiveJobject
|
|
2
|
+
|
|
3
|
+
Yes ActiveJobject, as in Active JSON Object (clever I know). This gem is built as a wraper for handling external connections with parsers and authentication built-in. It also supports
|
|
4
|
+
several HTTP request / gem so you can use your own `engine` or gem that better suits your needs. It also comes packaged with it's own error handling, allowing for more consistent and standard error handling cross-project.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
9
|
+
|
|
10
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
TODO: Write usage instructions here
|
|
25
|
+
|
|
26
|
+
## Development
|
|
27
|
+
|
|
28
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
29
|
+
|
|
30
|
+
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).
|
|
31
|
+
|
|
32
|
+
## Contributing
|
|
33
|
+
|
|
34
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_jobject.
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveJobject
|
|
4
|
+
class Base
|
|
5
|
+
include ActiveJobject::Request
|
|
6
|
+
|
|
7
|
+
class << self
|
|
8
|
+
attr_accessor :_site
|
|
9
|
+
|
|
10
|
+
def site
|
|
11
|
+
_site
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def site=(site)
|
|
15
|
+
self._site = site
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def uri
|
|
20
|
+
self.class._site.is_a?(URI) ? self.class._site : URI(self.class._site)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveJobject
|
|
4
|
+
module Parser
|
|
5
|
+
class InvalidInstance < ActiveJobject::Error; end
|
|
6
|
+
|
|
7
|
+
def parse(klass, **attributes)
|
|
8
|
+
raise InvalidInstance, "Class #{klass&.name || klass} is not an instance." if klass.is_a?(Class)
|
|
9
|
+
|
|
10
|
+
attributes.each do |attribute, value|
|
|
11
|
+
return_value = if value.is_a?(Array)
|
|
12
|
+
parse_array(klass, attribute, value)
|
|
13
|
+
elsif value.is_a?(Hash)
|
|
14
|
+
parse_hash(klass, attribute, value)
|
|
15
|
+
else
|
|
16
|
+
value
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
define_singleton_methods(klass, attribute, return_value)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
klass
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def parse_array(klass, attribute, value)
|
|
28
|
+
subklass = define_const(klass.class, attribute.split('_').map(&:capitalize).join.to_sym, Class.new(klass.class))
|
|
29
|
+
|
|
30
|
+
ActiveJobject::Collection.new(
|
|
31
|
+
value.map do |arr_value|
|
|
32
|
+
if arr_value.is_a?(Hash)
|
|
33
|
+
subklass_instance = subklass.new
|
|
34
|
+
parse(subklass_instance, **arr_value)
|
|
35
|
+
else
|
|
36
|
+
arr_value
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def parse_hash(klass, attribute, value)
|
|
43
|
+
subklass = define_const(klass.class, attribute.split('_').map(&:capitalize).join.to_sym, Class.new(klass.class))
|
|
44
|
+
subklass_instance = subklass.new
|
|
45
|
+
|
|
46
|
+
parse(subklass_instance, **value)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def define_singleton_methods(klass, attribute, return_value)
|
|
50
|
+
klass.instance_variable_set(:"@#{attribute}", return_value)
|
|
51
|
+
|
|
52
|
+
klass.singleton_class.send(:define_method, attribute.to_sym) do
|
|
53
|
+
klass.instance_variable_get(:"@#{attribute}")
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
klass.singleton_class.send(:define_method, "#{attribute.to_sym}=") do |new_value|
|
|
57
|
+
instance_variable_set(:"@#{attribute}", new_value)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def define_const(base_klass, name, klass)
|
|
62
|
+
if base_klass.const_defined?(name, false)
|
|
63
|
+
base_klass.const_get(name, false)
|
|
64
|
+
else
|
|
65
|
+
base_klass.const_set(name, klass)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveJobject
|
|
4
|
+
module Request
|
|
5
|
+
module Engine
|
|
6
|
+
class Base
|
|
7
|
+
class << self
|
|
8
|
+
def get
|
|
9
|
+
raise NotImplementedError
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def post
|
|
13
|
+
raise NotImplementedError
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def put
|
|
17
|
+
raise NotImplementedError
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def patch
|
|
21
|
+
raise NotImplementedError
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def delete
|
|
25
|
+
raise NotImplementedError
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def error_class(status)
|
|
29
|
+
case status
|
|
30
|
+
when 401
|
|
31
|
+
ActiveJobject::Request::Unauthorized
|
|
32
|
+
when 403
|
|
33
|
+
ActiveJobject::Request::Forbidden
|
|
34
|
+
when 404
|
|
35
|
+
ActiveJobject::Request::NotFound
|
|
36
|
+
when 500
|
|
37
|
+
ActiveJobject::Request::ServerError
|
|
38
|
+
else
|
|
39
|
+
ActiveJobject::Request::Error
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def authorization_strategy
|
|
44
|
+
@authorization_strategy ||= ->(site:) { raise NotImplementedError, 'Set `authorization_strategy` prior to running requests.' } # rubocop:disable Lint/UnusedBlockArgument
|
|
45
|
+
end
|
|
46
|
+
attr_writer :authorization_strategy
|
|
47
|
+
|
|
48
|
+
def before_request; end
|
|
49
|
+
|
|
50
|
+
def after_request; end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveJobject
|
|
4
|
+
module Request
|
|
5
|
+
module Engine
|
|
6
|
+
class NetHttp < ActiveJobject::Request::Engine::Base
|
|
7
|
+
class << self
|
|
8
|
+
# Make a HTTP GET request.
|
|
9
|
+
#
|
|
10
|
+
# @raise [ActiveJobject::Request::Error] error class related with the request.
|
|
11
|
+
#
|
|
12
|
+
# @return [Net::HTTPResponse]
|
|
13
|
+
def get(site:, headers: {}, params: {})
|
|
14
|
+
site.query = URI.encode_www_form(params)
|
|
15
|
+
req = Net::HTTP::Get.new(site)
|
|
16
|
+
|
|
17
|
+
headers.each do |key, value|
|
|
18
|
+
req[key] = value
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
response = Net::HTTP.start(site.hostname, site.port) do |http|
|
|
22
|
+
http.request(req)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
raise error_class(response.code.to_i).new(response:, status: response.code.to_i) unless response.class < Net::HTTPSuccess
|
|
26
|
+
|
|
27
|
+
response
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Make a HTTP POST request.
|
|
31
|
+
#
|
|
32
|
+
# @raise [ActiveJobject::Request::Error] error class related with the request.
|
|
33
|
+
#
|
|
34
|
+
# @return [Net::HTTPResponse]
|
|
35
|
+
def post(site:, headers: {}, params: {}, body: nil)
|
|
36
|
+
site.query = URI.encode_www_form(params)
|
|
37
|
+
req = Net::HTTP::Post.new(site)
|
|
38
|
+
|
|
39
|
+
headers.each do |key, value|
|
|
40
|
+
req[key] = value
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
req.body = body.is_a?(Hash) ? JSON.generate(body) : body
|
|
44
|
+
|
|
45
|
+
response = Net::HTTP.start(site.hostname, site.port) do |http|
|
|
46
|
+
http.request(req)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
raise error_class(response.code.to_i).new(response:, status: response.code.to_i) unless response.class < Net::HTTPSuccess
|
|
50
|
+
|
|
51
|
+
response
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Make a HTTP PUT request.
|
|
55
|
+
#
|
|
56
|
+
# @raise [ActiveJobject::Request::Error] error class related with the request.
|
|
57
|
+
#
|
|
58
|
+
# @return [Net::HTTPResponse]
|
|
59
|
+
def put(site:, headers: {}, params: {}, body: nil)
|
|
60
|
+
site.query = URI.encode_www_form(params)
|
|
61
|
+
req = Net::HTTP::Put.new(site)
|
|
62
|
+
|
|
63
|
+
headers.each do |key, value|
|
|
64
|
+
req[key] = value
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
req.body = body.is_a?(Hash) ? JSON.generate(body) : body
|
|
68
|
+
|
|
69
|
+
response = Net::HTTP.start(site.hostname, site.port) do |http|
|
|
70
|
+
http.request(req)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
raise error_class(response.code.to_i).new(response:, status: response.code.to_i) unless response.class < Net::HTTPSuccess
|
|
74
|
+
|
|
75
|
+
response
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Make a HTTP PATCH request.
|
|
79
|
+
#
|
|
80
|
+
# @raise [ActiveJobject::Request::Error] error class related with the request.
|
|
81
|
+
#
|
|
82
|
+
# @return [Net::HTTPResponse]
|
|
83
|
+
def patch(site:, headers: {}, params: {}, body: nil)
|
|
84
|
+
site.query = URI.encode_www_form(params)
|
|
85
|
+
req = Net::HTTP::Patch.new(site)
|
|
86
|
+
|
|
87
|
+
headers.each do |key, value|
|
|
88
|
+
req[key] = value
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
req.body = body.is_a?(Hash) ? JSON.generate(body) : body
|
|
92
|
+
|
|
93
|
+
response = Net::HTTP.start(site.hostname, site.port) do |http|
|
|
94
|
+
http.request(req)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
raise error_class(response.code.to_i).new(response:, status: response.code.to_i) unless response.class < Net::HTTPSuccess
|
|
98
|
+
|
|
99
|
+
response
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Make a HTTP DELETE request.
|
|
103
|
+
#
|
|
104
|
+
# @raise [ActiveJobject::Request::Error] error class related with the request.
|
|
105
|
+
#
|
|
106
|
+
# @return [Net::HTTPResponse]
|
|
107
|
+
def delete(site:, headers: {}, params: {})
|
|
108
|
+
site.query = URI.encode_www_form(params)
|
|
109
|
+
req = Net::HTTP::Delete.new(site)
|
|
110
|
+
|
|
111
|
+
headers.each do |key, value|
|
|
112
|
+
req[key] = value
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
response = Net::HTTP.start(site.hostname, site.port) do |http|
|
|
116
|
+
http.request(req)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
raise error_class(response.code.to_i).new(response:, status: response.code.to_i) unless response.class < Net::HTTPSuccess
|
|
120
|
+
|
|
121
|
+
response
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Optional Dependency since the default engine is Net/HTTP
|
|
4
|
+
require 'oauth2' if Gem::Specification.find_all_by_name('oauth2').any?
|
|
5
|
+
|
|
6
|
+
module ActiveJobject
|
|
7
|
+
module Request
|
|
8
|
+
module Engine
|
|
9
|
+
class OAuth2 < ActiveJobject::Request::Engine::Base
|
|
10
|
+
class << self
|
|
11
|
+
# Make a HTTP GET request.
|
|
12
|
+
#
|
|
13
|
+
# @raise [ActiveJobject::Request::Error] error class related with the request.
|
|
14
|
+
#
|
|
15
|
+
# @return [OAuth2::Response] the response from the request
|
|
16
|
+
#
|
|
17
|
+
# @see OAuth2::Client#request
|
|
18
|
+
def get(site:, headers: {}, params: {})
|
|
19
|
+
host, path = deconstruct_uri(site)
|
|
20
|
+
|
|
21
|
+
authorization_strategy.call(site: host).get(path, params:, headers:)
|
|
22
|
+
rescue ::OAuth2::Error => e
|
|
23
|
+
raise handle_error(e, caller)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Make a HTTP POST request.
|
|
27
|
+
#
|
|
28
|
+
# @raise [ActiveJobject::Request::Error] error class related with the request.
|
|
29
|
+
#
|
|
30
|
+
# @return [OAuth2::Response] the response from the request
|
|
31
|
+
#
|
|
32
|
+
# @see OAuth2::Client#request
|
|
33
|
+
def post(site:, headers: {}, params: {}, body: nil)
|
|
34
|
+
host, path = deconstruct_uri(site)
|
|
35
|
+
|
|
36
|
+
authorization_strategy.call(site: host).post(path, params:, headers:, body:)
|
|
37
|
+
rescue ::OAuth2::Error => e
|
|
38
|
+
raise handle_error(e, caller)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Make a HTTP PUT request.
|
|
42
|
+
#
|
|
43
|
+
# @raise [ActiveJobject::Request::Error] error class related with the request.
|
|
44
|
+
#
|
|
45
|
+
# @return [OAuth2::Response] the response from the request
|
|
46
|
+
#
|
|
47
|
+
# @see OAuth2::Client#request
|
|
48
|
+
def put(site:, headers: {}, params: {}, body: nil)
|
|
49
|
+
host, path = deconstruct_uri(site)
|
|
50
|
+
|
|
51
|
+
authorization_strategy.call(site: host).put(path, params:, headers:, body:)
|
|
52
|
+
rescue ::OAuth2::Error => e
|
|
53
|
+
raise handle_error(e, caller)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Make a HTTP PATCH request.
|
|
57
|
+
#
|
|
58
|
+
# @raise [ActiveJobject::Request::Error] error class related with the request.
|
|
59
|
+
#
|
|
60
|
+
# @return [OAuth2::Response] the response from the request
|
|
61
|
+
#
|
|
62
|
+
# @see OAuth2::Client#request
|
|
63
|
+
def patch(site:, headers: {}, params: {}, body: nil)
|
|
64
|
+
host, path = deconstruct_uri(site)
|
|
65
|
+
|
|
66
|
+
authorization_strategy.call(site: host).patch(path, params:, headers:, body:)
|
|
67
|
+
rescue ::OAuth2::Error => e
|
|
68
|
+
raise handle_error(e, caller)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Make a HTTP DELETE request.
|
|
72
|
+
#
|
|
73
|
+
# @raise [ActiveJobject::Request::Error] error class related with the request.
|
|
74
|
+
#
|
|
75
|
+
# @return [OAuth2::Response] the response from the request
|
|
76
|
+
#
|
|
77
|
+
# @see OAuth2::Client#request
|
|
78
|
+
def delete(site:, headers: {}, params: {})
|
|
79
|
+
host, path = deconstruct_uri(site)
|
|
80
|
+
|
|
81
|
+
authorization_strategy.call(site: host).delete(path, params:, headers:)
|
|
82
|
+
rescue ::OAuth2::Error => e
|
|
83
|
+
raise handle_error(e, caller)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
def handle_error(error, original_caller)
|
|
89
|
+
raise error if error.class != ::OAuth2::Error
|
|
90
|
+
|
|
91
|
+
response_status = error.response&.status
|
|
92
|
+
http_error_class = error_class(response_status).new(response: error.response, status: response_status)
|
|
93
|
+
|
|
94
|
+
http_error_class.set_backtrace(original_caller)
|
|
95
|
+
raise http_error_class
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def deconstruct_uri(uri)
|
|
99
|
+
host_with_scheme = [80, 443].include?(uri.port) ? "#{uri.scheme}://#{uri.host}/" : "#{uri.scheme}://#{uri.host}:#{uri.port}/"
|
|
100
|
+
path = uri.path
|
|
101
|
+
|
|
102
|
+
[host_with_scheme, path]
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveJobject
|
|
4
|
+
module Request
|
|
5
|
+
class Error < ActiveJobject::Error
|
|
6
|
+
def initialize(response:, status:)
|
|
7
|
+
@response = response
|
|
8
|
+
@status = status
|
|
9
|
+
|
|
10
|
+
super("Request Failed: Status #{status}.")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def to_h
|
|
14
|
+
{ message:, status: @status, backtrace: backtrace }
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Errors related to HTTP Status 401
|
|
19
|
+
class Unauthorized < Error; end
|
|
20
|
+
|
|
21
|
+
# Errors related to HTTP Status 403
|
|
22
|
+
class Forbidden < Error; end
|
|
23
|
+
|
|
24
|
+
# Errors related to HTTP Status 404
|
|
25
|
+
class NotFound < Error; end
|
|
26
|
+
|
|
27
|
+
# Errors related to HTTP Status 500
|
|
28
|
+
class ServerError < Error; end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveJobject
|
|
4
|
+
module Request
|
|
5
|
+
include ActiveJobject::Parser
|
|
6
|
+
|
|
7
|
+
def self.included(base)
|
|
8
|
+
base.extend(ClassMethods)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
module ClassMethods
|
|
12
|
+
def engine=(engine)
|
|
13
|
+
@engine = engine
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def engine
|
|
17
|
+
@engine ||= ActiveJobject::Request::Engine::NetHttp
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def default_headers=(headers)
|
|
21
|
+
@default_headers = headers
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def default_headers
|
|
25
|
+
@default_headers ||= {}
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def get(headers: {}, params: {})
|
|
30
|
+
merged_headers = self.class.default_headers.merge(headers)
|
|
31
|
+
response = self.class.engine.get(site: uri, headers: merged_headers, params:)
|
|
32
|
+
|
|
33
|
+
parse(self, **JSON.parse(response.body))
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def post(headers: {}, params: {}, body: nil)
|
|
37
|
+
merged_headers = self.class.default_headers.merge(headers)
|
|
38
|
+
response = self.class.engine.post(site: uri, headers: merged_headers, params:, body:)
|
|
39
|
+
|
|
40
|
+
parse(self, **JSON.parse(response.body))
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def put; end
|
|
44
|
+
|
|
45
|
+
def delete; end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def error_class(status)
|
|
50
|
+
case status
|
|
51
|
+
when 404
|
|
52
|
+
ActiveJobject::Request::NotFound
|
|
53
|
+
when 500
|
|
54
|
+
ActiveJobject::Request::ServerError
|
|
55
|
+
else
|
|
56
|
+
ActiveJobject::Request::Error
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'net/http'
|
|
5
|
+
|
|
6
|
+
module ActiveJobject
|
|
7
|
+
class Error < StandardError; end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
require_relative 'active_jobject/version'
|
|
11
|
+
|
|
12
|
+
require_relative 'active_jobject/collection'
|
|
13
|
+
require_relative 'active_jobject/parser'
|
|
14
|
+
|
|
15
|
+
require_relative 'active_jobject/request'
|
|
16
|
+
require_relative 'active_jobject/request/engine/base'
|
|
17
|
+
require_relative 'active_jobject/request/engine/net_http'
|
|
18
|
+
require_relative 'active_jobject/request/engine/oauth2'
|
|
19
|
+
require_relative 'active_jobject/request/error'
|
|
20
|
+
|
|
21
|
+
require_relative 'active_jobject/base'
|
metadata
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: active_jobject
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Guilherme Theodoro
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: This gem encapsulates and allows for multi-engine or multi-gem HTTP requests
|
|
13
|
+
that requests and respond in JSON.
|
|
14
|
+
email:
|
|
15
|
+
- gtheodoro@rescocompany.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- ".solargraph.yml"
|
|
21
|
+
- LICENSE.txt
|
|
22
|
+
- README.md
|
|
23
|
+
- Rakefile
|
|
24
|
+
- lib/active_jobject.rb
|
|
25
|
+
- lib/active_jobject/base.rb
|
|
26
|
+
- lib/active_jobject/collection.rb
|
|
27
|
+
- lib/active_jobject/parser.rb
|
|
28
|
+
- lib/active_jobject/request.rb
|
|
29
|
+
- lib/active_jobject/request/engine/base.rb
|
|
30
|
+
- lib/active_jobject/request/engine/net_http.rb
|
|
31
|
+
- lib/active_jobject/request/engine/oauth2.rb
|
|
32
|
+
- lib/active_jobject/request/error.rb
|
|
33
|
+
- lib/active_jobject/version.rb
|
|
34
|
+
- sig/active_jobject.rbs
|
|
35
|
+
homepage: https://rescocompany.com
|
|
36
|
+
licenses:
|
|
37
|
+
- MIT
|
|
38
|
+
metadata:
|
|
39
|
+
allowed_push_host: https://rubygems.org
|
|
40
|
+
homepage_uri: https://rescocompany.com
|
|
41
|
+
source_code_uri: https://github.com/RESCO-Company/active_jobject
|
|
42
|
+
rubygems_mfa_required: 'true'
|
|
43
|
+
rdoc_options: []
|
|
44
|
+
require_paths:
|
|
45
|
+
- lib
|
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: 3.3.0
|
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '0'
|
|
56
|
+
requirements: []
|
|
57
|
+
rubygems_version: 3.6.9
|
|
58
|
+
specification_version: 4
|
|
59
|
+
summary: HTTP Engine Object Wrapper
|
|
60
|
+
test_files: []
|