1
1
# frozen_string_literal: true
2
- # rubocop:todo all
3
2
4
3
# Copyright (C) 2016-2020 MongoDB Inc.
5
4
#
16
15
# limitations under the License.
17
16
18
17
require 'mongo/server/app_metadata/environment'
18
+ require 'mongo/server/app_metadata/platform'
19
19
require 'mongo/server/app_metadata/truncator'
20
20
21
21
module Mongo
@@ -24,35 +24,23 @@ class Server
24
24
# when a new connection is established.
25
25
#
26
26
# @api private
27
- #
28
- # @since 2.4.0
29
27
class AppMetadata
30
28
extend Forwardable
31
29
32
30
# The max application name byte size.
33
- #
34
- # @since 2.4.0
35
- MAX_APP_NAME_SIZE = 128 . freeze
31
+ MAX_APP_NAME_SIZE = 128
36
32
37
33
# The driver name.
38
- #
39
- # @since 2.4.0
40
34
DRIVER_NAME = 'mongo-ruby-driver'
41
35
42
36
# Option keys that affect auth mechanism negotiation.
43
- #
44
- # @api private
45
- AUTH_OPTION_KEYS = [ :user , :auth_source , :auth_mech ] . freeze
37
+ AUTH_OPTION_KEYS = %i[ user auth_source auth_mech ] . freeze
46
38
47
39
# Possible connection purposes.
48
- #
49
- # @api private
50
- PURPOSES = %i( application monitor push_monitor ) . freeze
40
+ PURPOSES = %i[ application monitor push_monitor ] . freeze
51
41
52
42
# Instantiate the new AppMetadata object.
53
43
#
54
- # @api private
55
- #
56
44
# @example Instantiate the app metadata.
57
45
# Mongo::Server::AppMetadata.new(options)
58
46
#
@@ -86,35 +74,33 @@ class AppMetadata
86
74
def initialize ( options = { } )
87
75
@app_name = options [ :app_name ] . to_s if options [ :app_name ]
88
76
@platform = options [ :platform ]
89
- if @purpose = options [ :purpose ]
90
- unless PURPOSES . include? ( @purpose )
91
- raise ArgumentError , "Invalid purpose: #{ @purpose } "
92
- end
93
- end
77
+
78
+ @purpose = check_purpose! ( options [ :purpose ] )
79
+
94
80
@compressors = options [ :compressors ] || [ ]
95
81
@wrapping_libraries = options [ :wrapping_libraries ]
96
82
@server_api = options [ :server_api ]
97
83
98
- if options [ :user ] && !options [ :auth_mech ]
99
- auth_db = options [ :auth_source ] || 'admin'
100
- @request_auth_mech = " #{ auth_db } . #{ options [ :user ] } "
101
- end
84
+ return unless options [ :user ] && !options [ :auth_mech ]
85
+
86
+ auth_db = options [ :auth_source ] || 'admin'
87
+ @request_auth_mech = " #{ auth_db } . #{ options [ :user ] } "
102
88
end
103
89
104
90
# @return [ Symbol ] The purpose of the connection for which this
105
91
# app metadata is created.
106
- #
107
- # @api private
108
92
attr_reader :purpose
109
93
94
+ # @return [ String ] The platform information given when the object was
95
+ # instantiated.
96
+ attr_reader :platform
97
+
110
98
# @return [ Hash | nil ] The requested server API version.
111
99
#
112
100
# Thes hash can have the following items:
113
101
# - *:version* -- string
114
102
# - *:strict* -- boolean
115
103
# - *:deprecation_errors* -- boolean
116
- #
117
- # @api private
118
104
attr_reader :server_api
119
105
120
106
# @return [ Array<Hash> | nil ] Information about libraries wrapping
@@ -130,8 +116,6 @@ def initialize(options = {})
130
116
# @return [BSON::Document] Valid document for connection's handshake.
131
117
#
132
118
# @raise [ Error::InvalidApplicationName ] When the metadata are invalid.
133
- #
134
- # @api private
135
119
def validated_document
136
120
validate!
137
121
document
@@ -141,15 +125,13 @@ def validated_document
141
125
# handshake document.
142
126
#
143
127
# @return [BSON::Document] Document describing client for handshake.
144
- #
145
- # @api private
146
128
def client_document
147
129
@client_document ||=
148
130
BSON ::Document . new . tap do |doc |
149
131
doc [ :application ] = { name : @app_name } if @app_name
150
132
doc [ :driver ] = driver_doc
151
133
doc [ :os ] = os_doc
152
- doc [ :platform ] = platform
134
+ doc [ :platform ] = platform_string
153
135
env_doc . tap { |env | doc [ :env ] = env if env }
154
136
end
155
137
end
@@ -164,6 +146,7 @@ def validate!
164
146
if @app_name && @app_name . bytesize > MAX_APP_NAME_SIZE
165
147
raise Error ::InvalidApplicationName . new ( @app_name , MAX_APP_NAME_SIZE )
166
148
end
149
+
167
150
true
168
151
end
169
152
@@ -183,14 +166,13 @@ def document
183
166
end
184
167
185
168
def driver_doc
186
- names = [ DRIVER_NAME ]
187
- versions = [ Mongo ::VERSION ]
188
- if wrapping_libraries
189
- wrapping_libraries . each do |library |
190
- names << library [ :name ] || ''
191
- versions << library [ :version ] || ''
192
- end
169
+ names = [ DRIVER_NAME ]
170
+ versions = [ Mongo ::VERSION ]
171
+ wrapping_libraries &.each do |library |
172
+ names << ( library [ :name ] || '' )
173
+ versions << ( library [ :version ] || '' )
193
174
end
175
+
194
176
{
195
177
name : names . join ( '|' ) ,
196
178
version : versions . join ( '|' ) ,
@@ -201,18 +183,25 @@ def os_doc
201
183
{
202
184
type : type ,
203
185
name : name ,
204
- architecture : architecture
186
+ architecture : architecture ,
205
187
}
206
188
end
207
189
190
+ # Returns the environment doc describing the current FaaS environment.
191
+ #
192
+ # @return [ Hash | nil ] the environment doc (or nil if not in a FaaS
193
+ # environment).
208
194
def env_doc
209
195
env = Environment . new
210
196
env . faas? ? env . to_h : nil
211
197
end
212
198
213
199
def type
214
- ( RbConfig ::CONFIG && RbConfig ::CONFIG [ 'host_os' ] ) ?
215
- RbConfig ::CONFIG [ 'host_os' ] . split ( '_' ) . first [ /[a-z]+/i ] . downcase : 'unknown'
200
+ if RbConfig ::CONFIG && RbConfig ::CONFIG [ 'host_os' ]
201
+ RbConfig ::CONFIG [ 'host_os' ] . split ( '_' ) . first [ /[a-z]+/i ] . downcase
202
+ else
203
+ 'unknown'
204
+ end
216
205
end
217
206
218
207
def name
@@ -223,31 +212,22 @@ def architecture
223
212
RbConfig ::CONFIG [ 'target_cpu' ]
224
213
end
225
214
226
- def platform
227
- if BSON ::Environment . jruby?
228
- ruby_versions = [ "JRuby #{ JRUBY_VERSION } " , "like Ruby #{ RUBY_VERSION } " ]
229
- platforms = [ RUBY_PLATFORM , "JVM #{ java . lang . System . get_property ( 'java.version' ) } " ]
230
- else
231
- ruby_versions = [ "Ruby #{ RUBY_VERSION } " ]
232
- platforms = [ RUBY_PLATFORM ]
233
- end
234
- platforms = [
235
- @platform ,
236
- *ruby_versions ,
237
- *platforms ,
238
- RbConfig ::CONFIG [ 'build' ] ,
239
- ]
240
- if @purpose
241
- platforms << @purpose . to_s [ 0 ] . upcase
242
- end
243
- platform = platforms . compact . join ( ', ' )
244
- platforms = [ platform ]
245
- if wrapping_libraries
246
- wrapping_libraries . each do |library |
247
- platforms << library [ :platform ] || ''
248
- end
249
- end
250
- platforms . join ( '|' )
215
+ def platform_string
216
+ Platform . new ( self ) . to_s
217
+ end
218
+
219
+ # Verifies that the given purpose is either nil, or is one of the
220
+ # allowed purposes.
221
+ #
222
+ # @param [ String | nil ] purpose The purpose to validate
223
+ #
224
+ # @return [ String | nil ] the {{purpose}} argument
225
+ #
226
+ # @raise [ ArgumentError ] if the purpose is invalid
227
+ def check_purpose! ( purpose )
228
+ return purpose unless purpose && !PURPOSES . include? ( purpose )
229
+
230
+ raise ArgumentError , "Invalid purpose: #{ purpose } "
251
231
end
252
232
end
253
233
end
0 commit comments