hosemonkey 0.1.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.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README +0 -0
- data/Rakefile +10 -0
- data/hosemonkey.gemspec +27 -0
- data/lib/hosemonkey.rb +10 -0
- data/lib/hosemonkey/ext/array.rb +6 -0
- data/lib/hosemonkey/ext/boolean.rb +17 -0
- data/lib/hosemonkey/ext/date.rb +21 -0
- data/lib/hosemonkey/ext/fixnum.rb +25 -0
- data/lib/hosemonkey/ext/hash.rb +97 -0
- data/lib/hosemonkey/ext/string.rb +61 -0
- data/lib/hosemonkey/ext/time.rb +119 -0
- data/lib/hosemonkey/version.rb +3 -0
- data/spec/fixnum_spec.rb +28 -0
- data/spec/hash_spec.rb +75 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/string_spec.rb +44 -0
- data/spec/time_spec.rb +21 -0
- metadata +133 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
data/hosemonkey.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "hosemonkey/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "hosemonkey"
|
7
|
+
s.version = Hosemonkey::VERSION
|
8
|
+
s.authors = ["Rob Lyon"]
|
9
|
+
s.email = ["rlyon@uidaho.edu"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Write a gem summary}
|
12
|
+
s.description = %q{Write a gem description}
|
13
|
+
|
14
|
+
s.rubyforge_project = "hosemonkey"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "rake"
|
23
|
+
s.add_development_dependency "rspec"
|
24
|
+
s.add_development_dependency "mocha"
|
25
|
+
|
26
|
+
s.add_runtime_dependency "nokogiri"
|
27
|
+
end
|
data/lib/hosemonkey.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Hosemonkey
|
2
|
+
module Boolean
|
3
|
+
Mapping = {
|
4
|
+
true => true,
|
5
|
+
'true' => true,
|
6
|
+
'1' => true,
|
7
|
+
1 => true,
|
8
|
+
false => false,
|
9
|
+
'false' => false,
|
10
|
+
'0' => false,
|
11
|
+
0 => false,
|
12
|
+
nil => nil
|
13
|
+
}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Boolean ; include Hosemonkey::Boolean ; end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Date
|
2
|
+
# From active support
|
3
|
+
def advance(options)
|
4
|
+
options = options.dup
|
5
|
+
d = self
|
6
|
+
d = d >> options.delete(:years) * 12 if options[:years]
|
7
|
+
d = d >> options.delete(:months) if options[:months]
|
8
|
+
d = d + options.delete(:weeks) * 7 if options[:weeks]
|
9
|
+
d = d + options.delete(:days) if options[:days]
|
10
|
+
d
|
11
|
+
end
|
12
|
+
|
13
|
+
# From active support
|
14
|
+
def change(options)
|
15
|
+
::Date.new(
|
16
|
+
options[:year] || self.year,
|
17
|
+
options[:month] || self.month,
|
18
|
+
options[:day] || self.day
|
19
|
+
)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Hosemonkey
|
2
|
+
module Fixnum
|
3
|
+
def days
|
4
|
+
::Time.now + (60 * 60 * 24 * self)
|
5
|
+
end
|
6
|
+
|
7
|
+
def hours
|
8
|
+
::Time.now + (60 * 60 * self)
|
9
|
+
end
|
10
|
+
|
11
|
+
def weeks
|
12
|
+
::Time.now + (60 * 60 * 24 * 7 * self)
|
13
|
+
end
|
14
|
+
|
15
|
+
def months
|
16
|
+
::Time.now.advance(:months => self)
|
17
|
+
end
|
18
|
+
|
19
|
+
def years
|
20
|
+
::Time.now.advance(:years => self)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Fixnum ; include Hosemonkey::Fixnum ; end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
class Hash
|
2
|
+
def stringify_keys
|
3
|
+
keys.each do |key|
|
4
|
+
self[(key.to_s rescue key) || key] = delete(key)
|
5
|
+
end
|
6
|
+
self
|
7
|
+
end
|
8
|
+
|
9
|
+
# From rails active_support. Destructively convert all keys to symbols as long as they respond to to_sym
|
10
|
+
def symbolize_keys!
|
11
|
+
keys.each do |key|
|
12
|
+
self[(key.to_sym rescue key) || key] = delete(key)
|
13
|
+
end
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
# From rails active_support. Return a new hash with all keys converted to symbols
|
18
|
+
def symbolize_keys
|
19
|
+
dup.symbolize_keys!
|
20
|
+
end
|
21
|
+
|
22
|
+
def include_only?(*args)
|
23
|
+
self.each do |key,value|
|
24
|
+
unless args.include?(key)
|
25
|
+
return false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
return true
|
29
|
+
end
|
30
|
+
|
31
|
+
def include_only(*args)
|
32
|
+
self.each do |key,value|
|
33
|
+
unless args.include?(key)
|
34
|
+
raise "Unknown Argument"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class << self
|
40
|
+
# https://gist.github.com/819999
|
41
|
+
def from_xml_string(xml)
|
42
|
+
begin
|
43
|
+
result = Nokogiri::XML(xml) do |config|
|
44
|
+
config.strict.noent
|
45
|
+
end
|
46
|
+
return { result.root.name => xml_node_to_hash(result.root)}
|
47
|
+
rescue Exception => e
|
48
|
+
raise "Could not parse xml: #{e}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# https://gist.github.com/819999
|
53
|
+
def xml_node_to_hash(node)
|
54
|
+
# If we are at the root of the document, start the hash
|
55
|
+
if node.element?
|
56
|
+
result_hash = {}
|
57
|
+
if node.attributes != {}
|
58
|
+
attributes = {}
|
59
|
+
node.attributes.keys.each do |key|
|
60
|
+
attributes[node.attributes[key].name] = node.attributes[key].value
|
61
|
+
end
|
62
|
+
end
|
63
|
+
if node.children.size > 0
|
64
|
+
node.children.each do |child|
|
65
|
+
result = xml_node_to_hash(child)
|
66
|
+
|
67
|
+
if child.name == "text"
|
68
|
+
unless child.next_sibling || child.previous_sibling
|
69
|
+
return result unless attributes
|
70
|
+
result_hash[child.name] = result
|
71
|
+
end
|
72
|
+
elsif result_hash[child.name]
|
73
|
+
|
74
|
+
if result_hash[child.name].is_a?(Object::Array)
|
75
|
+
result_hash[child.name] << result
|
76
|
+
else
|
77
|
+
result_hash[child.name] = [result_hash[child.name]] << result
|
78
|
+
end
|
79
|
+
else
|
80
|
+
result_hash[child.name] = result
|
81
|
+
end
|
82
|
+
end
|
83
|
+
if attributes
|
84
|
+
#add code to remove non-data attributes e.g. xml schema, namespace here
|
85
|
+
#if there is a collision then node content supersets attributes
|
86
|
+
result_hash = attributes.merge(result_hash)
|
87
|
+
end
|
88
|
+
return result_hash
|
89
|
+
else
|
90
|
+
return attributes
|
91
|
+
end
|
92
|
+
else
|
93
|
+
return node.content.to_s
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
class String
|
2
|
+
# def uncamelize
|
3
|
+
# uncameled = self.each_char.inject("") do |result,c|
|
4
|
+
# unless "#{c}" =~ /^[A-Z]$/
|
5
|
+
# result << "#{c}"
|
6
|
+
# else
|
7
|
+
# result << "_" unless result.empty?
|
8
|
+
# result << "#{c}".downcase
|
9
|
+
# end
|
10
|
+
# end
|
11
|
+
# uncameled
|
12
|
+
# end
|
13
|
+
|
14
|
+
def camelize
|
15
|
+
cap_next_char = true
|
16
|
+
cameled = self.each_char.inject("") do |result, c|
|
17
|
+
unless "#{c}" =~ /^[-_]$/
|
18
|
+
unless cap_next_char
|
19
|
+
result << "#{c}"
|
20
|
+
else
|
21
|
+
result << "#{c}".upcase
|
22
|
+
cap_next_char = false
|
23
|
+
end
|
24
|
+
else
|
25
|
+
cap_next_char = true
|
26
|
+
end
|
27
|
+
result
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class << self
|
32
|
+
def hex(args = { :length => 24, :case => :upper })
|
33
|
+
unless args.include_only?( :length, :case )
|
34
|
+
raise "Invalid parameters in random"
|
35
|
+
end
|
36
|
+
|
37
|
+
h = (((0..args[:length]).map{rand(256).chr}*"").unpack("H*")[0][0,args[:length]])
|
38
|
+
|
39
|
+
if args[:case] == :lower
|
40
|
+
h.downcase
|
41
|
+
else
|
42
|
+
h.upcase
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def random(args = { :length => 24, :charset => :all })
|
47
|
+
unless args.include_only?( :length, :charset )
|
48
|
+
raise "Invalid parameters in random"
|
49
|
+
end
|
50
|
+
|
51
|
+
if args[:charset] == :alpha
|
52
|
+
chars = ('a'..'z').to_a + ('A'..'Z').to_a
|
53
|
+
elsif args[:charset] == :alnum_upper
|
54
|
+
chars = ('A'..'Z').to_a + ('0'..'9').to_a
|
55
|
+
else
|
56
|
+
chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
|
57
|
+
end
|
58
|
+
(0...args[:length]).collect { chars[Kernel.rand(chars.length)] }.join
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
module Hosemonkey
|
2
|
+
module Time
|
3
|
+
# Copied directly from ActiveSupport
|
4
|
+
COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
5
|
+
# Copied directly from ActiveSupport
|
6
|
+
DAYS_INTO_WEEK = {
|
7
|
+
:monday => 0,
|
8
|
+
:tuesday => 1,
|
9
|
+
:wednesday => 2,
|
10
|
+
:thursday => 3,
|
11
|
+
:friday => 4,
|
12
|
+
:saturday => 5,
|
13
|
+
:sunday => 6
|
14
|
+
}
|
15
|
+
|
16
|
+
# Copied directly from ActiveSupport
|
17
|
+
# Return the number of days in the given month.
|
18
|
+
# If no year is specified, it will use the current year.
|
19
|
+
def days_in_month(month, year = now.year)
|
20
|
+
if month == 2 && ::Date.gregorian_leap?(year)
|
21
|
+
29
|
22
|
+
else
|
23
|
+
COMMON_YEAR_DAYS_IN_MONTH[month]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Copied directly from ActiveSupport
|
28
|
+
# Uses Date to provide precise Time calculations for years, months, and days.
|
29
|
+
# The +options+ parameter takes a hash with any of these keys: <tt>:years</tt>,
|
30
|
+
# <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>, <tt>:hours</tt>,
|
31
|
+
# <tt>:minutes</tt>, <tt>:seconds</tt>.
|
32
|
+
def advance(options)
|
33
|
+
unless options[:weeks].nil?
|
34
|
+
options[:weeks], partial_weeks = options[:weeks].divmod(1)
|
35
|
+
options[:days] = options.fetch(:days, 0) + 7 * partial_weeks
|
36
|
+
end
|
37
|
+
|
38
|
+
unless options[:days].nil?
|
39
|
+
options[:days], partial_days = options[:days].divmod(1)
|
40
|
+
options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
|
41
|
+
end
|
42
|
+
|
43
|
+
d = to_date.advance(options)
|
44
|
+
time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
|
45
|
+
seconds_to_advance = \
|
46
|
+
options.fetch(:seconds, 0) +
|
47
|
+
options.fetch(:minutes, 0) * 60 +
|
48
|
+
options.fetch(:hours, 0) * 3600
|
49
|
+
|
50
|
+
if seconds_to_advance.zero?
|
51
|
+
time_advanced_by_date
|
52
|
+
else
|
53
|
+
time_advanced_by_date.since(seconds_to_advance)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Copied directly from ActiveSupport
|
58
|
+
# Returns a new Time where one or more of the elements have been changed according to the +options+ parameter. The time options
|
59
|
+
# (hour, min, sec, usec) reset cascadingly, so if only the hour is passed, then minute, sec, and usec is set to 0. If the hour and
|
60
|
+
# minute is passed, then sec and usec is set to 0.
|
61
|
+
def change(options)
|
62
|
+
::Time.send(
|
63
|
+
utc? ? :utc_time : :local_time,
|
64
|
+
options.fetch(:year, year),
|
65
|
+
options.fetch(:month, month),
|
66
|
+
options.fetch(:day, day),
|
67
|
+
options.fetch(:hour, hour),
|
68
|
+
options.fetch(:min, options[:hour] ? 0 : min),
|
69
|
+
options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec),
|
70
|
+
options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000))
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_z
|
75
|
+
self.gmtime.strftime("%Y-%m-%dT%H:%M:%S.000Z")
|
76
|
+
end
|
77
|
+
|
78
|
+
def to_web
|
79
|
+
self.gmtime.strftime("%a, %d %b %Y %H:%M:%S %z")
|
80
|
+
end
|
81
|
+
|
82
|
+
def to_short
|
83
|
+
self.gmtime.strftime("%Y%m")
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
class Time
|
90
|
+
include Hosemonkey::Time
|
91
|
+
class << self
|
92
|
+
# Copied directly from ActiveSupport
|
93
|
+
# Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:utc</tt>.
|
94
|
+
def utc_time(*args)
|
95
|
+
time_with_datetime_fallback(:utc, *args)
|
96
|
+
end
|
97
|
+
|
98
|
+
# Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:local</tt>.
|
99
|
+
def local_time(*args)
|
100
|
+
time_with_datetime_fallback(:local, *args)
|
101
|
+
end
|
102
|
+
# Copied directly from ActiveSupport
|
103
|
+
# Returns a new Time if requested year can be accommodated by Ruby's Time class
|
104
|
+
# (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture);
|
105
|
+
# otherwise returns a DateTime.
|
106
|
+
def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
|
107
|
+
time = ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
|
108
|
+
|
109
|
+
# This check is needed because Time.utc(y) returns a time object in the 2000s for 0 <= y <= 138.
|
110
|
+
if time.year == year
|
111
|
+
time
|
112
|
+
else
|
113
|
+
::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec)
|
114
|
+
end
|
115
|
+
rescue
|
116
|
+
::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
data/spec/fixnum_spec.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe "Fixnum monkey: " do
|
4
|
+
it "days should return n number of days in the future" do
|
5
|
+
Time.stubs(:now).returns(Time.mktime(1970,1,1,0,0,0))
|
6
|
+
10.days.should == Time.mktime(1970,1,11,0,0,0)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "hours should return n number of hours in the future" do
|
10
|
+
Time.stubs(:now).returns(Time.mktime(1970,1,1,0,0,0))
|
11
|
+
10.hours.should == Time.mktime(1970,1,1,10,0,0)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "weeks should return n number of weeks in the future" do
|
15
|
+
Time.stubs(:now).returns(Time.mktime(1970,1,1,0,0,0))
|
16
|
+
2.weeks.should == Time.mktime(1970,1,15,0,0,0)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "months should return n number of months in the future" do
|
20
|
+
Time.stubs(:now).returns(Time.mktime(1970,1,1,0,0,0))
|
21
|
+
15.months.should == Time.mktime(1971,4,1,0,0,0)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "years should return n number of years in the future" do
|
25
|
+
Time.stubs(:now).returns(Time.mktime(1970,1,1,0,0,0))
|
26
|
+
15.years.should == Time.mktime(1985,1,1,0,0,0)
|
27
|
+
end
|
28
|
+
end
|
data/spec/hash_spec.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe "Hash monkey: include_only?" do
|
4
|
+
it "should return true if only the provided keys are found in the hash" do
|
5
|
+
h = {:a => 0, :b => 1, :c => 2}
|
6
|
+
{}.include_only?(:a).should be_true
|
7
|
+
h.include_only?(:a).should be_false
|
8
|
+
h.include_only?(:a, :b).should be_false
|
9
|
+
h.include_only?(:a, :b, :c).should be_true
|
10
|
+
h.include_only?(:a, :b, :c, :d).should be_true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
describe "Hash monkey: stringify keys" do
|
16
|
+
it "should turn symbols into key" do
|
17
|
+
h = {:a => 0, :b => 1, :c => 2}
|
18
|
+
h.stringify_keys
|
19
|
+
h.keys.size.should == 3
|
20
|
+
h.has_key?(:a).should be_false
|
21
|
+
h.has_key?(:b).should be_false
|
22
|
+
h.has_key?(:c).should be_false
|
23
|
+
h.has_key?('a').should be_true
|
24
|
+
h.has_key?('b').should be_true
|
25
|
+
h.has_key?('c').should be_true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "Hash monkey: include_only" do
|
30
|
+
it "shouldn't raise exception if only the provided keys are found in the hash" do
|
31
|
+
h = {:a => 0, :b => 1, :c => 3}
|
32
|
+
expect { {}.include_only(:a) }.to_not raise_error
|
33
|
+
expect { h.include_only(:a) }.to raise_error
|
34
|
+
expect { h.include_only(:a, :b) }.to raise_error
|
35
|
+
expect { h.include_only(:a, :b, :c) }.to_not raise_error
|
36
|
+
expect { h.include_only(:a, :b, :c, :d) }.to_not raise_error
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "Hash monkey: from_xml_string" do
|
41
|
+
it "should create a hash of symbol => values" do
|
42
|
+
xml = "<Error><Code>Hello</Code><Message>World</Message><MyDate>Now</MyDate></Error>"
|
43
|
+
h = Hash.from_xml_string(xml)
|
44
|
+
h.has_key?('Error').should be_true
|
45
|
+
error = h['Error']
|
46
|
+
error.has_key?('Code').should be_true
|
47
|
+
error['Code'].should == "Hello"
|
48
|
+
error.has_key?('Message').should be_true
|
49
|
+
error['Message'].should == "World"
|
50
|
+
error.has_key?('MyDate').should be_true
|
51
|
+
error['MyDate'].should == "Now"
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should raise exception on invalid xml" do
|
55
|
+
xml = "<Error><Code>Hello</Code><Message>World</Message><MyDate>Now</BADTAG></Error>"
|
56
|
+
expect { h = Hash.from_xml_string(xml) ; puts h.inspect }.to raise_error
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should parse attributes" do
|
60
|
+
xml = "<Tag attr=\"hello world\">Hello</Tag>"
|
61
|
+
h = Hash.from_xml_string(xml)
|
62
|
+
tag = h['Tag']
|
63
|
+
tag.has_key?('attr')
|
64
|
+
tag['attr'].should == "hello world"
|
65
|
+
tag['text'].should == "Hello"
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should handle multiple nested entries correctly" do
|
69
|
+
xml = "<List><Item><Name>one</Name><Type>this</Type></Item><Item><Name>two</Name><Type>this</Type></Item><Item><Name>three</Name><Type>this</Type></Item></List>"
|
70
|
+
h = Hash.from_xml_string(xml)
|
71
|
+
list = h['List']
|
72
|
+
list.has_key?('Item').should be_true
|
73
|
+
list['Item'].length.should == 3
|
74
|
+
end
|
75
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/string_spec.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe "String monkey: hex" do
|
4
|
+
it "shouldn't accept shitty args" do
|
5
|
+
expect { String.hex(:length => 100, :case => :upper, :bad => 'yep') }.to raise_error
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should return an upper case hex string" do
|
9
|
+
s = String.hex(:length => 100, :case => :upper)
|
10
|
+
(s =~ /^[abcdef0-9]+$/).should be_nil
|
11
|
+
(s =~ /^[ABCDEF0-9]+$/).should >= 0
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should return a lower case hex string" do
|
15
|
+
s = String.hex(:length => 100, :case => :lower)
|
16
|
+
(s =~ /^[abcdef0-9]+$/).should >= 0
|
17
|
+
(s =~ /^[ABCDEF0-9]+$/).should be_nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "String monkey: random" do
|
22
|
+
it "should return an exception if an invalid option is passed" do
|
23
|
+
expect { String.random(:length => 100, :bad => 'yep') }.to raise_error
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return a alpha only string" do
|
27
|
+
s = String.random(:length => 100, :charset => :alpha)
|
28
|
+
(s =~ /^[a-zA-Z]+$/).should >= 0
|
29
|
+
(s =~ /^[0-9]+$/).should be_nil
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return a uppercase alphanumeric only string" do
|
33
|
+
s = String.random(:length => 100, :charset => :alnum_upper)
|
34
|
+
(s =~ /^[0-9A-Z]+$/).should >= 0
|
35
|
+
(s =~ /^[a-z]+$/).should be_nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "String monkey: camalize" do
|
40
|
+
it "should camelize a string" do
|
41
|
+
c = "hello_world"
|
42
|
+
c.camelize.should == "HelloWorld"
|
43
|
+
end
|
44
|
+
end
|
data/spec/time_spec.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe "Time monkey: " do
|
4
|
+
it "to_z should return the correct value" do
|
5
|
+
# Converts to gmt
|
6
|
+
Time.stubs(:now).returns(Time.mktime(1970,1,1,0,0,0))
|
7
|
+
Time.now.to_z.should == "1970-01-01T08:00:00.000Z"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "to_web should return the correct value" do
|
11
|
+
# Converts to gmt
|
12
|
+
Time.stubs(:now).returns(Time.mktime(1970,1,1,0,0,0))
|
13
|
+
Time.now.to_web.should == "Thu, 01 Jan 1970 08:00:00 +0000"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "to_yearmonth should return the correct value" do
|
17
|
+
# Converts to gmt
|
18
|
+
Time.stubs(:now).returns(Time.mktime(1970,1,1,0,0,0))
|
19
|
+
Time.now.to_short.should == "197001"
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hosemonkey
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rob Lyon
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: mocha
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: nokogiri
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Write a gem description
|
79
|
+
email:
|
80
|
+
- rlyon@uidaho.edu
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- README
|
88
|
+
- Rakefile
|
89
|
+
- hosemonkey.gemspec
|
90
|
+
- lib/hosemonkey.rb
|
91
|
+
- lib/hosemonkey/ext/array.rb
|
92
|
+
- lib/hosemonkey/ext/boolean.rb
|
93
|
+
- lib/hosemonkey/ext/date.rb
|
94
|
+
- lib/hosemonkey/ext/fixnum.rb
|
95
|
+
- lib/hosemonkey/ext/hash.rb
|
96
|
+
- lib/hosemonkey/ext/string.rb
|
97
|
+
- lib/hosemonkey/ext/time.rb
|
98
|
+
- lib/hosemonkey/version.rb
|
99
|
+
- spec/fixnum_spec.rb
|
100
|
+
- spec/hash_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
- spec/string_spec.rb
|
103
|
+
- spec/time_spec.rb
|
104
|
+
homepage: ''
|
105
|
+
licenses: []
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
118
|
+
requirements:
|
119
|
+
- - ! '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project: hosemonkey
|
124
|
+
rubygems_version: 1.8.20
|
125
|
+
signing_key:
|
126
|
+
specification_version: 3
|
127
|
+
summary: Write a gem summary
|
128
|
+
test_files:
|
129
|
+
- spec/fixnum_spec.rb
|
130
|
+
- spec/hash_spec.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
- spec/string_spec.rb
|
133
|
+
- spec/time_spec.rb
|