remote_entity 1.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 33a2e9d809f7e4a0c941f234aa3e3d3c7233934cb476b44505ea12ae8980847d
4
- data.tar.gz: 3eadac241251a0e74fa92673864799579bc20b3fb6fd8d8fd894566379b56caf
3
+ metadata.gz: 8b067d90ced8890a77499154dea7970e77d5805d48a16b5d67eef7890a3d21b9
4
+ data.tar.gz: 99284e3c3f68804086a88b6d0bad1b55b5cc3b52b2c38f1b5a7c314a0334467f
5
5
  SHA512:
6
- metadata.gz: 4a4ddfb164a2ab1c6509fbfba0892ae8e0c8a0edc3c23fb67e8235971ee58c4df6dd585d0bba8f7cdd9f1b161bef0d23c4b3143676531b0c22811f6389e15184
7
- data.tar.gz: 3512aaac40f588bae7f232b77e8953662718182d0473ce946ca7cacd8839fef15633c042330c601bb201167b6060718688a585d0f17a4d8ab551c075631f974e
6
+ metadata.gz: 1e557430c6043180f1ca654c62f143798764a7297154823528cd7e327f68a764a6684224672ca744dca94682562a96d7b9d96123df79651568c7773e4bed1c53
7
+ data.tar.gz: d527cfc59ec21bf1c969dc7c29beb515097f5b62f18597bdb7ee41bfeb3ec867092e21a4f602fee7acec52e5c269d9ac1459f1b1b416d0c925c3ef4c49e8d051
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.6
2
+ TargetRubyVersion: 2.5
3
3
 
4
4
  Style/StringLiterals:
5
5
  Enabled: true
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
1
  ## [1.0.0] - 2024-01-18
2
2
 
3
3
  - Initial release
4
+
5
+ ## [2.0.0] - 2024-02-09
6
+
7
+ - Add `RemoteEntity` namespace to class generated. For example, `RemoteEntity::User`.
8
+
9
+
10
+ ## [2.1.0] - 2024-04-26
11
+
12
+ - Allow instant authorized token in the method params and bypass request to get oauth2 token
13
+ - Allow this gem to use with Ruby >= 2.5.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- remote_entity (1.0.0)
4
+ remote_entity (2.1.0)
5
5
  cgi (~> 0.2)
6
6
  oauth2 (~> 2.0)
7
7
 
@@ -9,6 +9,7 @@ GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
11
  ast (2.4.2)
12
+ base64 (0.2.0)
12
13
  cgi (0.4.1)
13
14
  diff-lcs (1.5.0)
14
15
  docile (1.4.0)
@@ -18,7 +19,8 @@ GEM
18
19
  net-http
19
20
  hashie (5.0.0)
20
21
  json (2.7.1)
21
- jwt (2.7.1)
22
+ jwt (2.8.1)
23
+ base64
22
24
  language_server-protocol (3.17.0.3)
23
25
  multi_xml (0.6.0)
24
26
  net-http (0.4.1)
@@ -35,7 +37,7 @@ GEM
35
37
  ast (~> 2.4.1)
36
38
  racc
37
39
  racc (1.7.3)
38
- rack (3.0.8)
40
+ rack (3.0.10)
39
41
  rainbow (3.1.1)
40
42
  rake (13.1.0)
41
43
  regexp_parser (2.9.0)
@@ -78,7 +80,7 @@ GEM
78
80
  version_gem (~> 1.1, >= 1.1.1)
79
81
  unicode-display_width (2.5.0)
80
82
  uri (0.13.0)
81
- version_gem (1.1.3)
83
+ version_gem (1.1.4)
82
84
 
83
85
  PLATFORMS
84
86
  ruby
data/README.md CHANGED
@@ -6,6 +6,9 @@ It is something less restricts than [her](https://github.com/remi/her) which map
6
6
 
7
7
  Currently it supports oauth2 `client_credentials` grant type for API authentication.
8
8
 
9
+ ## Supporting Ruby versions
10
+ This gem can be used in Ruby >= 2.5.0. However, it might have incompatibilities as this gem was tested with Ruby 3.2.2. Please test thoroughly.
11
+
9
12
  ## Installation
10
13
 
11
14
  Install the gem and add to the application's Gemfile by executing:
@@ -31,7 +34,8 @@ RemoteEntity.configure(
31
34
  query_params: [:public]
32
35
  },
33
36
  authentication: {
34
- method: "oauth2.client_credentials"
37
+ method: "oauth2.client_credentials",
38
+ accepting_instant_token: :authorized_token
35
39
  },
36
40
  r_turn: true
37
41
  },
@@ -58,18 +62,36 @@ RemoteEntity.configure(
58
62
  }
59
63
  )
60
64
  ```
65
+ The configuration generated `RemoteEntity::User` class with `find` method which can be used by the following example:
66
+
67
+ ```
68
+ RemoteEntity::User.find(id: 1, public: true)
61
69
  ```
62
- User.find(id: 1, public: true)
70
+ **NOTE**:
71
+ - All entity classes created are under `RemoteEntity` namespace.
72
+ - The classes generated are a typical Ruby class which can be extended, inherited. For example:
63
73
  ```
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.
74
+ class UserService < RemoteEntity::User
75
+
76
+ end
77
+ ```
78
+ - The methods generated are `class method`.
79
+ - The method parameter generated from configuration is always key/value pairs (hash)
80
+
81
+ The method parameters will be transformed into `path parameter`, `query parameter` or `body parameter` of the request by `param_mapping` configuration. It makes the following `GET` http request:
65
82
  ```
66
83
  curl --location 'https://example.com/users/1?public=true' \
67
84
  --header 'Content-Type: application/json' \
68
85
  --header 'Authorization: Bearer <token>'
69
86
  ```
87
+
70
88
  `Content-Type` header as `application/json` is by default.
71
89
 
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)
90
+ `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). However, if the method has `authentication` key which includes `accepting_instant_token: :authorized_token`, the authentication token request will be bypassed. The `authorized_token` param value will pass instantly in the request `Authorization` header. For example:
91
+
92
+ ```
93
+ RemoteEntity::User.find(id: 1, public: true, authorized_token: "authorized-token")
94
+ ```
73
95
 
74
96
  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
97
  ```
@@ -79,10 +101,11 @@ Since the `find` method is configured to return value (`r_turn` is set to `true`
79
101
  age: 22
80
102
  }
81
103
  ```
104
+ The configuration also creates `create` method. For example:
82
105
  ```
83
- User.create(name: "John", age: 23)
106
+ RemoteEntity::User.create(name: "John", age: 23)
84
107
  ```
85
- Above will translate to following `POST` http request. `param_mapping` will transform `body parameter` of the request into `User` entity method parameter.
108
+ It will make the following `POST` http request:
86
109
  ```
87
110
  curl --location 'https://example.com/users' \
88
111
  --header 'Content-Type: application/json' \
@@ -94,13 +117,11 @@ curl --location 'https://example.com/users' \
94
117
  ```
95
118
  It has no authorization configured, so no `Authorization` header.
96
119
 
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.
120
+ It does not return anything as `r_turn` is set to `false`.
100
121
 
101
122
  ## Contributing
102
123
 
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).
124
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kuroun/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
125
 
105
126
  ## License
106
127
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RemoteEntity
4
- VERSION = "1.0.0"
4
+ VERSION = "2.1.0"
5
5
  end
data/lib/remote_entity.rb CHANGED
@@ -16,7 +16,7 @@ module RemoteEntity
16
16
  raise "missing required parameter - name" if options[:name].nil?
17
17
  raise "missing required parameter - methods" if options[:methods].nil?
18
18
 
19
- Object.const_set(options[:name], RemoteEntity.build_dynamic_class(options))
19
+ RemoteEntity.const_set(options[:name], build_dynamic_class(options))
20
20
  end
21
21
 
22
22
  def self.build_dynamic_class(options)
@@ -32,24 +32,32 @@ module RemoteEntity
32
32
  request = http_class.new(url)
33
33
  request["Content-Type"] = "application/json"
34
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
35
+ RemoteEntity.set_authorization_header(request, method, arg, options) if method[:authentication]
40
36
 
41
37
  if method[:param_mapping] && method[:param_mapping][:body_params]
42
38
  request.body = JSON.dump(RemoteEntity.build_body_params(method[:param_mapping][:body_params], arg))
43
39
  end
44
40
 
45
41
  response = https.request(request)
46
- attributes = JSON.parse(response.read_body)
47
- return attributes if method[:r_turn]
42
+
43
+ return JSON.parse(response.read_body) if method[:r_turn] && !response.read_body.empty?
48
44
  end
49
45
  end
50
46
  end
51
47
  end
52
48
 
49
+ def self.set_authorization_header(request, method, arg, options)
50
+ accepting_instant_token_key = method[:authentication][:accepting_instant_token]
51
+
52
+ if accepting_instant_token_key && arg[accepting_instant_token_key]
53
+ request["Authorization"] = "Bearer #{arg[accepting_instant_token_key]}"
54
+ elsif method[:authentication][:method].include?("oauth2")
55
+ request["Authorization"] =
56
+ RemoteEntity.build_oauth2_authorized_token_header(method[:authentication][:method],
57
+ options[:authentications])
58
+ end
59
+ end
60
+
53
61
  def self.build_url(method, arg)
54
62
  url = method[:url]
55
63
 
@@ -102,6 +110,9 @@ module RemoteEntity
102
110
  site: credentials_info[:site],
103
111
  token_url: credentials_info[:token_url])
104
112
  token = client.send(grant_type).get_token(scope: credentials_info[:scope]).token
113
+
105
114
  "Bearer #{token}"
106
115
  end
116
+
117
+ private_class_method :build_dynamic_class
107
118
  end
@@ -9,15 +9,15 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ["kuroun.seung@gmail.com"]
10
10
 
11
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"
12
+ spec.homepage = "https://github.com/kuroun/remote_entity"
13
13
  spec.license = "MIT"
14
- spec.required_ruby_version = ">= 2.6.0"
14
+ spec.required_ruby_version = ">= 2.5.0"
15
15
 
16
16
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
17
 
18
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"
19
+ spec.metadata["source_code_uri"] = "https://github.com/kuroun/remote_entity"
20
+ spec.metadata["changelog_uri"] = "https://github.com/kuroun/remote_entity/blob/main/CHANGELOG.md"
21
21
 
22
22
  # Specify which files should be added to the gem when it is released.
23
23
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remote_entity
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kuroun Seung
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-18 00:00:00.000000000 Z
11
+ date: 2024-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cgi
@@ -64,14 +64,14 @@ files:
64
64
  - lib/remote_entity/version.rb
65
65
  - remote_entity.gemspec
66
66
  - sig/remote_entity.rbs
67
- homepage: https://github.com/kseung-gpsw/remote_entity
67
+ homepage: https://github.com/kuroun/remote_entity
68
68
  licenses:
69
69
  - MIT
70
70
  metadata:
71
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
72
+ homepage_uri: https://github.com/kuroun/remote_entity
73
+ source_code_uri: https://github.com/kuroun/remote_entity
74
+ changelog_uri: https://github.com/kuroun/remote_entity/blob/main/CHANGELOG.md
75
75
  post_install_message:
76
76
  rdoc_options: []
77
77
  require_paths:
@@ -80,7 +80,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
80
  requirements:
81
81
  - - ">="
82
82
  - !ruby/object:Gem::Version
83
- version: 2.6.0
83
+ version: 2.5.0
84
84
  required_rubygems_version: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - ">="