rdf 2.1.1 → 2.2.0.pre.rc1

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.
@@ -118,7 +118,7 @@ module RDF
118
118
  # @yield [repository]
119
119
  # @yieldparam [Repository]
120
120
  # @return [void]
121
- def self.load(urls, options = {}, &block)
121
+ def self.load(urls, **options, &block)
122
122
  self.new(options) do |repository|
123
123
  Array(urls).each do |url|
124
124
  repository.load(url, options)
@@ -371,7 +371,7 @@ module RDF
371
371
  #
372
372
  # @private
373
373
  # @see RDF::Queryable#query_pattern
374
- def query_pattern(pattern, options = {}, &block)
374
+ def query_pattern(pattern, **options, &block)
375
375
  snapshot = @data
376
376
  if block_given?
377
377
  graph_name = pattern.graph_name
@@ -88,7 +88,7 @@ module RDF
88
88
  # @yieldparam [RDF::Transaction] tx
89
89
  # @return [void]
90
90
  def self.begin(repository, mutable: false, **options, &block)
91
- self.new(repository, options.merge(mutable: mutable), &block)
91
+ self.new(repository, mutable: mutable, **options, &block)
92
92
  end
93
93
 
94
94
  ##
data/lib/rdf/util/file.rb CHANGED
@@ -30,8 +30,7 @@ module RDF; module Util
30
30
  # @option options [Array, String] :headers
31
31
  # HTTP Request headers
32
32
  # @return [Hash] A hash of HTTP request headers
33
- def self.headers options
34
- headers = options.fetch(:headers, {})
33
+ def self.headers headers: {}
35
34
  headers['Accept'] ||= default_accept_header
36
35
  headers
37
36
  end
@@ -45,19 +44,19 @@ module RDF; module Util
45
44
  ##
46
45
  # @abstract
47
46
  # @param [String] base_uri to open
47
+ # @param [String] proxy
48
+ # HTTP Proxy to use for requests.
49
+ # @param [Array, String] headers ({})
50
+ # HTTP Request headers
51
+ # @param [Boolean] verify_none (false)
52
+ # Don't verify SSL certificates
48
53
  # @param [Hash{Symbol => Object}] options
49
54
  # options are ignored in this implementation. Applications are encouraged
50
55
  # to override this implementation to provide more control over HTTP
51
56
  # headers and redirect following.
52
- # @option options [String] :proxy
53
- # HTTP Proxy to use for requests.
54
- # @option options [Array, String] :headers
55
- # HTTP Request headers
56
- # @option options [Boolean] :verify_none (false)
57
- # Don't verify SSL certificates
58
57
  # @return [RemoteDocument, Object] A {RemoteDocument}. If a block is given, the result of evaluating the block is returned.
59
58
  # @raise [IOError] if not found
60
- def self.open_url(base_uri, options)
59
+ def self.open_url(base_uri, proxy: nil, headers: {}, verify_none: false, **options)
61
60
  raise NoMethodError.new("#{self.inspect} does not implement required method `open_url` for ", "open_url")
62
61
  end
63
62
  end
@@ -69,18 +68,14 @@ module RDF; module Util
69
68
  # allowing the use of `Rack::Cache` to avoid network access.
70
69
  # @since 1.2
71
70
  class RestClientAdapter < HttpAdapter
72
- # @see HttpAdapter.open_url
73
- # @param [String] base_uri to open
74
- # @param [Hash{Symbol => Object}] options
75
- # @return [RemoteDocument, Object] A {RemoteDocument}. If a block is given, the result of evaluating the block is returned.
76
- # @raise [IOError] if not found
77
- def self.open_url(base_uri, options)
78
- ssl_verify = options[:verify_none] ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
71
+ # (see HttpAdapter.open_url)
72
+ def self.open_url(base_uri, proxy: nil, headers: {}, verify_none: false, **options)
73
+ ssl_verify = verify_none ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
79
74
 
80
75
  # If RestClient is loaded, prefer it
81
- RestClient.proxy = options[:proxy].to_s if options[:proxy]
76
+ RestClient.proxy = proxy.to_s if proxy
82
77
  client = RestClient::Resource.new(base_uri, verify_ssl: ssl_verify)
83
- client.get(headers(options)) do |response, request, res, &blk|
78
+ client.get(headers(headers: headers)) do |response, request, res, &blk|
84
79
  case response.code
85
80
  when 200..299
86
81
  # found object
@@ -109,18 +104,14 @@ module RDF; module Util
109
104
  # Net::HTTP adapter to retrieve resources without additional dependencies
110
105
  # @since 1.2
111
106
  class NetHttpAdapter < HttpAdapter
112
- # @see HttpAdapter.open_url
113
- # @param [String] base_uri to open
114
- # @param [Hash{Symbol => Object}] options
115
- # @return [RemoteDocument, Object] A {RemoteDocument}. If a block is given, the result of evaluating the block is returned.
116
- # @raise [IOError] if not found
117
- def self.open_url(base_uri, options)
118
- ssl_verify = options[:verify_none] ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
107
+ # (see HttpAdapter.open_url)
108
+ def self.open_url(base_uri, proxy: nil, headers: {}, verify_none: false, **options)
109
+ ssl_verify = verify_none ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
119
110
 
120
111
  redirect_count = 0
121
112
  max_redirects = 5
122
113
  parsed_url = ::URI.parse(base_uri)
123
- parsed_proxy = ::URI.parse(options[:proxy].to_s)
114
+ parsed_proxy = ::URI.parse(proxy.to_s)
124
115
  base_uri = parsed_url.to_s
125
116
  remote_document = nil
126
117
 
@@ -131,7 +122,7 @@ module RDF; module Util
131
122
  use_ssl: parsed_url.scheme == 'https',
132
123
  verify_mode: ssl_verify
133
124
  ) do |http|
134
- request = Net::HTTP::Get.new(parsed_url.request_uri, headers(options))
125
+ request = Net::HTTP::Get.new(parsed_url.request_uri, headers(headers: headers))
135
126
  http.request(request) do |response|
136
127
  case response
137
128
  when Net::HTTPSuccess
@@ -193,14 +184,11 @@ module RDF; module Util
193
184
  end
194
185
  end
195
186
 
196
- # @see HttpAdapter.open_url
197
- # @param [String] base_uri to open
198
- # @param [Hash{Symbol => Object}] options
199
- # @return [RemoteDocument, Object] A {RemoteDocument}.
200
- def self.open_url(base_uri, options)
187
+ # (see HttpAdapter.open_url)
188
+ def self.open_url(base_uri, proxy: nil, headers: {}, verify_none: false, **options)
201
189
  response = conn.get do |req|
202
190
  req.url base_uri
203
- headers(options).each do |k,v|
191
+ headers(headers: headers).each do |k,v|
204
192
  req.headers[k] = v
205
193
  end
206
194
  end
@@ -283,29 +271,34 @@ module RDF; module Util
283
271
  # # => Cached resource if current, otherwise returned resource
284
272
  #
285
273
  # @param [String] filename_or_url to open
274
+ # @param [String] proxy
275
+ # HTTP Proxy to use for requests.
276
+ # @param [Array, String] headers ({})
277
+ # HTTP Request headers
278
+ # @param [Boolean] verify_none (false)
279
+ # Don't verify SSL certificates
286
280
  # @param [Hash{Symbol => Object}] options
287
281
  # options are ignored in this implementation. Applications are encouraged
288
282
  # to override this implementation to provide more control over HTTP
289
283
  # headers and redirect following. If opening as a file,
290
284
  # options are passed to `Kernel.open`.
291
- # @option options [String] :proxy
292
- # HTTP Proxy to use for requests.
293
- # @option options [Array, String] :headers
294
- # HTTP Request headers, passed to Kernel.open.
295
- # @option options [Boolean] :verify_none (false)
296
- # Don't verify SSL certificates
297
285
  # @return [RemoteDocument, Object] A {RemoteDocument}. If a block is given, the result of evaluating the block is returned.
298
286
  # @yield [ RemoteDocument] A {RemoteDocument} for local files
299
287
  # @yieldreturn [Object] returned from open_file
300
288
  # @raise [IOError] if not found
301
- def self.open_file(filename_or_url, options = {}, &block)
289
+ def self.open_file(filename_or_url, proxy: nil, headers: {}, verify_none: false, **options, &block)
302
290
  filename_or_url = $1 if filename_or_url.to_s.match(/^file:(.*)$/)
303
291
  remote_document = nil
304
292
 
305
293
  if filename_or_url.to_s =~ /^https?/
306
294
  base_uri = filename_or_url.to_s
307
295
 
308
- remote_document = self.http_adapter(!!options[:use_net_http]).open_url(base_uri, options)
296
+ remote_document = self.http_adapter(!!options[:use_net_http]).
297
+ open_url(base_uri,
298
+ proxy: proxy,
299
+ headers: headers,
300
+ verify_none: verify_none,
301
+ **options)
309
302
  else
310
303
  # Fake content type based on found format
311
304
  format = RDF::Format.for(filename_or_url.to_s)
@@ -13,7 +13,7 @@ module RDF; module Util
13
13
  # @param [Hash{Symbol => Object}] options
14
14
  # @option options [Logger, #<<] :logger
15
15
  # @return [Logger, #write, #<<]
16
- def logger(options = {})
16
+ def logger(**options)
17
17
  logger = options.fetch(:logger, @logger)
18
18
  logger = @options[:logger] if logger.nil? && @options
19
19
  if logger.nil?
@@ -33,20 +33,20 @@ module RDF; module Util
33
33
  # @param [Hash{Symbol => Object}] options
34
34
  # @option options [Logger, #<<] :logger
35
35
  # @return [Hash{Symbol => Integer}]
36
- def log_statistics(options = {})
36
+ def log_statistics(**options)
37
37
  logger(options).log_statistics
38
38
  end
39
39
 
40
40
  ##
41
41
  # Used for fatal errors where processing cannot continue. If `logger` is not configured, it logs to `$stderr`.
42
42
  #
43
- # @overload log_fatal(*args, options = {}, &block)
43
+ # @overload log_fatal(*args, **options, &block)
44
44
  # @param [Array<String>] args
45
45
  # @param [Array<String>] args Messages
46
+ # @param [:fatal, :error, :warn, :info, :debug] level (:fatal)
46
47
  # @param [Hash{Symbol => Object}] options
47
48
  # @option options [Integer] :depth
48
49
  # Recursion depth for indenting output
49
- # @option options [:fatal, :error, :warn, :info, :debug] level (:<<)
50
50
  # @option options [Integer] :lineno associated with message
51
51
  # @option options [Logger, #<<] :logger
52
52
  # @option options [Class] :exception, (StandardError)
@@ -54,9 +54,8 @@ module RDF; module Util
54
54
  # @yieldreturn [String] added to message
55
55
  # @return [void]
56
56
  # @raise Raises the provided exception class using the first element from args as the message component.
57
- def log_fatal(*args, &block)
58
- options = args.last.is_a?(Hash) ? args.pop : {}
59
- logger_common(*args, "Called from #{Gem.location_of_caller.join(':')}", options.merge(level: :fatal), &block)
57
+ def log_fatal(*args, level: :fatal, **options, &block)
58
+ logger_common(*args, "Called from #{Gem.location_of_caller.join(':')}", level: level, **options, &block)
60
59
  raise options.fetch(:exception, StandardError), args.first
61
60
  end
62
61
 
@@ -65,9 +64,10 @@ module RDF; module Util
65
64
  #
66
65
  # As a side-effect of setting `@logger_in_error`, which will suppress further error messages until cleared using {#log_recover}.
67
66
  #
68
- # @overload log_error(*args, options = {}, &block)
67
+ # @overload log_error(*args, **options, &block)
69
68
  # @param [Array<String>] args
70
69
  # @param [Array<String>] args Messages
70
+ # @param [:fatal, :error, :warn, :info, :debug] level (:error)
71
71
  # @param [Hash{Symbol => Object}] options
72
72
  # @option options [Integer] :depth
73
73
  # Recursion depth for indenting output
@@ -79,12 +79,11 @@ module RDF; module Util
79
79
  # @yieldreturn [String] added to message
80
80
  # @return [void]
81
81
  # @raise Raises the provided exception class using the first element from args as the message component, if `:exception` option is provided.
82
- def log_error(*args, &block)
83
- options = args.last.is_a?(Hash) ? args.pop : {}
82
+ def log_error(*args, level: :error, **options, &block)
84
83
  logger = self.logger(options)
85
84
  return if logger.recovering
86
85
  logger.recovering = true
87
- logger_common(*args, options.merge(level: :error), &block)
86
+ logger_common(*args, level: level, **options, &block)
88
87
  raise options[:exception], args.first if options[:exception]
89
88
  end
90
89
 
@@ -92,16 +91,17 @@ module RDF; module Util
92
91
  # @param [Hash{Symbol => Object}] options
93
92
  # @option options [Logger, #<<] :logger
94
93
  # @return [Boolean]
95
- def log_recovering?(options = {})
94
+ def log_recovering?(**options)
96
95
  self.logger(options).recovering
97
96
  end
98
97
 
99
98
  ##
100
99
  # Warning message.
101
100
  #
102
- # @overload log_warn(*args, options = {}, &block)
101
+ # @overload log_warn(*args, **options, &block)
103
102
  # @param [Array<String>] args
104
103
  # @param [Array<String>] args Messages
104
+ # @param [:fatal, :error, :warn, :info, :debug] level (:warn)
105
105
  # @param [Hash{Symbol => Object}] options
106
106
  # @option options [Integer] :depth
107
107
  # Recursion depth for indenting output
@@ -110,9 +110,8 @@ module RDF; module Util
110
110
  # @option options [Logger, #<<] :logger
111
111
  # @yieldreturn [String] added to message
112
112
  # @return [void]
113
- def log_warn(*args, &block)
114
- options = args.last.is_a?(Hash) ? args.pop : {}
115
- logger_common(*args, options.merge(level: :warn), &block)
113
+ def log_warn(*args, level: :warn, **options, &block)
114
+ logger_common(*args, level: level, **options, &block)
116
115
  end
117
116
 
118
117
  ##
@@ -120,9 +119,10 @@ module RDF; module Util
120
119
  #
121
120
  # As a side-effect of clearing `@logger_in_error`.
122
121
  #
123
- # @overload log_recover(*args, options = {}, &block)
122
+ # @overload log_recover(*args, **options, &block)
124
123
  # @param [Array<String>] args
125
124
  # @param [Array<String>] args Messages
125
+ # @param [:fatal, :error, :warn, :info, :debug] level (:info)
126
126
  # @param [Hash{Symbol => Object}] options
127
127
  # @option options [Integer] :depth
128
128
  # Recursion depth for indenting output
@@ -130,20 +130,20 @@ module RDF; module Util
130
130
  # @option options [Logger, #<<] :logger
131
131
  # @yieldreturn [String] added to message
132
132
  # @return [void]
133
- def log_recover(*args, &block)
134
- options = args.last.is_a?(Hash) ? args.pop : {}
133
+ def log_recover(*args, level: :info, **options, &block)
135
134
  logger = self.logger(options)
136
135
  logger.recovering = false
137
136
  return if args.empty? && !block_given?
138
- logger_common(*args, options.merge(level: :info), &block)
137
+ logger_common(*args, level: level, **options, &block)
139
138
  end
140
139
 
141
140
  ##
142
141
  # Informational message.
143
142
  #
144
- # @overload log_info(*args, options = {}, &block)
143
+ # @overload log_info(*args, **options, &block)
145
144
  # @param [Array<String>] args
146
145
  # @param [Array<String>] args Messages
146
+ # @param [:fatal, :error, :warn, :info, :debug] level (:info)
147
147
  # @param [Hash{Symbol => Object}] options
148
148
  # @option options [Integer] :depth
149
149
  # Recursion depth for indenting output
@@ -151,17 +151,17 @@ module RDF; module Util
151
151
  # @option options [Logger, #<<] :logger
152
152
  # @yieldreturn [String] added to message
153
153
  # @return [void]
154
- def log_info(*args, &block)
155
- options = args.last.is_a?(Hash) ? args.pop : {}
156
- logger_common(*args, options.merge(level: :info), &block)
154
+ def log_info(*args, level: :info, **options, &block)
155
+ logger_common(*args, level: level, **options, &block)
157
156
  end
158
157
 
159
158
  ##
160
159
  # Debug message.
161
160
  #
162
- # @overload log_debug(*args, options = {}, &block)
161
+ # @overload log_debug(*args, **options, &block)
163
162
  # @param [Array<String>] args
164
163
  # @param [Array<String>] args Messages
164
+ # @param [:fatal, :error, :warn, :info, :debug] level (:debug)
165
165
  # @param [Hash{Symbol => Object}] options
166
166
  # @option options [Integer] :depth
167
167
  # Recursion depth for indenting output
@@ -169,9 +169,8 @@ module RDF; module Util
169
169
  # @option options [Logger, #<<] :logger
170
170
  # @yieldreturn [String] added to message
171
171
  # @return [void]
172
- def log_debug(*args, &block)
173
- options = args.last.is_a?(Hash) ? args.pop : {}
174
- logger_common(*args, options.merge(level: :debug), &block)
172
+ def log_debug(*args, level: :debug, **options, &block)
173
+ logger_common(*args, level: level, **options, &block)
175
174
  end
176
175
 
177
176
  ##
@@ -188,7 +187,7 @@ module RDF; module Util
188
187
  # @overload log_depth
189
188
  # # Return the current log depth
190
189
  # @return [Integer]
191
- def log_depth(options = {}, &block)
190
+ def log_depth(**options, &block)
192
191
  self.logger(options).log_depth(&block)
193
192
  end
194
193
 
@@ -200,6 +199,7 @@ module RDF; module Util
200
199
  #
201
200
  # @overload logger_common(args, options)
202
201
  # @param [Array<String>] args Messages
202
+ # @param [:fatal, :error, :warn, :info, :debug] level
203
203
  # @param [Hash{Symbol => Object}] options
204
204
  # @option options [Integer] :depth
205
205
  # Recursion depth for indenting output
@@ -208,11 +208,11 @@ module RDF; module Util
208
208
  # @option options [Logger, #<<] :logger
209
209
  # @yieldreturn [String] added to message
210
210
  # @return [void]
211
- def logger_common(*args)
212
- options = args.last.is_a?(Hash) ? args.pop : {}
213
- level = options[:level]
211
+ def logger_common(*args, level:, **options)
214
212
  logger = self.logger(options)
215
213
  logger.log_statistics[level] = logger.log_statistics[level].to_i + 1
214
+ # Some older code uses integer level numbers
215
+ level = [:debug, :info, :warn, :error, :fatal][level] if level.is_a?(Integer)
216
216
  return if logger.level > {fatal: 4, error: 3, warn: 2, info: 1, debug: 0}[level]
217
217
 
218
218
  depth = options.fetch(:depth, logger.log_depth)
@@ -234,8 +234,8 @@ module RDF; module Util
234
234
  ##
235
235
  # @overload log_depth(options, &block)
236
236
  # Increase depth around a method invocation
237
+ # @param [Integer] depth (1) recursion depth
237
238
  # @param [Hash{Symbol}] options (@options || {})
238
- # @option options [Integer] :depth (1) recursion depth
239
239
  # @option options [Logger, #<<] :logger
240
240
  # @yield
241
241
  # Yields with no arguments
@@ -245,16 +245,16 @@ module RDF; module Util
245
245
  # @overload log_depth
246
246
  # # Return the current log depth
247
247
  # @return [Integer]
248
- def log_depth(options = {})
248
+ def log_depth(depth: 1, **options)
249
249
  @log_depth ||= 0
250
250
  if block_given?
251
- @log_depth += options.fetch(:depth, 1)
251
+ @log_depth += depth
252
252
  yield
253
253
  else
254
254
  @log_depth
255
255
  end
256
256
  ensure
257
- @log_depth -= options.fetch(:depth, 1) if block_given?
257
+ @log_depth -= depth if block_given?
258
258
  end
259
259
 
260
260
  # Give Logger like behavior to non-logger objects
data/lib/rdf/util/uuid.rb CHANGED
@@ -13,16 +13,17 @@ module RDF; module Util
13
13
  # [UUID]: http://rubygems.org/gems/uuid
14
14
  # [UUIDTools]: http://rubygems.org/gems/uuidtools
15
15
  #
16
+ # @param [:default, :compact, :urn] format (:default)
16
17
  # @param [Hash{Symbol => Object}] options
17
18
  # any options to pass through to the underlying UUID library
18
19
  # @return [String] a UUID string
19
20
  # @raise [LoadError] if no UUID library is available
20
21
  # @see http://rubygems.org/gems/uuid
21
22
  # @see http://rubygems.org/gems/uuidtools
22
- def self.generate(options = {})
23
+ def self.generate(format: :default, **options)
23
24
  begin
24
25
  require 'uuid'
25
- ::UUID.generate(options[:format] || :default)
26
+ ::UUID.generate(format)
26
27
  rescue LoadError => e
27
28
  begin
28
29
  require 'uuidtools'
@@ -54,10 +54,10 @@ module RDF
54
54
  #
55
55
  # @param [IO, File] output
56
56
  # the output stream
57
+ # @param [RDF::URI] base_uri
58
+ # URI of this vocabulary
57
59
  # @param [Hash{Symbol => Object}] options = ({})
58
60
  # any additional options. See {RDF::Writer#initialize}
59
- # @option options [RDF::URI] :base_uri
60
- # URI of this vocabulary
61
61
  # @option options [String] :class_name
62
62
  # Class name for this vocabulary
63
63
  # @option options [String] :module_name ("RDF")
@@ -71,9 +71,9 @@ module RDF
71
71
  # @yield [writer] `self`
72
72
  # @yieldparam [RDF::Writer] writer
73
73
  # @yieldreturn [void]
74
- def initialize(output = $stdout, options = {}, &block)
75
- options.fetch(:base_uri) {raise ArgumentError, "base_uri option required"}
74
+ def initialize(output = $stdout, base_uri:, **options, &block)
76
75
  @graph = RDF::Repository.new
76
+ options.merge(base_uri: base_uri)
77
77
  super
78
78
  end
79
79