remote_entity 1.0.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: 33a2e9d809f7e4a0c941f234aa3e3d3c7233934cb476b44505ea12ae8980847d
4
+ data.tar.gz: 3eadac241251a0e74fa92673864799579bc20b3fb6fd8d8fd894566379b56caf
5
+ SHA512:
6
+ metadata.gz: 4a4ddfb164a2ab1c6509fbfba0892ae8e0c8a0edc3c23fb67e8235971ee58c4df6dd585d0bba8f7cdd9f1b161bef0d23c4b3143676531b0c22811f6389e15184
7
+ data.tar.gz: 3512aaac40f588bae7f232b77e8953662718182d0473ce946ca7cacd8839fef15633c042330c601bb201167b6060718688a585d0f17a4d8ab551c075631f974e
data/.idea/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Editor-based HTTP Client requests
5
+ /httpRequests/
6
+ # Datasource local storage ignored files
7
+ /dataSources/
8
+ /dataSources.local.xml
data/.idea/misc.xml ADDED
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager" version="2" project-jdk-name="rbenv: 2.5.5" project-jdk-type="RUBY_SDK" />
4
+ </project>
data/.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/remote_entity.iml" filepath="$PROJECT_DIR$/.idea/remote_entity.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,82 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared />
5
+ </component>
6
+ <component name="NewModuleRootManager">
7
+ <content url="file://$MODULE_DIR$">
8
+ <sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
9
+ <sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
10
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
11
+ </content>
12
+ <orderEntry type="jdk" jdkName="rbenv: 2.6.6" jdkType="RUBY_SDK" />
13
+ <orderEntry type="sourceFolder" forTests="false" />
14
+ <orderEntry type="library" scope="PROVIDED" name="ast (v2.4.2, rbenv: 2.6.6) [gem]" level="application" />
15
+ <orderEntry type="library" scope="PROVIDED" name="base64 (v0.2.0, rbenv: 2.6.6) [gem]" level="application" />
16
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v2.1.4, rbenv: 2.6.6) [gem]" level="application" />
17
+ <orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.5.0, rbenv: 2.6.6) [gem]" level="application" />
18
+ <orderEntry type="library" scope="PROVIDED" name="faraday (v2.8.1, rbenv: 2.6.6) [gem]" level="application" />
19
+ <orderEntry type="library" scope="PROVIDED" name="faraday-net_http (v3.0.2, rbenv: 2.6.6) [gem]" level="application" />
20
+ <orderEntry type="library" scope="PROVIDED" name="hashie (v5.0.0, rbenv: 2.6.6) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="json (v2.7.1, rbenv: 2.6.6) [gem]" level="application" />
22
+ <orderEntry type="library" scope="PROVIDED" name="jwt (v2.7.1, rbenv: 2.6.6) [gem]" level="application" />
23
+ <orderEntry type="library" scope="PROVIDED" name="multi_xml (v0.6.0, rbenv: 2.6.6) [gem]" level="application" />
24
+ <orderEntry type="library" scope="PROVIDED" name="oauth2 (v2.0.9, rbenv: 2.6.6) [gem]" level="application" />
25
+ <orderEntry type="library" scope="PROVIDED" name="parallel (v1.24.0, rbenv: 2.6.6) [gem]" level="application" />
26
+ <orderEntry type="library" scope="PROVIDED" name="parser (v3.2.2.4, rbenv: 2.6.6) [gem]" level="application" />
27
+ <orderEntry type="library" scope="PROVIDED" name="racc (v1.7.3, rbenv: 2.6.6) [gem]" level="application" />
28
+ <orderEntry type="library" scope="PROVIDED" name="rack (v3.0.8, rbenv: 2.6.6) [gem]" level="application" />
29
+ <orderEntry type="library" scope="PROVIDED" name="rainbow (v3.1.1, rbenv: 2.6.6) [gem]" level="application" />
30
+ <orderEntry type="library" scope="PROVIDED" name="rake (v13.1.0, rbenv: 2.6.6) [gem]" level="application" />
31
+ <orderEntry type="library" scope="PROVIDED" name="regexp_parser (v2.8.3, rbenv: 2.6.6) [gem]" level="application" />
32
+ <orderEntry type="library" scope="PROVIDED" name="rexml (v3.2.6, rbenv: 2.6.6) [gem]" level="application" />
33
+ <orderEntry type="library" scope="PROVIDED" name="rspec (v3.12.0, rbenv: 2.6.6) [gem]" level="application" />
34
+ <orderEntry type="library" scope="PROVIDED" name="rspec-core (v3.12.2, rbenv: 2.6.6) [gem]" level="application" />
35
+ <orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v3.12.3, rbenv: 2.6.6) [gem]" level="application" />
36
+ <orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.12.6, rbenv: 2.6.6) [gem]" level="application" />
37
+ <orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.12.1, rbenv: 2.6.6) [gem]" level="application" />
38
+ <orderEntry type="library" scope="PROVIDED" name="rubocop (v1.50.2, rbenv: 2.6.6) [gem]" level="application" />
39
+ <orderEntry type="library" scope="PROVIDED" name="rubocop-ast (v1.30.0, rbenv: 2.6.6) [gem]" level="application" />
40
+ <orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.13.0, rbenv: 2.6.6) [gem]" level="application" />
41
+ <orderEntry type="library" scope="PROVIDED" name="ruby2_keywords (v0.0.5, rbenv: 2.6.6) [gem]" level="application" />
42
+ <orderEntry type="library" scope="PROVIDED" name="snaky_hash (v2.0.1, rbenv: 2.6.6) [gem]" level="application" />
43
+ <orderEntry type="library" scope="PROVIDED" name="unicode-display_width (v2.5.0, rbenv: 2.6.6) [gem]" level="application" />
44
+ <orderEntry type="library" scope="PROVIDED" name="version_gem (v1.1.3, rbenv: 2.6.6) [gem]" level="application" />
45
+ </component>
46
+ <component name="RakeTasksCache-v2">
47
+ <option name="myRootTask">
48
+ <RakeTaskImpl id="rake">
49
+ <subtasks>
50
+ <RakeTaskImpl description="Build remote_entity-0.1.0.gem into the pkg directory" fullCommand="build" id="build" />
51
+ <RakeTaskImpl description="Remove any temporary products" fullCommand="clean" id="clean" />
52
+ <RakeTaskImpl description="Remove any generated files" fullCommand="clobber" id="clobber" />
53
+ <RakeTaskImpl description="Build and install remote_entity-0.1.0.gem into system gems" fullCommand="install" id="install" />
54
+ <RakeTaskImpl id="install">
55
+ <subtasks>
56
+ <RakeTaskImpl description="Build and install remote_entity-0.1.0.gem into system gems without network access" fullCommand="install:local" id="local" />
57
+ </subtasks>
58
+ </RakeTaskImpl>
59
+ <RakeTaskImpl description="Create tag v0.1.0 and build and push remote_entity-0.1.0.gem to Set to your gem server 'https://example.com'" fullCommand="release[remote]" id="release[remote]" />
60
+ <RakeTaskImpl description="Run RuboCop" fullCommand="rubocop" id="rubocop" />
61
+ <RakeTaskImpl id="rubocop">
62
+ <subtasks>
63
+ <RakeTaskImpl description="Autocorrect RuboCop offenses (only when it's safe)" fullCommand="rubocop:autocorrect" id="autocorrect" />
64
+ <RakeTaskImpl description="Autocorrect RuboCop offenses (safe and unsafe)" fullCommand="rubocop:autocorrect_all" id="autocorrect_all" />
65
+ <RakeTaskImpl description="" fullCommand="rubocop:auto_correct" id="auto_correct" />
66
+ </subtasks>
67
+ </RakeTaskImpl>
68
+ <RakeTaskImpl description="Run RSpec code examples" fullCommand="spec" id="spec" />
69
+ <RakeTaskImpl description="" fullCommand="default" id="default" />
70
+ <RakeTaskImpl description="" fullCommand="release" id="release" />
71
+ <RakeTaskImpl id="release">
72
+ <subtasks>
73
+ <RakeTaskImpl description="" fullCommand="release:guard_clean" id="guard_clean" />
74
+ <RakeTaskImpl description="" fullCommand="release:rubygem_push" id="rubygem_push" />
75
+ <RakeTaskImpl description="" fullCommand="release:source_control_push" id="source_control_push" />
76
+ </subtasks>
77
+ </RakeTaskImpl>
78
+ </subtasks>
79
+ </RakeTaskImpl>
80
+ </option>
81
+ </component>
82
+ </module>
data/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="" vcs="Git" />
5
+ </component>
6
+ </project>
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,28 @@
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
14
+ Exclude:
15
+ - '*.gemspec'
16
+
17
+ Metrics/AbcSize:
18
+ Max: 50
19
+
20
+ Metrics/CyclomaticComplexity:
21
+ Max: 10
22
+
23
+ Metrics/MethodLength:
24
+ Max: 30
25
+
26
+ Metrics/BlockLength:
27
+ Exclude:
28
+ - 'spec/**/*'
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-3.2.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [1.0.0] - 2024-01-18
2
+
3
+ - 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 kuroun.seung@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,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in remote_entity.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0", require: false
9
+ gem "rspec", "~> 3.0", require: false
10
+ gem "rubocop", "~> 1.21", require: false
11
+
12
+ group :test do
13
+ gem "simplecov", require: false
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,94 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ remote_entity (1.0.0)
5
+ cgi (~> 0.2)
6
+ oauth2 (~> 2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ cgi (0.4.1)
13
+ diff-lcs (1.5.0)
14
+ docile (1.4.0)
15
+ faraday (2.9.0)
16
+ faraday-net_http (>= 2.0, < 3.2)
17
+ faraday-net_http (3.1.0)
18
+ net-http
19
+ hashie (5.0.0)
20
+ json (2.7.1)
21
+ jwt (2.7.1)
22
+ language_server-protocol (3.17.0.3)
23
+ multi_xml (0.6.0)
24
+ net-http (0.4.1)
25
+ uri
26
+ oauth2 (2.0.9)
27
+ faraday (>= 0.17.3, < 3.0)
28
+ jwt (>= 1.0, < 3.0)
29
+ multi_xml (~> 0.5)
30
+ rack (>= 1.2, < 4)
31
+ snaky_hash (~> 2.0)
32
+ version_gem (~> 1.1)
33
+ parallel (1.24.0)
34
+ parser (3.3.0.3)
35
+ ast (~> 2.4.1)
36
+ racc
37
+ racc (1.7.3)
38
+ rack (3.0.8)
39
+ rainbow (3.1.1)
40
+ rake (13.1.0)
41
+ regexp_parser (2.9.0)
42
+ rexml (3.2.6)
43
+ rspec (3.12.0)
44
+ rspec-core (~> 3.12.0)
45
+ rspec-expectations (~> 3.12.0)
46
+ rspec-mocks (~> 3.12.0)
47
+ rspec-core (3.12.2)
48
+ rspec-support (~> 3.12.0)
49
+ rspec-expectations (3.12.3)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.12.0)
52
+ rspec-mocks (3.12.6)
53
+ diff-lcs (>= 1.2.0, < 2.0)
54
+ rspec-support (~> 3.12.0)
55
+ rspec-support (3.12.1)
56
+ rubocop (1.59.0)
57
+ json (~> 2.3)
58
+ language_server-protocol (>= 3.17.0)
59
+ parallel (~> 1.10)
60
+ parser (>= 3.2.2.4)
61
+ rainbow (>= 2.2.2, < 4.0)
62
+ regexp_parser (>= 1.8, < 3.0)
63
+ rexml (>= 3.2.5, < 4.0)
64
+ rubocop-ast (>= 1.30.0, < 2.0)
65
+ ruby-progressbar (~> 1.7)
66
+ unicode-display_width (>= 2.4.0, < 3.0)
67
+ rubocop-ast (1.30.0)
68
+ parser (>= 3.2.1.0)
69
+ ruby-progressbar (1.13.0)
70
+ simplecov (0.22.0)
71
+ docile (~> 1.1)
72
+ simplecov-html (~> 0.11)
73
+ simplecov_json_formatter (~> 0.1)
74
+ simplecov-html (0.12.3)
75
+ simplecov_json_formatter (0.1.4)
76
+ snaky_hash (2.0.1)
77
+ hashie
78
+ version_gem (~> 1.1, >= 1.1.1)
79
+ unicode-display_width (2.5.0)
80
+ uri (0.13.0)
81
+ version_gem (1.1.3)
82
+
83
+ PLATFORMS
84
+ ruby
85
+
86
+ DEPENDENCIES
87
+ rake (~> 13.0)
88
+ remote_entity!
89
+ rspec (~> 3.0)
90
+ rubocop (~> 1.21)
91
+ simplecov
92
+
93
+ BUNDLED WITH
94
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Kuroun Seung
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,111 @@
1
+ # RemoteEntity
2
+
3
+ A gem to create entity classes and methods which make http request to remote servers using **configuration style**. It avoids writing duplicated code that communicates to multiple remote services.
4
+
5
+ It is something less restricts than [her](https://github.com/remi/her) which maps to any http requests (not only for RESTful resources) but comparing to [rest-client](https://github.com/rest-client/rest-client), this gem is more object oriented. See usage below for details.
6
+
7
+ Currently it supports oauth2 `client_credentials` grant type for API authentication.
8
+
9
+ ## Installation
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add remote_entity
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install remote_entity
18
+
19
+ ## Usage
20
+
21
+ ```
22
+ RemoteEntity.configure(
23
+ name: "User",
24
+ methods: [
25
+ {
26
+ name: "find",
27
+ http_method: "Get",
28
+ url: "https://example.com/users/:id",
29
+ param_mapping: {
30
+ path_params: [:id],
31
+ query_params: [:public]
32
+ },
33
+ authentication: {
34
+ method: "oauth2.client_credentials"
35
+ },
36
+ r_turn: true
37
+ },
38
+ {
39
+ name: "create",
40
+ http_method: "Post",
41
+ url: "https://example.com/users",
42
+ param_mapping: {
43
+ body_params: [:name, :age]
44
+ },
45
+ r_turn: false
46
+ }
47
+ ],
48
+ authentications: {
49
+ oauth2: {
50
+ client_credentials: {
51
+ client_id: "client-id",
52
+ client_secret: "client-secret",
53
+ site: "https:/example.com",
54
+ token_url: "/token",
55
+ scope: "users:read users:write",
56
+ }
57
+ }
58
+ }
59
+ )
60
+ ```
61
+ ```
62
+ User.find(id: 1, public: true)
63
+ ```
64
+ Above will translate to following `GET` http request. `param_mapping` will transform any `path parameter`, and `query parameter` of the request into `User` entity method parameter.
65
+ ```
66
+ curl --location 'https://example.com/users/1?public=true' \
67
+ --header 'Content-Type: application/json' \
68
+ --header 'Authorization: Bearer <token>'
69
+ ```
70
+ `Content-Type` header as `application/json` is by default.
71
+
72
+ `Authorization` header is generated by oauth2 request using grant type defined in the configuration. The example above uses ouath2 client_credentials grant type. (The gem uses [oauth2](https://gitlab.com/oauth-xx/oauth2/) gem underdying, so the param structure is pretty the same as oauth2)
73
+
74
+ Since the `find` method is configured to return value (`r_turn` is set to `true`), it will return whatever the http response has. For example:
75
+ ```
76
+ {
77
+ id: 1,
78
+ name: "Jonas",
79
+ age: 22
80
+ }
81
+ ```
82
+ ```
83
+ User.create(name: "John", age: 23)
84
+ ```
85
+ Above will translate to following `POST` http request. `param_mapping` will transform `body parameter` of the request into `User` entity method parameter.
86
+ ```
87
+ curl --location 'https://example.com/users' \
88
+ --header 'Content-Type: application/json' \
89
+ --data-raw '
90
+ {
91
+ "name": "John",
92
+ "age": 23
93
+ }'
94
+ ```
95
+ It has no authorization configured, so no `Authorization` header.
96
+
97
+ It does not return anything as `r_turn` is set to `false`
98
+
99
+ **NOTE**: All http request params whether it is query parameter, path parameter or body parameter, it will always transform into key/value pairs parameter of the entity method.
100
+
101
+ ## Contributing
102
+
103
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kseung-gpsw/remote_entity. 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]/remote_entity/blob/main/CODE_OF_CONDUCT.md).
104
+
105
+ ## License
106
+
107
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
108
+
109
+ ## Code of Conduct
110
+
111
+ Everyone interacting in the RemoteEntity project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/remote_entity/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RemoteEntity
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "remote_entity/version"
4
+ require "oauth2"
5
+ require "time"
6
+ require "cgi"
7
+ require "json"
8
+ require "net/http"
9
+
10
+ # RemoteEntity is a gem that allows you to define a class that can call remote APIs.
11
+ module RemoteEntity
12
+ class Error < StandardError; end
13
+
14
+ # Your code goes here...
15
+ def self.configure(options)
16
+ raise "missing required parameter - name" if options[:name].nil?
17
+ raise "missing required parameter - methods" if options[:methods].nil?
18
+
19
+ Object.const_set(options[:name], RemoteEntity.build_dynamic_class(options))
20
+ end
21
+
22
+ def self.build_dynamic_class(options)
23
+ Class.new do
24
+ options[:methods].each do |method|
25
+ singleton_class.send(:define_method, method[:name]) do |arg|
26
+ raise "invalid parameter type - accepted only hash" unless arg.is_a? Hash
27
+
28
+ url = RemoteEntity.build_url(method, arg)
29
+ https = Net::HTTP.new(url.host, url.port)
30
+ https.use_ssl = true
31
+ http_class = Object.const_get("Net::HTTP::#{method[:http_method]}")
32
+ request = http_class.new(url)
33
+ request["Content-Type"] = "application/json"
34
+
35
+ if method[:authentication] && method[:authentication][:method].include?("oauth2")
36
+ request["Authorization"] =
37
+ RemoteEntity.build_oauth2_authorized_token_header(method[:authentication][:method],
38
+ options[:authentications])
39
+ end
40
+
41
+ if method[:param_mapping] && method[:param_mapping][:body_params]
42
+ request.body = JSON.dump(RemoteEntity.build_body_params(method[:param_mapping][:body_params], arg))
43
+ end
44
+
45
+ response = https.request(request)
46
+ attributes = JSON.parse(response.read_body)
47
+ return attributes if method[:r_turn]
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ def self.build_url(method, arg)
54
+ url = method[:url]
55
+
56
+ if method[:param_mapping] && method[:param_mapping][:path_params]
57
+ url = RemoteEntity.build_path_params(url, method[:param_mapping][:path_params], arg)
58
+ end
59
+
60
+ if method[:param_mapping] && method[:param_mapping][:query_params]
61
+ url = RemoteEntity.build_query_params(url, method[:param_mapping][:query_params], arg)
62
+ end
63
+
64
+ URI(url)
65
+ end
66
+
67
+ def self.build_path_params(base_url, keys, params)
68
+ result = base_url
69
+ keys.each do |k|
70
+ result = result.gsub(":#{k}", params[k].to_s)
71
+ end
72
+ result
73
+ end
74
+
75
+ def self.build_query_params(base_url, keys, params)
76
+ if keys.size.positive?
77
+ result = "#{base_url}?"
78
+ keys.each do |k|
79
+ result = "#{result}#{k}=#{CGI.escape(params[k].to_s)}&"
80
+ end
81
+ # remove the last &
82
+ return result[0...-1]
83
+ end
84
+ base_url
85
+ end
86
+
87
+ def self.build_body_params(keys, params)
88
+ result = {}
89
+ keys.each do |k|
90
+ result[k.to_sym] = params[k.to_sym]
91
+ end
92
+ result
93
+ end
94
+
95
+ def self.build_oauth2_authorized_token_header(method, method_inventory)
96
+ # TODO: implement cache
97
+ grant_type = method.split(".")[1].to_sym
98
+ credentials_info = method_inventory[:oauth2][grant_type]
99
+
100
+ client = OAuth2::Client.new(credentials_info[:client_id],
101
+ credentials_info[:client_secret],
102
+ site: credentials_info[:site],
103
+ token_url: credentials_info[:token_url])
104
+ token = client.send(grant_type).get_token(scope: credentials_info[:scope]).token
105
+ "Bearer #{token}"
106
+ end
107
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/remote_entity/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "remote_entity"
7
+ spec.version = RemoteEntity::VERSION
8
+ spec.authors = ["Kuroun Seung"]
9
+ spec.email = ["kuroun.seung@gmail.com"]
10
+
11
+ spec.summary = "Using configuration style to generate Ruby classes and methods that wrap the API calls to a remote service."
12
+ spec.homepage = "https://github.com/kseung-gpsw/remote_entity"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/kseung-gpsw/remote_entity"
20
+ spec.metadata["changelog_uri"] = "https://github.com/kseung-gpsw/remote_entity/blob/main/CHANGELOG.md"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git]) || f.end_with?(".gem")
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ # Uncomment to register a new dependency of your gem
34
+ spec.add_dependency "cgi", "~> 0.2"
35
+ spec.add_dependency "oauth2", "~> 2.0"
36
+
37
+ # For more information and examples about making a new gem, check out our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
@@ -0,0 +1,4 @@
1
+ module RemoteEntity
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: remote_entity
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kuroun Seung
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-01-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cgi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: oauth2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ description:
42
+ email:
43
+ - kuroun.seung@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".idea/.gitignore"
49
+ - ".idea/misc.xml"
50
+ - ".idea/modules.xml"
51
+ - ".idea/remote_entity.iml"
52
+ - ".idea/vcs.xml"
53
+ - ".rspec"
54
+ - ".rubocop.yml"
55
+ - ".ruby-version"
56
+ - CHANGELOG.md
57
+ - CODE_OF_CONDUCT.md
58
+ - Gemfile
59
+ - Gemfile.lock
60
+ - LICENSE.txt
61
+ - README.md
62
+ - Rakefile
63
+ - lib/remote_entity.rb
64
+ - lib/remote_entity/version.rb
65
+ - remote_entity.gemspec
66
+ - sig/remote_entity.rbs
67
+ homepage: https://github.com/kseung-gpsw/remote_entity
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ allowed_push_host: https://rubygems.org
72
+ homepage_uri: https://github.com/kseung-gpsw/remote_entity
73
+ source_code_uri: https://github.com/kseung-gpsw/remote_entity
74
+ changelog_uri: https://github.com/kseung-gpsw/remote_entity/blob/main/CHANGELOG.md
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 2.6.0
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.4.10
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Using configuration style to generate Ruby classes and methods that wrap
94
+ the API calls to a remote service.
95
+ test_files: []