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