ruby-nuggets 0.9.9 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- metadata +20 -324
- data/COPYING +0 -663
- data/ChangeLog +0 -7
- data/README +0 -62
- data/Rakefile +0 -47
- data/lib/nuggets.rb +0 -73
- data/lib/nuggets/all.rb +0 -36
- data/lib/nuggets/all_mixins.rb +0 -34
- data/lib/nuggets/ansicolor2css.rb +0 -125
- data/lib/nuggets/argv/option.rb +0 -3
- data/lib/nuggets/argv/option_mixin.rb +0 -85
- data/lib/nuggets/array/boost.rb +0 -5
- data/lib/nuggets/array/boost_mixin.rb +0 -78
- data/lib/nuggets/array/combination.rb +0 -63
- data/lib/nuggets/array/correlation.rb +0 -5
- data/lib/nuggets/array/correlation_mixin.rb +0 -64
- data/lib/nuggets/array/flatten_once.rb +0 -57
- data/lib/nuggets/array/flush.rb +0 -5
- data/lib/nuggets/array/flush_mixin.rb +0 -47
- data/lib/nuggets/array/format.rb +0 -66
- data/lib/nuggets/array/hashify.rb +0 -5
- data/lib/nuggets/array/hashify_mixin.rb +0 -48
- data/lib/nuggets/array/histogram.rb +0 -5
- data/lib/nuggets/array/histogram_mixin.rb +0 -170
- data/lib/nuggets/array/in_order.rb +0 -51
- data/lib/nuggets/array/limit.rb +0 -5
- data/lib/nuggets/array/limit_mixin.rb +0 -57
- data/lib/nuggets/array/mean.rb +0 -5
- data/lib/nuggets/array/mean_mixin.rb +0 -203
- data/lib/nuggets/array/median.rb +0 -5
- data/lib/nuggets/array/median_mixin.rb +0 -74
- data/lib/nuggets/array/mode.rb +0 -5
- data/lib/nuggets/array/mode_mixin.rb +0 -70
- data/lib/nuggets/array/monotone.rb +0 -84
- data/lib/nuggets/array/only.rb +0 -42
- data/lib/nuggets/array/rand.rb +0 -45
- data/lib/nuggets/array/regression.rb +0 -5
- data/lib/nuggets/array/regression_mixin.rb +0 -150
- data/lib/nuggets/array/runiq.rb +0 -5
- data/lib/nuggets/array/runiq_mixin.rb +0 -53
- data/lib/nuggets/array/shuffle.rb +0 -133
- data/lib/nuggets/array/standard_deviation.rb +0 -5
- data/lib/nuggets/array/standard_deviation_mixin.rb +0 -51
- data/lib/nuggets/array/to_hash.rb +0 -65
- data/lib/nuggets/array/variance.rb +0 -5
- data/lib/nuggets/array/variance_mixin.rb +0 -82
- data/lib/nuggets/cli.rb +0 -8
- data/lib/nuggets/content_type.rb +0 -98
- data/lib/nuggets/dotted_decimal.rb +0 -60
- data/lib/nuggets/enumerable/agrep.rb +0 -80
- data/lib/nuggets/enumerable/all_any_extended.rb +0 -67
- data/lib/nuggets/enumerable/minmax.rb +0 -103
- data/lib/nuggets/env/set.rb +0 -3
- data/lib/nuggets/env/set_mixin.rb +0 -68
- data/lib/nuggets/env/user_encoding.rb +0 -3
- data/lib/nuggets/env/user_encoding_mixin.rb +0 -55
- data/lib/nuggets/env/user_home.rb +0 -3
- data/lib/nuggets/env/user_home_mixin.rb +0 -57
- data/lib/nuggets/file/ext.rb +0 -5
- data/lib/nuggets/file/ext_mixin.rb +0 -88
- data/lib/nuggets/file/replace.rb +0 -5
- data/lib/nuggets/file/replace_mixin.rb +0 -56
- data/lib/nuggets/file/sub.rb +0 -5
- data/lib/nuggets/file/sub_mixin.rb +0 -96
- data/lib/nuggets/file/which.rb +0 -5
- data/lib/nuggets/file/which_mixin.rb +0 -73
- data/lib/nuggets/hash/at.rb +0 -69
- data/lib/nuggets/hash/deep_fetch.rb +0 -5
- data/lib/nuggets/hash/deep_fetch_mixin.rb +0 -75
- data/lib/nuggets/hash/deep_merge.rb +0 -5
- data/lib/nuggets/hash/deep_merge_mixin.rb +0 -54
- data/lib/nuggets/hash/idmap.rb +0 -5
- data/lib/nuggets/hash/idmap_mixin.rb +0 -42
- data/lib/nuggets/hash/in_order.rb +0 -44
- data/lib/nuggets/hash/insert.rb +0 -54
- data/lib/nuggets/hash/nest.rb +0 -5
- data/lib/nuggets/hash/nest_mixin.rb +0 -79
- data/lib/nuggets/hash/only.rb +0 -53
- data/lib/nuggets/hash/seen.rb +0 -5
- data/lib/nuggets/hash/seen_mixin.rb +0 -59
- data/lib/nuggets/hash/unroll.rb +0 -5
- data/lib/nuggets/hash/unroll_mixin.rb +0 -89
- data/lib/nuggets/hash/zip.rb +0 -5
- data/lib/nuggets/hash/zip_mixin.rb +0 -160
- data/lib/nuggets/i18n.rb +0 -156
- data/lib/nuggets/integer/factorial.rb +0 -57
- data/lib/nuggets/integer/length.rb +0 -5
- data/lib/nuggets/integer/length_mixin.rb +0 -50
- data/lib/nuggets/integer/map.rb +0 -5
- data/lib/nuggets/integer/map_mixin.rb +0 -43
- data/lib/nuggets/integer/to_binary_s.rb +0 -39
- data/lib/nuggets/io/agrep.rb +0 -44
- data/lib/nuggets/io/interact.rb +0 -5
- data/lib/nuggets/io/interact_mixin.rb +0 -160
- data/lib/nuggets/io/modes.rb +0 -122
- data/lib/nuggets/io/null.rb +0 -5
- data/lib/nuggets/io/null_mixin.rb +0 -41
- data/lib/nuggets/io/redirect.rb +0 -5
- data/lib/nuggets/io/redirect_mixin.rb +0 -51
- data/lib/nuggets/lazy_attr.rb +0 -45
- data/lib/nuggets/log_parser.rb +0 -71
- data/lib/nuggets/log_parser/apache.rb +0 -102
- data/lib/nuggets/log_parser/rails.rb +0 -220
- data/lib/nuggets/lsi.rb +0 -8
- data/lib/nuggets/midos.rb +0 -8
- data/lib/nuggets/mysql.rb +0 -8
- data/lib/nuggets/net/success.rb +0 -60
- data/lib/nuggets/numeric/between.rb +0 -2
- data/lib/nuggets/numeric/duration.rb +0 -101
- data/lib/nuggets/numeric/limit.rb +0 -63
- data/lib/nuggets/numeric/signum.rb +0 -53
- data/lib/nuggets/numeric/to_multiple.rb +0 -62
- data/lib/nuggets/object/blank.rb +0 -20
- data/lib/nuggets/object/blank_mixin.rb +0 -100
- data/lib/nuggets/object/boolean.rb +0 -5
- data/lib/nuggets/object/boolean_mixin.rb +0 -62
- data/lib/nuggets/object/eigenclass.rb +0 -2
- data/lib/nuggets/object/ghost_class.rb +0 -2
- data/lib/nuggets/object/metaclass.rb +0 -2
- data/lib/nuggets/object/msend.rb +0 -5
- data/lib/nuggets/object/msend_mixin.rb +0 -44
- data/lib/nuggets/object/silence.rb +0 -5
- data/lib/nuggets/object/silence_mixin.rb +0 -45
- data/lib/nuggets/object/singleton_class.rb +0 -5
- data/lib/nuggets/object/singleton_class_mixin.rb +0 -96
- data/lib/nuggets/object/uniclass.rb +0 -2
- data/lib/nuggets/object/virtual_class.rb +0 -2
- data/lib/nuggets/pluggable.rb +0 -92
- data/lib/nuggets/proc/bind.rb +0 -5
- data/lib/nuggets/proc/bind_mixin.rb +0 -52
- data/lib/nuggets/range/quantile.rb +0 -5
- data/lib/nuggets/range/quantile_mixin.rb +0 -43
- data/lib/nuggets/rdf/compression.rb +0 -6
- data/lib/nuggets/rdf/prefix.rb +0 -6
- data/lib/nuggets/rdf/turtle.rb +0 -8
- data/lib/nuggets/rdf/turtle/reader.rb +0 -6
- data/lib/nuggets/rdf/uri.rb +0 -6
- data/lib/nuggets/ruby.rb +0 -236
- data/lib/nuggets/statistics.rb +0 -12
- data/lib/nuggets/statistics_mixins.rb +0 -12
- data/lib/nuggets/string/camelscore.rb +0 -5
- data/lib/nuggets/string/camelscore_mixin.rb +0 -117
- data/lib/nuggets/string/capitalize_first.rb +0 -47
- data/lib/nuggets/string/case.rb +0 -82
- data/lib/nuggets/string/evaluate.rb +0 -5
- data/lib/nuggets/string/evaluate_mixin.rb +0 -48
- data/lib/nuggets/string/msub.rb +0 -85
- data/lib/nuggets/string/nsub.rb +0 -66
- data/lib/nuggets/string/sub_with_md.rb +0 -112
- data/lib/nuggets/string/wc.rb +0 -5
- data/lib/nuggets/string/wc_mixin.rb +0 -96
- data/lib/nuggets/string/word_wrap.rb +0 -77
- data/lib/nuggets/string/xor.rb +0 -5
- data/lib/nuggets/string/xor_mixin.rb +0 -60
- data/lib/nuggets/tempfile/open.rb +0 -58
- data/lib/nuggets/uri/content_type.rb +0 -5
- data/lib/nuggets/uri/content_type_mixin.rb +0 -48
- data/lib/nuggets/uri/exist.rb +0 -5
- data/lib/nuggets/uri/exist_mixin.rb +0 -57
- data/lib/nuggets/uri/redirect.rb +0 -5
- data/lib/nuggets/uri/redirect_mixin.rb +0 -102
- data/lib/nuggets/util/ansicolor2css.rb +0 -4
- data/lib/nuggets/util/cli.rb +0 -4
- data/lib/nuggets/util/content_type.rb +0 -4
- data/lib/nuggets/util/dotted_decimal.rb +0 -3
- data/lib/nuggets/util/i18n.rb +0 -4
- data/lib/nuggets/util/lazy_attr.rb +0 -4
- data/lib/nuggets/util/log_parser.rb +0 -4
- data/lib/nuggets/util/log_parser/apache.rb +0 -4
- data/lib/nuggets/util/log_parser/rails.rb +0 -4
- data/lib/nuggets/util/midos.rb +0 -4
- data/lib/nuggets/util/mysql.rb +0 -4
- data/lib/nuggets/util/pluggable.rb +0 -4
- data/lib/nuggets/util/ruby.rb +0 -4
- data/lib/nuggets/version.rb +0 -27
- data/spec/nuggets/array/boost_spec.rb +0 -50
- data/spec/nuggets/array/combination_spec.rb +0 -25
- data/spec/nuggets/array/correlation_spec.rb +0 -81
- data/spec/nuggets/array/flatten_once_spec.rb +0 -16
- data/spec/nuggets/array/flush_spec.rb +0 -43
- data/spec/nuggets/array/format_spec.rb +0 -52
- data/spec/nuggets/array/hashify_spec.rb +0 -41
- data/spec/nuggets/array/histogram_spec.rb +0 -87
- data/spec/nuggets/array/in_order_spec.rb +0 -13
- data/spec/nuggets/array/limit_spec.rb +0 -62
- data/spec/nuggets/array/mean_spec.rb +0 -203
- data/spec/nuggets/array/median_spec.rb +0 -77
- data/spec/nuggets/array/mode_spec.rb +0 -57
- data/spec/nuggets/array/monotone_spec.rb +0 -30
- data/spec/nuggets/array/only_spec.rb +0 -26
- data/spec/nuggets/array/regression_spec.rb +0 -54
- data/spec/nuggets/array/runiq_spec.rb +0 -25
- data/spec/nuggets/array/standard_deviation_spec.rb +0 -33
- data/spec/nuggets/array/to_hash_spec.rb +0 -28
- data/spec/nuggets/array/variance_spec.rb +0 -106
- data/spec/nuggets/dotted_decimal_spec.rb +0 -27
- data/spec/nuggets/enumerable/all_any_extended_spec.rb +0 -31
- data/spec/nuggets/enumerable/minmax_spec.rb +0 -21
- data/spec/nuggets/env/set_spec.rb +0 -29
- data/spec/nuggets/env/user_encoding_spec.rb +0 -38
- data/spec/nuggets/env/user_home_spec.rb +0 -42
- data/spec/nuggets/file/ext_spec.rb +0 -38
- data/spec/nuggets/file/replace_spec.rb +0 -95
- data/spec/nuggets/file/sub_spec.rb +0 -149
- data/spec/nuggets/file/which_spec.rb +0 -22
- data/spec/nuggets/hash/at_spec.rb +0 -19
- data/spec/nuggets/hash/deep_fetch_spec.rb +0 -159
- data/spec/nuggets/hash/deep_merge_spec.rb +0 -78
- data/spec/nuggets/hash/in_order_spec.rb +0 -12
- data/spec/nuggets/hash/insert_spec.rb +0 -13
- data/spec/nuggets/hash/nest_spec.rb +0 -102
- data/spec/nuggets/hash/only_spec.rb +0 -29
- data/spec/nuggets/hash/seen_spec.rb +0 -36
- data/spec/nuggets/hash/unroll_spec.rb +0 -68
- data/spec/nuggets/i18n_spec.rb +0 -13
- data/spec/nuggets/integer/factorial_spec.rb +0 -10
- data/spec/nuggets/integer/length_spec.rb +0 -18
- data/spec/nuggets/integer/map_spec.rb +0 -19
- data/spec/nuggets/integer/to_binary_s_spec.rb +0 -19
- data/spec/nuggets/numeric/duration_spec.rb +0 -25
- data/spec/nuggets/numeric/limit_spec.rb +0 -16
- data/spec/nuggets/numeric/signum_spec.rb +0 -16
- data/spec/nuggets/numeric/to_multiple_spec.rb +0 -16
- data/spec/nuggets/object/blank_spec.rb +0 -34
- data/spec/nuggets/object/boolean_spec.rb +0 -23
- data/spec/nuggets/object/msend_spec.rb +0 -25
- data/spec/nuggets/object/silence_spec.rb +0 -36
- data/spec/nuggets/object/singleton_class_spec.rb +0 -51
- data/spec/nuggets/proc/bind_spec.rb +0 -28
- data/spec/nuggets/range/quantile_spec.rb +0 -33
- data/spec/nuggets/string/camelscore_spec.rb +0 -114
- data/spec/nuggets/string/capitalize_first_spec.rb +0 -13
- data/spec/nuggets/string/case_spec.rb +0 -31
- data/spec/nuggets/string/evaluate_spec.rb +0 -24
- data/spec/nuggets/string/msub_spec.rb +0 -20
- data/spec/nuggets/string/nsub_spec.rb +0 -13
- data/spec/nuggets/string/sub_with_md_spec.rb +0 -25
- data/spec/nuggets/string/wc_spec.rb +0 -73
- data/spec/nuggets/string/word_wrap_spec.rb +0 -81
- data/spec/nuggets/string/xor_spec.rb +0 -57
- data/spec/nuggets/uri/content_type_spec.rb +0 -42
- data/spec/nuggets/uri/exist_spec.rb +0 -49
- data/spec/spec_helper.rb +0 -36
data/lib/nuggets/string/wc.rb
DELETED
@@ -1,96 +0,0 @@
|
|
1
|
-
#--
|
2
|
-
###############################################################################
|
3
|
-
# #
|
4
|
-
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
-
# language. #
|
6
|
-
# #
|
7
|
-
# Copyright (C) 2007-2011 Jens Wille #
|
8
|
-
# #
|
9
|
-
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@gmail.com> #
|
11
|
-
# #
|
12
|
-
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
-
# under the terms of the GNU Affero General Public License as published by #
|
14
|
-
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
-
# option) any later version. #
|
16
|
-
# #
|
17
|
-
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
20
|
-
# for more details. #
|
21
|
-
# #
|
22
|
-
# You should have received a copy of the GNU Affero General Public License #
|
23
|
-
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
-
# #
|
25
|
-
###############################################################################
|
26
|
-
#++
|
27
|
-
|
28
|
-
module Nuggets
|
29
|
-
class String
|
30
|
-
module WcMixin
|
31
|
-
|
32
|
-
# call-seq:
|
33
|
-
# str.wc => anArray
|
34
|
-
#
|
35
|
-
# Count number of lines, words, and bytes in _str_.
|
36
|
-
def wc
|
37
|
-
[wc_l, wc_w, wc_c]
|
38
|
-
end
|
39
|
-
|
40
|
-
# call-seq:
|
41
|
-
# str.line_count => anInteger
|
42
|
-
#
|
43
|
-
# Count number of lines in _str_.
|
44
|
-
def line_count
|
45
|
-
count_by_re(/#{$/}/)
|
46
|
-
end
|
47
|
-
alias_method :wc_l, :line_count
|
48
|
-
|
49
|
-
# call-seq:
|
50
|
-
# str.word_count => anInteger
|
51
|
-
#
|
52
|
-
# Count number of words in _str_.
|
53
|
-
def word_count
|
54
|
-
count_by_re(/\S+/)
|
55
|
-
end
|
56
|
-
alias_method :wc_w, :word_count
|
57
|
-
|
58
|
-
# call-seq:
|
59
|
-
# str.byte_count => anInteger
|
60
|
-
#
|
61
|
-
# Count number of bytes in _str_.
|
62
|
-
def byte_count
|
63
|
-
respond_to?(:bytesize) ? bytesize : count_by_re(//n) - 1
|
64
|
-
end
|
65
|
-
alias_method :wc_c, :byte_count
|
66
|
-
|
67
|
-
# call-seq:
|
68
|
-
# str.char_count => anInteger
|
69
|
-
#
|
70
|
-
# Count number of characters in _str_.
|
71
|
-
def char_count
|
72
|
-
count_by_re(/./um)
|
73
|
-
end
|
74
|
-
alias_method :wc_m, :char_count
|
75
|
-
|
76
|
-
# call-seq:
|
77
|
-
# str.count_by_re(re) => anInteger
|
78
|
-
#
|
79
|
-
# Count number of occurrences of +re+ in _str_.
|
80
|
-
def count_by_re(re)
|
81
|
-
scan(re).size
|
82
|
-
end
|
83
|
-
|
84
|
-
# call-seq:
|
85
|
-
# str.count_by_re2(re) => anInteger
|
86
|
-
#
|
87
|
-
# A more memory-efficient version of #count_by_re.
|
88
|
-
def count_by_re2(re)
|
89
|
-
count = 0
|
90
|
-
scan(re) { |_| count += 1 }
|
91
|
-
count
|
92
|
-
end
|
93
|
-
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
@@ -1,77 +0,0 @@
|
|
1
|
-
#--
|
2
|
-
###############################################################################
|
3
|
-
# #
|
4
|
-
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
-
# language. #
|
6
|
-
# #
|
7
|
-
# Copyright (C) 2007-2011 Jens Wille #
|
8
|
-
# #
|
9
|
-
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@gmail.com> #
|
11
|
-
# #
|
12
|
-
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
-
# under the terms of the GNU Affero General Public License as published by #
|
14
|
-
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
-
# option) any later version. #
|
16
|
-
# #
|
17
|
-
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
20
|
-
# for more details. #
|
21
|
-
# #
|
22
|
-
# You should have received a copy of the GNU Affero General Public License #
|
23
|
-
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
-
# #
|
25
|
-
###############################################################################
|
26
|
-
#++
|
27
|
-
|
28
|
-
require 'enumerator'
|
29
|
-
|
30
|
-
class String
|
31
|
-
|
32
|
-
# call-seq:
|
33
|
-
# str.word_wrap(line_width) => new_str
|
34
|
-
#
|
35
|
-
# Word wrap a string not exceeding +line_width+. Based on the Ruby Facets
|
36
|
-
# implementation, but preserves paragraphs. Thus
|
37
|
-
# <tt>str == str.word_wrap(str.split("\n").map { |l| l.length }.max)</tt>.
|
38
|
-
def word_wrap(line_width = 80, as_array = false)
|
39
|
-
wrapped = []
|
40
|
-
|
41
|
-
split(/(\n+)/).to_enum(:each_slice, 2).each { |paragraph, linebreaks|
|
42
|
-
wrapped << paragraph.word_wrap_paragraph!(line_width) << linebreaks
|
43
|
-
}
|
44
|
-
|
45
|
-
wrapped = wrapped.join
|
46
|
-
|
47
|
-
as_array ? wrapped.split("\n") : wrapped
|
48
|
-
end
|
49
|
-
|
50
|
-
# call-seq:
|
51
|
-
# str.word_wrap!(line_width) => str
|
52
|
-
#
|
53
|
-
# As with #word_wrap, but modifies the string in place.
|
54
|
-
def word_wrap!(line_width = 80)
|
55
|
-
replace(word_wrap(line_width))
|
56
|
-
end
|
57
|
-
|
58
|
-
# call-seq:
|
59
|
-
# str.word_wrap_paragraph(line_width) => new_str
|
60
|
-
#
|
61
|
-
# Similar to #word_wrap, but assumes a single paragraph.
|
62
|
-
def word_wrap_paragraph(line_width = 80)
|
63
|
-
(_dup = dup).word_wrap_paragraph!(line_width) || _dup
|
64
|
-
end
|
65
|
-
|
66
|
-
# call-seq:
|
67
|
-
# str.word_wrap_paragraph!(line_width) => str
|
68
|
-
#
|
69
|
-
# Destructive version of #word_wrap_paragraph.
|
70
|
-
def word_wrap_paragraph!(line_width = 80)
|
71
|
-
gsub!(/(.{1,#{line_width}})(?:\s+|$)/, "\\1\n")
|
72
|
-
sub!(/\n$/, '')
|
73
|
-
|
74
|
-
self
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
data/lib/nuggets/string/xor.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
#--
|
2
|
-
###############################################################################
|
3
|
-
# #
|
4
|
-
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
-
# language. #
|
6
|
-
# #
|
7
|
-
# Copyright (C) 2007-2011 Jens Wille #
|
8
|
-
# #
|
9
|
-
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@gmail.com> #
|
11
|
-
# #
|
12
|
-
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
-
# under the terms of the GNU Affero General Public License as published by #
|
14
|
-
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
-
# option) any later version. #
|
16
|
-
# #
|
17
|
-
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
20
|
-
# for more details. #
|
21
|
-
# #
|
22
|
-
# You should have received a copy of the GNU Affero General Public License #
|
23
|
-
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
-
# #
|
25
|
-
###############################################################################
|
26
|
-
#++
|
27
|
-
|
28
|
-
require 'nuggets/integer/to_binary_s'
|
29
|
-
|
30
|
-
module Nuggets
|
31
|
-
class String
|
32
|
-
module XorMixin
|
33
|
-
|
34
|
-
# call-seq:
|
35
|
-
# str ^ other => new_string
|
36
|
-
# str.xor(other) => new_string
|
37
|
-
#
|
38
|
-
# Bitwise EXCLUSIVE OR.
|
39
|
-
def xor(other, require_same_length = false)
|
40
|
-
format = 'B*'
|
41
|
-
binary = [self, other.to_s].map { |s| s.unpack(format).first }
|
42
|
-
|
43
|
-
length = binary.map { |s| s.length }.inject { |a, b|
|
44
|
-
if require_same_length
|
45
|
-
a == b ? a : raise(::ArgumentError, 'must be of same length')
|
46
|
-
else
|
47
|
-
[a, b].max
|
48
|
-
end
|
49
|
-
}
|
50
|
-
|
51
|
-
[binary.map { |s| s.to_i(2) }.
|
52
|
-
inject { |a, b| a ^ b }.
|
53
|
-
to_binary_s(length)].pack(format)
|
54
|
-
end
|
55
|
-
|
56
|
-
alias_method :^, :xor
|
57
|
-
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
@@ -1,58 +0,0 @@
|
|
1
|
-
#--
|
2
|
-
###############################################################################
|
3
|
-
# #
|
4
|
-
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
-
# language. #
|
6
|
-
# #
|
7
|
-
# Copyright (C) 2007-2011 Jens Wille #
|
8
|
-
# #
|
9
|
-
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@gmail.com> #
|
11
|
-
# #
|
12
|
-
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
-
# under the terms of the GNU Affero General Public License as published by #
|
14
|
-
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
-
# option) any later version. #
|
16
|
-
# #
|
17
|
-
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
20
|
-
# for more details. #
|
21
|
-
# #
|
22
|
-
# You should have received a copy of the GNU Affero General Public License #
|
23
|
-
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
-
# #
|
25
|
-
###############################################################################
|
26
|
-
#++
|
27
|
-
|
28
|
-
require 'tempfile'
|
29
|
-
|
30
|
-
class Tempfile
|
31
|
-
|
32
|
-
class << self
|
33
|
-
|
34
|
-
alias_method :_nuggets_original_open, :open
|
35
|
-
|
36
|
-
# If no block is given, this is a synonym for new().
|
37
|
-
#
|
38
|
-
# If a block is given, it will be passed tempfile as an argument,
|
39
|
-
# and the tempfile will automatically be closed when the block
|
40
|
-
# terminates. In this case, open() returns tempfile -- in contrast
|
41
|
-
# to the original implementation, which returns +nil+.
|
42
|
-
def open(*args)
|
43
|
-
tempfile = new(*args)
|
44
|
-
|
45
|
-
if block_given?
|
46
|
-
begin
|
47
|
-
yield tempfile
|
48
|
-
ensure
|
49
|
-
tempfile.close
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
tempfile
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
#--
|
2
|
-
###############################################################################
|
3
|
-
# #
|
4
|
-
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
-
# language. #
|
6
|
-
# #
|
7
|
-
# Copyright (C) 2007-2011 Jens Wille #
|
8
|
-
# #
|
9
|
-
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@gmail.com> #
|
11
|
-
# #
|
12
|
-
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
-
# under the terms of the GNU Affero General Public License as published by #
|
14
|
-
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
-
# option) any later version. #
|
16
|
-
# #
|
17
|
-
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
20
|
-
# for more details. #
|
21
|
-
# #
|
22
|
-
# You should have received a copy of the GNU Affero General Public License #
|
23
|
-
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
-
# #
|
25
|
-
###############################################################################
|
26
|
-
#++
|
27
|
-
|
28
|
-
require 'nuggets/uri/exist_mixin'
|
29
|
-
|
30
|
-
module Nuggets
|
31
|
-
module URI
|
32
|
-
module ContentTypeMixin
|
33
|
-
|
34
|
-
def self.extended(base)
|
35
|
-
base.extend Nuggets::URI::ExistMixin
|
36
|
-
end
|
37
|
-
|
38
|
-
# call-seq:
|
39
|
-
# URI.content_type(uri) => aString or +nil+
|
40
|
-
#
|
41
|
-
# Return the content type of +uri+, or +nil+ if not found.
|
42
|
-
def content_type(uri)
|
43
|
-
exist?(uri) { |res| res.content_type }
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
data/lib/nuggets/uri/exist.rb
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
#--
|
2
|
-
###############################################################################
|
3
|
-
# #
|
4
|
-
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
-
# language. #
|
6
|
-
# #
|
7
|
-
# Copyright (C) 2007-2011 Jens Wille #
|
8
|
-
# #
|
9
|
-
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@gmail.com> #
|
11
|
-
# #
|
12
|
-
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
-
# under the terms of the GNU Affero General Public License as published by #
|
14
|
-
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
-
# option) any later version. #
|
16
|
-
# #
|
17
|
-
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
20
|
-
# for more details. #
|
21
|
-
# #
|
22
|
-
# You should have received a copy of the GNU Affero General Public License #
|
23
|
-
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
-
# #
|
25
|
-
###############################################################################
|
26
|
-
#++
|
27
|
-
|
28
|
-
require 'nuggets/uri/redirect_mixin'
|
29
|
-
require 'nuggets/net/success'
|
30
|
-
|
31
|
-
module Nuggets
|
32
|
-
module URI
|
33
|
-
module ExistMixin
|
34
|
-
|
35
|
-
def self.extended(base)
|
36
|
-
base.extend Nuggets::URI::RedirectMixin
|
37
|
-
end
|
38
|
-
|
39
|
-
# call-seq:
|
40
|
-
# URI.exist?(uri) => +true+, +false+ or +nil+
|
41
|
-
# URI.exist?(uri) { |res| ... } => anObject, +false+ or +nil+
|
42
|
-
#
|
43
|
-
# Checks whether the URI +uri+ exists by performing a +HEAD+ request. If
|
44
|
-
# successful, yields the response to the given block and returns its return
|
45
|
-
# value, or +true+ if no block was given. Returns +false+ in case of failure,
|
46
|
-
# or +nil+ if the redirect limit has been exceeded.
|
47
|
-
#
|
48
|
-
# See Nuggets::URI::RedirectMixin#follow_redirect for more information.
|
49
|
-
def exist?(uri)
|
50
|
-
head_redirect(uri) { |res| res.success? && (!block_given? || yield(res)) }
|
51
|
-
end
|
52
|
-
|
53
|
-
alias_method :exists?, :exist?
|
54
|
-
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
data/lib/nuggets/uri/redirect.rb
DELETED
@@ -1,102 +0,0 @@
|
|
1
|
-
#--
|
2
|
-
###############################################################################
|
3
|
-
# #
|
4
|
-
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
-
# language. #
|
6
|
-
# #
|
7
|
-
# Copyright (C) 2007-2011 Jens Wille #
|
8
|
-
# #
|
9
|
-
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@gmail.com> #
|
11
|
-
# #
|
12
|
-
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
-
# under the terms of the GNU Affero General Public License as published by #
|
14
|
-
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
-
# option) any later version. #
|
16
|
-
# #
|
17
|
-
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
20
|
-
# for more details. #
|
21
|
-
# #
|
22
|
-
# You should have received a copy of the GNU Affero General Public License #
|
23
|
-
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
-
# #
|
25
|
-
###############################################################################
|
26
|
-
#++
|
27
|
-
|
28
|
-
require 'net/https'
|
29
|
-
|
30
|
-
module Nuggets
|
31
|
-
module URI
|
32
|
-
module RedirectMixin
|
33
|
-
|
34
|
-
# Maximum number of redirects to follow.
|
35
|
-
URI_REDIRECT_MAX_STEPS = 20
|
36
|
-
|
37
|
-
# Cache for HTTP objects to reuse.
|
38
|
-
URI_REDIRECT_HTTP_CACHE = ::Hash.new { |h, k| h[k] = ::Net::HTTP.new(*k) }
|
39
|
-
|
40
|
-
# call-seq:
|
41
|
-
# URI.follow_redirect(uri[, steps]) { |req, path| ... } => aResponse or +nil+
|
42
|
-
#
|
43
|
-
# Performs any HTTP request on +uri+ while following at most +steps+ redirects.
|
44
|
-
# Accepts both strings and URI objects for +uri+. Defers to the given block to
|
45
|
-
# perform the actual request; yields the request object and the request URI
|
46
|
-
# string.
|
47
|
-
#
|
48
|
-
# Returns the response object if successful, or +nil+ if +uri+ is not an HTTP
|
49
|
-
# URI, the redirect limit has been exceeded, or any connection error occurs.
|
50
|
-
def follow_redirect(uri, steps = URI_REDIRECT_MAX_STEPS, cache = URI_REDIRECT_HTTP_CACHE)
|
51
|
-
unless uri.is_a?(::URI::HTTP)
|
52
|
-
uri = ::URI.parse(uri.to_s)
|
53
|
-
return unless uri.is_a?(::URI::HTTP)
|
54
|
-
end
|
55
|
-
|
56
|
-
req = cache[[uri.host, uri.port]]
|
57
|
-
req.use_ssl = uri.is_a?(::URI::HTTPS)
|
58
|
-
|
59
|
-
ctx = req.instance_variable_get(:@ssl_context) if req.use_ssl?
|
60
|
-
ctx.verify_mode ||= ::OpenSSL::SSL::VERIFY_NONE if ctx
|
61
|
-
|
62
|
-
res = yield req, uri.request_uri
|
63
|
-
return res unless res.is_a?(::Net::HTTPRedirection)
|
64
|
-
return nil unless steps > 0
|
65
|
-
|
66
|
-
follow_redirect(res['Location'], steps - 1, cache) { |*a| yield(*a) }
|
67
|
-
rescue ::SocketError, ::Errno::EHOSTUNREACH, ::Errno::ENOENT
|
68
|
-
end
|
69
|
-
|
70
|
-
# call-seq:
|
71
|
-
# URI.head_redirect(uri[, steps]) => aResponse or +nil+
|
72
|
-
# URI.head_redirect(uri[, steps]) { |res| ... } => anObject or +nil+
|
73
|
-
#
|
74
|
-
# Performs a +HEAD+ request on +uri+ while following at most +steps+ redirects.
|
75
|
-
# If successful, yields the response to the given block and returns its return
|
76
|
-
# value, or the response itself if no block was given. Returns +nil+ in case
|
77
|
-
# of failure.
|
78
|
-
#
|
79
|
-
# See Nuggets::URI::RedirectMixin#follow_redirect for more information.
|
80
|
-
def head_redirect(uri, steps = URI_REDIRECT_MAX_STEPS)
|
81
|
-
res = follow_redirect(uri, steps) { |req, path| req.head(path) }
|
82
|
-
res && block_given? ? yield(res) : res
|
83
|
-
end
|
84
|
-
|
85
|
-
# call-seq:
|
86
|
-
# URI.get_redirect(uri[, steps]) => aResponse or +nil+
|
87
|
-
# URI.get_redirect(uri[, steps]) { |res| ... } => anObject or +nil+
|
88
|
-
#
|
89
|
-
# Performs a +GET+ request on +uri+ while following at most +steps+ redirects.
|
90
|
-
# If successful, yields the response to the given block and returns its return
|
91
|
-
# value, or the response itself if no block was given. Returns +nil+ in case
|
92
|
-
# of failure.
|
93
|
-
#
|
94
|
-
# See Nuggets::URI::RedirectMixin#follow_redirect for more information.
|
95
|
-
def get_redirect(uri, steps = URI_REDIRECT_MAX_STEPS)
|
96
|
-
res = follow_redirect(uri, steps) { |req, path| req.get(path) }
|
97
|
-
res && block_given? ? yield(res) : res
|
98
|
-
end
|
99
|
-
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|