nuggets 1.4.0 → 1.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/ChangeLog +14 -0
- data/README +4 -4
- data/lib/nuggets/array/format.rb +1 -1
- data/lib/nuggets/array/histogram_mixin.rb +4 -4
- data/lib/nuggets/file/sub_mixin.rb +8 -8
- data/lib/nuggets/hash/in_order.rb +2 -2
- data/lib/nuggets/hash/nest_mixin.rb +5 -9
- data/lib/nuggets/hash/unroll_mixin.rb +4 -4
- data/lib/nuggets/io/modes.rb +15 -15
- data/lib/nuggets/io/redirect_mixin.rb +2 -4
- data/lib/nuggets/json/canonical.rb +5 -0
- data/lib/nuggets/{io/null_mixin.rb → json/canonical_mixin.rb} +31 -12
- data/lib/nuggets/json/multi.rb +5 -0
- data/lib/nuggets/json/multi_mixin.rb +71 -0
- data/lib/nuggets/log_parser/rails.rb +18 -18
- data/lib/nuggets/string/format.rb +5 -0
- data/lib/nuggets/string/format_mixin.rb +59 -0
- data/lib/nuggets/string/highlight.rb +5 -0
- data/lib/nuggets/string/highlight_mixin.rb +62 -0
- data/lib/nuggets/version.rb +2 -2
- data/spec/nuggets/array/format_spec.rb +2 -1
- data/spec/nuggets/enumerable/minmax_spec.rb +2 -2
- data/spec/nuggets/env/set_spec.rb +2 -2
- data/spec/nuggets/env/user_home_spec.rb +1 -21
- data/spec/nuggets/hash/at_spec.rb +1 -1
- data/spec/nuggets/hash/deep_fetch_spec.rb +1 -1
- data/spec/nuggets/hash/deep_merge_spec.rb +16 -16
- data/spec/nuggets/hash/seen_spec.rb +1 -1
- data/spec/nuggets/hash/unroll_spec.rb +6 -18
- data/spec/nuggets/json/canonical_spec.rb +74 -0
- data/spec/nuggets/json/multi_spec.rb +67 -0
- data/spec/nuggets/object/blank_spec.rb +2 -2
- data/spec/nuggets/object/singleton_class_spec.rb +3 -2
- data/spec/nuggets/string/format_spec.rb +85 -0
- data/spec/nuggets/string/highlight_spec.rb +47 -0
- metadata +25 -16
- data/lib/nuggets/io/null.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1918bdc23725b5833549da651c379edda6a6e070753571e0037e7a7147be67ca
|
4
|
+
data.tar.gz: e48e2395d1366f14bfba167be73e36a4f2050ea64419918eca3b1f095d0fb37c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6719bf7667073a9bb9eb8c6f667a1715ebce7dff48b4cc4d514dbfbb6a245fafbfe3101a4f6426cb96e9820b072c8431d5b88c8f51a8201c143166eb65be4daf
|
7
|
+
data.tar.gz: de3a4dad5dcd6e5d238c92ea95ee4d4964c79c10df4b634c0b533e2e7f93e7107516c3d147c856dccb40f556911791b11b515c4ce427c6dd8f256036b5bd7c91
|
data/ChangeLog
CHANGED
@@ -2,6 +2,20 @@
|
|
2
2
|
|
3
3
|
= Revision history for nuggets
|
4
4
|
|
5
|
+
== 1.6.1 [2022-05-18]
|
6
|
+
|
7
|
+
* Ruby 3.1 compatibility.
|
8
|
+
|
9
|
+
== 1.6.0 [2018-07-12]
|
10
|
+
|
11
|
+
* Added <tt>JSON.*_{multi,canonical}</tt>.
|
12
|
+
|
13
|
+
== 1.5.0 [2016-04-19]
|
14
|
+
|
15
|
+
* Added String#format.
|
16
|
+
* Added String#highlight.
|
17
|
+
* Dropped IO::NULL, available as File::NULL since Ruby 1.9.3.
|
18
|
+
|
5
19
|
== 1.4.0 [2015-10-02]
|
6
20
|
|
7
21
|
* Added File.open_file.
|
data/README
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
== VERSION
|
4
4
|
|
5
|
-
This documentation refers to nuggets version 1.
|
5
|
+
This documentation refers to nuggets version 1.6.1.
|
6
6
|
|
7
7
|
|
8
8
|
== DESCRIPTION
|
9
9
|
|
10
10
|
Nuggets provides a collection of extensions to Ruby classes.
|
11
|
-
It's similar to projects like Facets[
|
11
|
+
It's similar to projects like Facets[https://rubyworks.github.io/facets]
|
12
12
|
and Extensions[http://extensions.rubyforge.org] (or even
|
13
13
|
Labrador[http://labrador.rubyforge.org]).
|
14
14
|
|
@@ -45,7 +45,7 @@ expected or cause other libraries to misbehave. Use at your own risk!
|
|
45
45
|
|
46
46
|
== LINKS
|
47
47
|
|
48
|
-
Documentation:: https://blackwinter.github.
|
48
|
+
Documentation:: https://blackwinter.github.io/nuggets
|
49
49
|
Source code:: https://github.com/blackwinter/nuggets
|
50
50
|
RubyGem:: https://rubygems.org/gems/nuggets
|
51
51
|
Travis CI:: https://travis-ci.org/blackwinter/nuggets
|
@@ -58,7 +58,7 @@ Travis CI:: https://travis-ci.org/blackwinter/nuggets
|
|
58
58
|
|
59
59
|
== LICENSE AND COPYRIGHT
|
60
60
|
|
61
|
-
Copyright (C) 2007-
|
61
|
+
Copyright (C) 2007-2022 Jens Wille
|
62
62
|
|
63
63
|
nuggets is free software: you can redistribute it and/or modify it
|
64
64
|
under the terms of the GNU Affero General Public License as published by
|
data/lib/nuggets/array/format.rb
CHANGED
@@ -40,7 +40,7 @@ class Array
|
|
40
40
|
# Applies to string argument accordingly: First string in _array_ applied to
|
41
41
|
# _str_; empty string if _str_ is empty.
|
42
42
|
def %(args)
|
43
|
-
opts = { :
|
43
|
+
opts = { sep: ', ' }
|
44
44
|
opts.update(pop) if last.is_a?(::Hash)
|
45
45
|
|
46
46
|
default = lambda { |n| ['%s'] * n * opts[:sep] }
|
@@ -39,10 +39,10 @@ module Nuggets
|
|
39
39
|
#
|
40
40
|
# The "numeric" variants format the item as a (decimal) number.
|
41
41
|
FORMATS = {
|
42
|
-
:
|
43
|
-
:
|
44
|
-
:
|
45
|
-
:
|
42
|
+
default: '%-*s [%s]%*s %*d',
|
43
|
+
percent: '%-*s [%s]%*s %*d (%.2f%%)',
|
44
|
+
numeric: '%*d [%s]%*s %*d',
|
45
|
+
numeric_percent: '%*d [%s]%*s %*d (%.2f%%)'
|
46
46
|
}
|
47
47
|
|
48
48
|
# Encapsulates a #histogram item and provides the following attributes (see
|
@@ -39,9 +39,9 @@ module Nuggets
|
|
39
39
|
#
|
40
40
|
# Calls String#sub! on file +name+'s contents with +args+ and (optional)
|
41
41
|
# +block+ and returns the new content.
|
42
|
-
def sub(name, *args)
|
42
|
+
def sub(name, *args, &block)
|
43
43
|
content = read(name)
|
44
|
-
content.sub!(*args, &
|
44
|
+
content.sub!(*args, &block)
|
45
45
|
content
|
46
46
|
end
|
47
47
|
|
@@ -51,11 +51,11 @@ module Nuggets
|
|
51
51
|
# Calls String#sub! on file +name+'s contents with +args+ and (optional)
|
52
52
|
# +block+ and replaces the file with the new content. Returns the result
|
53
53
|
# of the String#sub! call.
|
54
|
-
def sub!(name, *args)
|
54
|
+
def sub!(name, *args, &block)
|
55
55
|
res = nil
|
56
56
|
|
57
57
|
replace(name) { |content|
|
58
|
-
res = content.sub!(*args, &
|
58
|
+
res = content.sub!(*args, &block)
|
59
59
|
content
|
60
60
|
}
|
61
61
|
|
@@ -67,9 +67,9 @@ module Nuggets
|
|
67
67
|
#
|
68
68
|
# Calls String#gsub! on file +name+'s contents with +args+ and (optional)
|
69
69
|
# +block+ and returns the new content.
|
70
|
-
def gsub(name, *args)
|
70
|
+
def gsub(name, *args, &block)
|
71
71
|
content = read(name)
|
72
|
-
content.gsub!(*args, &
|
72
|
+
content.gsub!(*args, &block)
|
73
73
|
content
|
74
74
|
end
|
75
75
|
|
@@ -79,11 +79,11 @@ module Nuggets
|
|
79
79
|
# Calls String#gsub! on file +name+'s contents with +args+ and (optional)
|
80
80
|
# +block+ and replaces the file with the new content. Returns the result
|
81
81
|
# of the String#gsub! call.
|
82
|
-
def gsub!(name, *args)
|
82
|
+
def gsub!(name, *args, &block)
|
83
83
|
res = nil
|
84
84
|
|
85
85
|
replace(name) { |content|
|
86
|
-
res = content.gsub!(*args, &
|
86
|
+
res = content.gsub!(*args, &block)
|
87
87
|
content
|
88
88
|
}
|
89
89
|
|
@@ -34,8 +34,8 @@ class Hash
|
|
34
34
|
# Returns <tt>hash#to_a</tt>, in forced order (cf. Array#in_order).
|
35
35
|
#
|
36
36
|
# Examples:
|
37
|
-
# { :
|
38
|
-
# { :
|
37
|
+
# { a: 1, b: 2, c: 3 }.in_order(:b, :c) #=> [[:b, 2], [:c, 3], [:a, 1]]
|
38
|
+
# { a: 1, b: 2, c: 3 }.in_order(:b, :d) #=> [[:b, 2], [:a, 1], [:c, 3]]
|
39
39
|
def in_order(*ordered)
|
40
40
|
keys.in_order(*ordered).map { |key| [key, self[key]] }
|
41
41
|
end
|
@@ -45,15 +45,15 @@ module Nuggets
|
|
45
45
|
# Example:
|
46
46
|
#
|
47
47
|
# hash = Hash.nest(2)
|
48
|
-
# hash[:foo][:bar][:a] = { :
|
49
|
-
# hash[:foo][:bar][:b] = { :
|
48
|
+
# hash[:foo][:bar][:a] = { x: 1, y: 2 }
|
49
|
+
# hash[:foo][:bar][:b] = { x: 0, y: 3 }
|
50
50
|
# hash
|
51
51
|
# #=> {:foo=>{:bar=>{:b=>{:y=>3, :x=>0}, :a=>{:y=>2, :x=>1}}}}
|
52
|
-
def nest(depth = 0, value = default = true)
|
52
|
+
def nest(depth = 0, value = default = true, &block)
|
53
53
|
if depth.zero?
|
54
54
|
if default
|
55
55
|
if block_given?
|
56
|
-
new { |hash, key| hash[key] =
|
56
|
+
new { |hash, key| hash[key] = block[key] }
|
57
57
|
else
|
58
58
|
new { |hash, key| hash[key] = hash.default }
|
59
59
|
end
|
@@ -62,11 +62,7 @@ module Nuggets
|
|
62
62
|
end
|
63
63
|
else
|
64
64
|
if default
|
65
|
-
|
66
|
-
new { |hash, key| hash[key] = nest(depth - 1, &::Proc.new) }
|
67
|
-
else
|
68
|
-
new { |hash, key| hash[key] = nest(depth - 1) }
|
69
|
-
end
|
65
|
+
new { |hash, key| hash[key] = nest(depth - 1, &block) }
|
70
66
|
else
|
71
67
|
new { |hash, key| hash[key] = nest(depth - 1, value) }
|
72
68
|
end
|
@@ -30,7 +30,7 @@ module Nuggets
|
|
30
30
|
|
31
31
|
# call-seq:
|
32
32
|
# hash.unroll(*value_keys) => anArray
|
33
|
-
# hash.unroll(*value_keys, :
|
33
|
+
# hash.unroll(*value_keys, sort_by: ...) => anArray
|
34
34
|
# hash.unroll(*value_keys) { |value_hash| ... } => anArray
|
35
35
|
#
|
36
36
|
# "Unrolls" a nested hash, so that each path through _hash_ results in a
|
@@ -46,13 +46,13 @@ module Nuggets
|
|
46
46
|
#
|
47
47
|
# Examples:
|
48
48
|
#
|
49
|
-
# { :
|
49
|
+
# { foo: { bar: { a: { x: 1, y: 2 }, b: { x: 0, y: 3 } } } }.unroll
|
50
50
|
# #=> [[:foo, :bar, :b, 3, 0], [:foo, :bar, :a, 2, 1]]
|
51
51
|
#
|
52
|
-
# { :
|
52
|
+
# { foo: { bar: { a: { x: 1, y: 2 }, b: { x: 0, y: 3 } } } }.unroll(sort_by: :to_s)
|
53
53
|
# #=> [[:foo, :bar, :a, 1, 2], [:foo, :bar, :b, 0, 3]]
|
54
54
|
#
|
55
|
-
# { :
|
55
|
+
# { foo: { bar: { a: { x: 1, y: 2 }, b: { x: 0, y: 3 } } } }.unroll { |data| data[:x] = nil; data[:y] *= 2 }
|
56
56
|
# #=> [[:foo, :bar, :b, 6, nil], [:foo, :bar, :a, 4, nil]]
|
57
57
|
def unroll(*value_keys, &block)
|
58
58
|
args = value_keys.dup
|
data/lib/nuggets/io/modes.rb
CHANGED
@@ -38,8 +38,8 @@ class IO
|
|
38
38
|
#
|
39
39
|
# Opens +name+ with mode +r+. NOTE: With no associated block,
|
40
40
|
# acts like the original IO::read, not like IO::new.
|
41
|
-
def read(name, *args)
|
42
|
-
return _nuggets_original_read(name, *args) unless block_given?
|
41
|
+
def read(name, *args, **opts, &block)
|
42
|
+
return _nuggets_original_read(name, *args, **opts) unless block_given?
|
43
43
|
|
44
44
|
case args.size
|
45
45
|
when 0
|
@@ -55,7 +55,7 @@ class IO
|
|
55
55
|
raise ::ArgumentError, "wrong number of arguments (#{args.size + 1} for 1-2)"
|
56
56
|
end
|
57
57
|
|
58
|
-
open_with_mode(name, 'r', binary,
|
58
|
+
open_with_mode(name, 'r', binary, &block)
|
59
59
|
end
|
60
60
|
|
61
61
|
# call-seq:
|
@@ -63,8 +63,8 @@ class IO
|
|
63
63
|
# IO.write(name[, binary]) { |io| ... } => anObject
|
64
64
|
#
|
65
65
|
# Opens +name+ with mode +w+.
|
66
|
-
def write(name, binary = false)
|
67
|
-
open_with_mode(name, 'w', binary, &
|
66
|
+
def write(name, binary = false, &block)
|
67
|
+
open_with_mode(name, 'w', binary, &block)
|
68
68
|
end
|
69
69
|
|
70
70
|
# call-seq:
|
@@ -72,8 +72,8 @@ class IO
|
|
72
72
|
# IO.append(name[, binary]) { |io| ... } => anObject
|
73
73
|
#
|
74
74
|
# Opens +name+ with mode +a+.
|
75
|
-
def append(name, binary = false)
|
76
|
-
open_with_mode(name, 'a', binary, &
|
75
|
+
def append(name, binary = false, &block)
|
76
|
+
open_with_mode(name, 'a', binary, &block)
|
77
77
|
end
|
78
78
|
|
79
79
|
# call-seq:
|
@@ -81,8 +81,8 @@ class IO
|
|
81
81
|
# IO.read_write(name[, binary]) { |io| ... } => anObject
|
82
82
|
#
|
83
83
|
# Opens +name+ with mode <tt>r+</tt>.
|
84
|
-
def read_write(name, binary = false)
|
85
|
-
open_with_mode(name, 'r+', binary, &
|
84
|
+
def read_write(name, binary = false, &block)
|
85
|
+
open_with_mode(name, 'r+', binary, &block)
|
86
86
|
end
|
87
87
|
|
88
88
|
# call-seq:
|
@@ -90,8 +90,8 @@ class IO
|
|
90
90
|
# IO.write_read(name[, binary]) { |io| ... } => anObject
|
91
91
|
#
|
92
92
|
# Opens +name+ with mode <tt>w+</tt>.
|
93
|
-
def write_read(name, binary = false)
|
94
|
-
open_with_mode(name, 'w+', binary, &
|
93
|
+
def write_read(name, binary = false, &block)
|
94
|
+
open_with_mode(name, 'w+', binary, &block)
|
95
95
|
end
|
96
96
|
|
97
97
|
# call-seq:
|
@@ -99,15 +99,15 @@ class IO
|
|
99
99
|
# IO.append_read(name[, binary]) { |io| ... } => anObject
|
100
100
|
#
|
101
101
|
# Opens +name+ with mode <tt>a+</tt>.
|
102
|
-
def append_read(name, binary = false)
|
103
|
-
open_with_mode(name, 'a+', binary, &
|
102
|
+
def append_read(name, binary = false, &block)
|
103
|
+
open_with_mode(name, 'a+', binary, &block)
|
104
104
|
end
|
105
105
|
|
106
106
|
private
|
107
107
|
|
108
108
|
# Just a helper to DRY things up.
|
109
|
-
def open_with_mode(name, mode, binary = false)
|
110
|
-
open(name, "#{mode}#{'b' if binary}", &
|
109
|
+
def open_with_mode(name, mode, binary = false, &block)
|
110
|
+
open(name, "#{mode}#{'b' if binary}", &block)
|
111
111
|
end
|
112
112
|
|
113
113
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
# #
|
4
4
|
# nuggets -- Extending Ruby #
|
5
5
|
# #
|
6
|
-
# Copyright (C) 2007-
|
6
|
+
# Copyright (C) 2007-2016 Jens Wille #
|
7
7
|
# #
|
8
8
|
# Authors: #
|
9
9
|
# Jens Wille <jens.wille@gmail.com> #
|
@@ -24,13 +24,11 @@
|
|
24
24
|
###############################################################################
|
25
25
|
#++
|
26
26
|
|
27
|
-
require 'nuggets/io/null_mixin'
|
28
|
-
|
29
27
|
module Nuggets
|
30
28
|
class IO
|
31
29
|
module RedirectMixin
|
32
30
|
|
33
|
-
def redirect(target =
|
31
|
+
def redirect(target = ::File::NULL, mode = 'w')
|
34
32
|
unless target.is_a?(self.class)
|
35
33
|
target = ::File.open(target, mode)
|
36
34
|
close_target = true
|
@@ -3,7 +3,7 @@
|
|
3
3
|
# #
|
4
4
|
# nuggets -- Extending Ruby #
|
5
5
|
# #
|
6
|
-
# Copyright (C) 2007-
|
6
|
+
# Copyright (C) 2007-2018 Jens Wille #
|
7
7
|
# #
|
8
8
|
# Authors: #
|
9
9
|
# Jens Wille <jens.wille@gmail.com> #
|
@@ -24,18 +24,37 @@
|
|
24
24
|
###############################################################################
|
25
25
|
#++
|
26
26
|
|
27
|
-
require 'nuggets/ruby_mixin'
|
28
|
-
|
29
27
|
module Nuggets
|
30
|
-
|
31
|
-
module
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
28
|
+
module JSON
|
29
|
+
module CanonicalMixin
|
30
|
+
|
31
|
+
def generate_canonical(obj, opts = {})
|
32
|
+
generate(_nuggets_json_canonical(obj), opts)
|
33
|
+
end
|
34
|
+
|
35
|
+
def pretty_generate_canonical(obj, opts = {})
|
36
|
+
pretty_generate(_nuggets_json_canonical(obj), opts)
|
37
|
+
end
|
38
|
+
|
39
|
+
def pretty_print_canonical(source, opts = {})
|
40
|
+
pretty_generate_canonical(parse_multi(source, opts), opts)
|
41
|
+
end
|
42
|
+
|
43
|
+
alias_method :pc, :pretty_print_canonical
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def _nuggets_json_canonical(obj)
|
48
|
+
case obj
|
49
|
+
when ::Hash
|
50
|
+
obj.class.new.tap { |res|
|
51
|
+
obj.keys.sort.each { |k| res[k] = send(__method__, obj[k]) } }
|
52
|
+
when ::Array
|
53
|
+
obj.map { |v| send(__method__, v) }.sort_by(&:to_s)
|
54
|
+
else
|
55
|
+
obj
|
56
|
+
end
|
57
|
+
end
|
39
58
|
|
40
59
|
end
|
41
60
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2018 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@gmail.com> #
|
10
|
+
# #
|
11
|
+
# nuggets is free software; you can redistribute it and/or modify it under #
|
12
|
+
# the terms of the GNU Affero General Public License as published by the Free #
|
13
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
14
|
+
# any later version. #
|
15
|
+
# #
|
16
|
+
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
|
17
|
+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
18
|
+
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
|
19
|
+
# more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
module Nuggets
|
28
|
+
module JSON
|
29
|
+
module MultiMixin
|
30
|
+
|
31
|
+
# Preserves multiple (non-unique) names in
|
32
|
+
# {"non-interoperable"}[https://tools.ietf.org/html/rfc7159#section-4] JSON objects.
|
33
|
+
def parse_multi(source, opts = {})
|
34
|
+
parse(source, opts.merge(object_class: MULTI_OBJECT))
|
35
|
+
end
|
36
|
+
|
37
|
+
def pretty_print_multi(source, opts = {})
|
38
|
+
pretty_generate(parse_multi(source, opts), opts)
|
39
|
+
end
|
40
|
+
|
41
|
+
alias_method :pp, :pretty_print_multi
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
class MULTI_OBJECT < ::Hash
|
46
|
+
|
47
|
+
def []=(k, v)
|
48
|
+
super(MULTI_KEY.new(k), v)
|
49
|
+
end
|
50
|
+
|
51
|
+
def each_multi(k)
|
52
|
+
block_given? ? each { |l, v| yield v if k == l } : enum_for(__method__, k)
|
53
|
+
end
|
54
|
+
|
55
|
+
def fetch_multi(k)
|
56
|
+
each_multi(k).to_a
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
class MULTI_KEY < ::String
|
62
|
+
|
63
|
+
def eql?(*)
|
64
|
+
false
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -54,7 +54,7 @@ module Nuggets
|
|
54
54
|
# Log line patterns
|
55
55
|
ITEMS = [
|
56
56
|
[:processing, {
|
57
|
-
:
|
57
|
+
re: %r{
|
58
58
|
#{PREFIX_RE} # pid, host
|
59
59
|
Processing\s+
|
60
60
|
(\w+) # controller
|
@@ -72,23 +72,23 @@ module Nuggets
|
|
72
72
|
(\w+) # request_method
|
73
73
|
\]
|
74
74
|
}xo,
|
75
|
-
:
|
75
|
+
keys: [:controller, :action, :ip, :datetime, :request_method]
|
76
76
|
}],
|
77
77
|
[:session_id, {
|
78
|
-
:
|
78
|
+
re: %r{
|
79
79
|
#{PREFIX_RE} # pid, host
|
80
80
|
Session\sID:\s+
|
81
81
|
(\S+) # sid
|
82
82
|
}xo,
|
83
|
-
:
|
83
|
+
keys: [:sid]
|
84
84
|
}],
|
85
85
|
[:parameters, {
|
86
|
-
:
|
86
|
+
re: %r{
|
87
87
|
#{PREFIX_RE} # pid, host
|
88
88
|
Parameters:\s+
|
89
89
|
(\{.*\}) # params
|
90
90
|
}xo, #}
|
91
|
-
:
|
91
|
+
proc: lambda { |entry, md|
|
92
92
|
entry[:params_hash] = md[3].hash
|
93
93
|
entry[:params] = begin
|
94
94
|
eval("$SAFE = 3\n#{md[3].gsub(/#<.*?>/, '%q{\&}')}", nil, __FILE__, __LINE__) # !!!
|
@@ -98,7 +98,7 @@ module Nuggets
|
|
98
98
|
}
|
99
99
|
}],
|
100
100
|
[:client_info, {
|
101
|
-
:
|
101
|
+
re: %r{
|
102
102
|
#{PREFIX_RE} # pid, host
|
103
103
|
Client\sinfo:\s+
|
104
104
|
UA\s+=\s+
|
@@ -107,18 +107,18 @@ module Nuggets
|
|
107
107
|
LANG\s+=\s+
|
108
108
|
(.*) # accept_language
|
109
109
|
}xo,
|
110
|
-
:
|
110
|
+
keys: [:user_agent, :accept_language]
|
111
111
|
}],
|
112
112
|
[:referer, {
|
113
|
-
:
|
113
|
+
re: %r{
|
114
114
|
#{PREFIX_RE} # pid, host
|
115
115
|
Referer:\s+
|
116
116
|
(.*) # referer
|
117
117
|
}xo,
|
118
|
-
:
|
118
|
+
keys: [:referer]
|
119
119
|
}],
|
120
120
|
[:meta, {
|
121
|
-
:
|
121
|
+
re: %r{
|
122
122
|
#{PREFIX_RE} # pid, host
|
123
123
|
Meta:\s+
|
124
124
|
User\s+=\s+
|
@@ -127,20 +127,20 @@ module Nuggets
|
|
127
127
|
Institution\s+=\s+
|
128
128
|
(.*) # institution_id
|
129
129
|
}xo,
|
130
|
-
:
|
130
|
+
keys: [:user_id, :institution_id]
|
131
131
|
}],
|
132
132
|
[:stats, {
|
133
|
-
:
|
133
|
+
re: %r{
|
134
134
|
#{PREFIX_RE} # pid, host
|
135
135
|
Stats:\s+
|
136
136
|
(.*) # flags
|
137
137
|
}xo,
|
138
|
-
:
|
138
|
+
proc: lambda { |entry, md|
|
139
139
|
entry[:flags] = md[3].split(SEPARATOR_RE)
|
140
140
|
}
|
141
141
|
}],
|
142
142
|
[:oauth, {
|
143
|
-
:
|
143
|
+
re: %r{
|
144
144
|
#{PREFIX_RE} # pid, host
|
145
145
|
OAuth:\s+
|
146
146
|
Token\s+=\s+
|
@@ -152,10 +152,10 @@ module Nuggets
|
|
152
152
|
Client\s+=\s+
|
153
153
|
(.*) # client_id
|
154
154
|
}xo,
|
155
|
-
:
|
155
|
+
keys: [:token_type, :token, :user_id, :client_id]
|
156
156
|
}],
|
157
157
|
[:benchmark, {
|
158
|
-
:
|
158
|
+
re: %r{
|
159
159
|
#{PREFIX_RE} # pid, host
|
160
160
|
Completed\sin\s+
|
161
161
|
(\S+) # runtime
|
@@ -185,7 +185,7 @@ module Nuggets
|
|
185
185
|
(.*) # request_uri
|
186
186
|
\]
|
187
187
|
}xo,
|
188
|
-
:
|
188
|
+
keys: [:runtime, :rendering_runtime, :db_runtime, :status, :request_uri]
|
189
189
|
}]
|
190
190
|
]
|
191
191
|
|
@@ -0,0 +1,59 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2016 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@gmail.com> #
|
10
|
+
# #
|
11
|
+
# nuggets is free software; you can redistribute it and/or modify it under #
|
12
|
+
# the terms of the GNU Affero General Public License as published by the Free #
|
13
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
14
|
+
# any later version. #
|
15
|
+
# #
|
16
|
+
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
|
17
|
+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
18
|
+
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
|
19
|
+
# more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
module Nuggets
|
28
|
+
class String
|
29
|
+
module FormatMixin
|
30
|
+
|
31
|
+
PREFIX = '%'
|
32
|
+
|
33
|
+
FORMAT_RE = %r{#{PREFIX}(?:\{(.*?)\}|(.))}o
|
34
|
+
|
35
|
+
# call-seq:
|
36
|
+
# str.format(hash) => aString
|
37
|
+
# str.format { |directive| ... } => aString
|
38
|
+
#
|
39
|
+
# Replace format directives in _str_.
|
40
|
+
#
|
41
|
+
# If +hash+ given, uses value of directive key (String or Symbol)
|
42
|
+
# as replacement value. Raises KeyError if directive key not found.
|
43
|
+
#
|
44
|
+
# If block given, uses result of +directive+ yielded to block as
|
45
|
+
# replacement value. Raises ArgumentError if block returns no value.
|
46
|
+
def format(hash = nil)
|
47
|
+
replace = lambda { |&b| gsub(FORMAT_RE) {
|
48
|
+
(k = $1 || $2) == PREFIX ? k : b[k] } }
|
49
|
+
|
50
|
+
hash ? replace.() { |k| hash.fetch(k) {
|
51
|
+
hash.fetch(k.to_sym) { raise(KeyError, "key not found: #{k}") } } } :
|
52
|
+
block_given? ? replace.() { |k| yield k or
|
53
|
+
raise(ArgumentError, "malformed format string - #{$&}") } :
|
54
|
+
raise(ArgumentError, 'wrong number of arguments (given 0, expected 1)')
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|