dohutil 0.1.3 → 0.1.4
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/MIT-LICENSE +1 -1
- data/lib/doh/app/common.rb +10 -0
- data/lib/doh/app/pwd.rb +4 -0
- data/lib/doh/array_to_hash.rb +12 -0
- data/lib/doh/class_basename.rb +10 -0
- data/lib/doh/core_ext/bigdecimal.rb +34 -0
- data/lib/doh/core_ext/class/force_deep_copy.rb +12 -0
- data/lib/doh/core_ext/date.rb +109 -0
- data/lib/doh/core_ext/dir.rb +7 -0
- data/lib/doh/core_ext/hash.rb +21 -0
- data/lib/doh/core_ext/kernel.rb +8 -0
- data/lib/doh/core_ext/string.rb +41 -0
- data/lib/doh/current_date.rb +43 -0
- data/lib/doh/http_helper.rb +108 -0
- data/lib/doh/log/stub.rb +2 -2
- data/lib/doh/root.rb +0 -1
- data/lib/doh/to_display.rb +86 -0
- metadata +17 -3
data/MIT-LICENSE
CHANGED
data/lib/doh/app/pwd.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
class Integer
|
4
|
+
def to_d
|
5
|
+
to_s.to_d
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class BigDecimal
|
10
|
+
alias :_original_to_s :to_s
|
11
|
+
def to_s(format = 'F')
|
12
|
+
_original_to_s(format)
|
13
|
+
end
|
14
|
+
|
15
|
+
undef inspect
|
16
|
+
def inspect
|
17
|
+
to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_d
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_dig(digits_after_decimal = 2)
|
25
|
+
raise ArgumentError.new("digits_after_decimal must be > 0") unless digits_after_decimal > 0
|
26
|
+
return '0.' + ('0' * digits_after_decimal) if nan? || infinite?
|
27
|
+
|
28
|
+
retval = truncate(digits_after_decimal).to_s
|
29
|
+
digits_needed = retval.index('.') + digits_after_decimal + 1 - retval.size
|
30
|
+
retval += ('0' * digits_needed) if digits_needed > 0
|
31
|
+
|
32
|
+
retval
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class Class
|
2
|
+
def force_deep_copy(*syms)
|
3
|
+
return if syms.empty?
|
4
|
+
code = "def initialize_copy(orig); "
|
5
|
+
code << "super(orig)\n "
|
6
|
+
syms.each do |elem|
|
7
|
+
code << "@#{elem} = @#{elem}.nil? ? nil : @#{elem}.dup\n "
|
8
|
+
end
|
9
|
+
code << "end\n"
|
10
|
+
class_eval(code)
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
class Date
|
5
|
+
def self.days_in_month(year, month)
|
6
|
+
civil(year, month, -1).mday
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.smart_parse(datestr)
|
10
|
+
if datestr =~ /\A(\d\d)\/(\d\d)\/(\d\d)\z/
|
11
|
+
month = $1.to_i
|
12
|
+
mday = $2.to_i
|
13
|
+
year = $3.to_i
|
14
|
+
result = Date.new(year, month, mday)
|
15
|
+
else
|
16
|
+
result = Date.parse(datestr)
|
17
|
+
end
|
18
|
+
|
19
|
+
if result.year < 50
|
20
|
+
result = Date.new(result.year + 2000, result.month, result.day)
|
21
|
+
elsif result.year < 100
|
22
|
+
result = Date.new(result.year + 1900, result.month, result.day)
|
23
|
+
end
|
24
|
+
result
|
25
|
+
end
|
26
|
+
|
27
|
+
undef inspect
|
28
|
+
def inspect
|
29
|
+
strftime('%F')
|
30
|
+
end
|
31
|
+
|
32
|
+
def weekday?
|
33
|
+
(wday > 0) && (wday < 6)
|
34
|
+
end
|
35
|
+
|
36
|
+
def date_only
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.short_weekday_to_num(weekday)
|
41
|
+
@@short_days_of_week ||= Time::RFC2822_DAY_NAME.collect {|day| day.downcase}
|
42
|
+
@@short_days_of_week.index(weekday.downcase)
|
43
|
+
end
|
44
|
+
|
45
|
+
def years_since(date)
|
46
|
+
years_diff = year - date.year
|
47
|
+
if month < date.month || (month == date.month && day < date.day)
|
48
|
+
years_diff -= 1
|
49
|
+
end
|
50
|
+
years_diff
|
51
|
+
end
|
52
|
+
|
53
|
+
def make_datetime(hour = 0, min = 0, sec = 0)
|
54
|
+
DateTime.new(year, month, mday, hour, min, sec)
|
55
|
+
end
|
56
|
+
|
57
|
+
def add_months(months, new_day = nil)
|
58
|
+
total_month = month + months
|
59
|
+
new_month = total_month % 12
|
60
|
+
new_month = 12 if new_month == 0
|
61
|
+
new_year = year + ((total_month - 1) / 12)
|
62
|
+
calc_day = [mday, Date.days_in_month(new_year, new_month)].min
|
63
|
+
Date.new(new_year, new_month, new_day || calc_day)
|
64
|
+
end
|
65
|
+
|
66
|
+
def add_years(years)
|
67
|
+
new_year = year + years
|
68
|
+
calc_day = [mday, Date.days_in_month(new_year, month)].min
|
69
|
+
Date.new(new_year, month, calc_day)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class DateTime
|
74
|
+
DOHRUBY_SECONDS_IN_DAY = (24 * 60 * 60).freeze
|
75
|
+
|
76
|
+
def self.seconds_to_days(seconds)
|
77
|
+
seconds.to_f / DOHRUBY_SECONDS_IN_DAY.to_f
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.zow
|
81
|
+
obj = now
|
82
|
+
new(obj.year, obj.month, obj.mday, obj.hour, obj.min, obj.sec)
|
83
|
+
end
|
84
|
+
|
85
|
+
def inspect
|
86
|
+
strftime('%F %X')
|
87
|
+
end
|
88
|
+
|
89
|
+
def date_only
|
90
|
+
Date.new(year, month, mday)
|
91
|
+
end
|
92
|
+
|
93
|
+
def add_seconds(seconds)
|
94
|
+
self + Rational(seconds, DOHRUBY_SECONDS_IN_DAY)
|
95
|
+
end
|
96
|
+
|
97
|
+
def sub_seconds(seconds)
|
98
|
+
add_seconds(-seconds)
|
99
|
+
end
|
100
|
+
|
101
|
+
# subtract another DateTime object, return difference in seconds
|
102
|
+
def sub_dt(other)
|
103
|
+
((self - other) * DOHRUBY_SECONDS_IN_DAY).to_i
|
104
|
+
end
|
105
|
+
|
106
|
+
def make_datetime
|
107
|
+
self
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Hash
|
2
|
+
def to_h
|
3
|
+
self
|
4
|
+
end
|
5
|
+
|
6
|
+
def setnew(key, value)
|
7
|
+
store(key, value) unless key?(key)
|
8
|
+
end
|
9
|
+
|
10
|
+
def merge_with_remove(hash)
|
11
|
+
self.dup.merge_with_remove!(hash)
|
12
|
+
end
|
13
|
+
|
14
|
+
#merges the given hash with the current
|
15
|
+
#then removes any elements from the merged hash that have
|
16
|
+
#a value of nil in the hash argument
|
17
|
+
def merge_with_remove!(hash)
|
18
|
+
self.merge!(hash).reject! {|elem, value| hash.key?(elem) && hash[elem].nil?}
|
19
|
+
self
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class String
|
2
|
+
def firstn(limit)
|
3
|
+
return nil if limit < 0
|
4
|
+
return '' if limit == 0
|
5
|
+
slice(Range.new(0, limit -1))
|
6
|
+
end
|
7
|
+
|
8
|
+
def lastn(limit)
|
9
|
+
return nil if limit < 0
|
10
|
+
return '' if limit == 0
|
11
|
+
slice(Range.new(-limit, -1)) || self
|
12
|
+
end
|
13
|
+
|
14
|
+
def mid(first, count = nil)
|
15
|
+
slice(first, count || (size - first))
|
16
|
+
end
|
17
|
+
|
18
|
+
def after(substr)
|
19
|
+
loc = index(substr)
|
20
|
+
return nil if loc.nil?
|
21
|
+
mid(loc + substr.size)
|
22
|
+
end
|
23
|
+
|
24
|
+
def rafter(substr)
|
25
|
+
loc = rindex(substr)
|
26
|
+
return nil if loc.nil?
|
27
|
+
mid(loc + substr.size)
|
28
|
+
end
|
29
|
+
|
30
|
+
def before(substr)
|
31
|
+
loc = index(substr)
|
32
|
+
return nil if loc.nil?
|
33
|
+
firstn(loc)
|
34
|
+
end
|
35
|
+
|
36
|
+
def rbefore(substr)
|
37
|
+
loc = rindex(substr)
|
38
|
+
return nil if loc.nil?
|
39
|
+
firstn(loc)
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'doh/core_ext/date'
|
2
|
+
|
3
|
+
module Doh
|
4
|
+
|
5
|
+
@@current_date_objs = []
|
6
|
+
|
7
|
+
def self.current_date(dflt = Date.today)
|
8
|
+
return dflt if @@current_date_objs.empty?
|
9
|
+
@@current_date_objs.last.date_only
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.current_datetime(dflt = DateTime.zow)
|
13
|
+
return dflt if @@current_date_objs.empty?
|
14
|
+
cdo = @@current_date_objs.last
|
15
|
+
return cdo if cdo.respond_to?(:hour)
|
16
|
+
DateTime.new(cdo.year, cdo.month, cdo.day, dflt.hour, dflt.min, dflt.sec, dflt.zone)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.push_current_date(date_obj)
|
20
|
+
@@current_date_objs.push(date_obj)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.pop_current_date
|
24
|
+
@@current_date_objs.pop
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.clear_current_date
|
28
|
+
@@current_date_objs.clear
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.date_as(date_obj, &block)
|
32
|
+
push_current_date(date_obj)
|
33
|
+
begin
|
34
|
+
retval = block.call
|
35
|
+
rescue
|
36
|
+
raise
|
37
|
+
ensure
|
38
|
+
pop_current_date
|
39
|
+
end
|
40
|
+
retval
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/https'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
module Doh
|
6
|
+
|
7
|
+
class HttpHelper
|
8
|
+
attr_reader :headers
|
9
|
+
|
10
|
+
def initialize(url)
|
11
|
+
@url = url
|
12
|
+
@headers = {}
|
13
|
+
init_sender
|
14
|
+
end
|
15
|
+
|
16
|
+
def post(data, get_body_only = true)
|
17
|
+
dohlog.debug("posting to url #@url data: #{data.inspect}")
|
18
|
+
response = if data.is_a?(Hash)
|
19
|
+
post_hash(data)
|
20
|
+
else
|
21
|
+
post_string(data)
|
22
|
+
end
|
23
|
+
check_for_errors(response)
|
24
|
+
return response.body.to_s if get_body_only
|
25
|
+
response
|
26
|
+
end
|
27
|
+
|
28
|
+
def get(get_body_only = true, allow_redirect = false, num_redirects = 0)
|
29
|
+
getstr = @parsed_url.path
|
30
|
+
getstr += '?' + @parsed_url.query unless @parsed_url.query.to_s.empty?
|
31
|
+
request = Net::HTTP::Get.new(getstr)
|
32
|
+
request.initialize_http_header(@headers)
|
33
|
+
response = @sender.start { |http| http.request(request) }
|
34
|
+
if response.is_a?(Net::HTTPRedirection) && allow_redirect
|
35
|
+
raise "too many redirects" if num_redirects > 20
|
36
|
+
@url = response['location']
|
37
|
+
init_sender
|
38
|
+
return get(get_body_only, true, num_redirects + 1)
|
39
|
+
end
|
40
|
+
return response.body.to_s if get_body_only
|
41
|
+
response
|
42
|
+
end
|
43
|
+
|
44
|
+
def xml
|
45
|
+
@headers['Content-Type'] = 'text/xml'
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
def soap_action(action)
|
50
|
+
@headers['SOAPAction'] = action
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
def nossl
|
55
|
+
@sender.use_ssl = false
|
56
|
+
@nossl = true
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
def timeout(seconds)
|
61
|
+
@sender.read_timeout = seconds
|
62
|
+
self
|
63
|
+
end
|
64
|
+
|
65
|
+
def hdr(name, value)
|
66
|
+
@headers[name] = value
|
67
|
+
self
|
68
|
+
end
|
69
|
+
|
70
|
+
#shortcut method
|
71
|
+
def self.load(url, use_ssl = true, encode_url = false)
|
72
|
+
url = URI.escape(url) if encode_url
|
73
|
+
helper = HttpHelper.new(url)
|
74
|
+
helper.nossl if !use_ssl
|
75
|
+
helper.get
|
76
|
+
end
|
77
|
+
private
|
78
|
+
def init_sender
|
79
|
+
@parsed_url = URI.parse(@url)
|
80
|
+
@sender = Net::HTTP.new(@parsed_url.host, @parsed_url.port)
|
81
|
+
@sender.use_ssl = true unless @nossl || (['127.0.0.1', 'localhost'].include?(@parsed_url.host) && @parsed_url.port != 443)
|
82
|
+
@sender.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
83
|
+
end
|
84
|
+
|
85
|
+
def post_hash(data)
|
86
|
+
request = Net::HTTP::Post.new(@parsed_url.path)
|
87
|
+
request.set_form_data(data)
|
88
|
+
request.initialize_http_header(@headers)
|
89
|
+
@sender.start { |http| http.request(request) }
|
90
|
+
end
|
91
|
+
|
92
|
+
def post_string(data)
|
93
|
+
@sender.post(@parsed_url.path, data, @headers)
|
94
|
+
end
|
95
|
+
|
96
|
+
def check_for_errors(response)
|
97
|
+
# TODO: do something useful here
|
98
|
+
# case response
|
99
|
+
# when Net::HTTPSuccess
|
100
|
+
# puts "success"
|
101
|
+
# puts response.body.to_s
|
102
|
+
# else
|
103
|
+
# puts response.error!
|
104
|
+
# end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
data/lib/doh/log/stub.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
class Object
|
2
|
-
unless
|
2
|
+
unless method_defined?(:dohlog)
|
3
3
|
class DohlogStubInterface
|
4
4
|
def self.debug(msg); end
|
5
5
|
def self.info(msg); end
|
6
|
-
def self.
|
6
|
+
def self.warn(msg); end
|
7
7
|
def self.error(msg, exception = nil); end
|
8
8
|
def self.fatal(msg, exception = nil); end
|
9
9
|
end
|
data/lib/doh/root.rb
CHANGED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'doh/core_ext/bigdecimal'
|
2
|
+
|
3
|
+
class Object
|
4
|
+
def to_display
|
5
|
+
to_s
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class DateTime
|
10
|
+
def to_display
|
11
|
+
strftime('%m/%d/%Y %I:%M%P')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Date
|
16
|
+
def to_display
|
17
|
+
strftime('%m/%d/%Y')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class TrueClass
|
22
|
+
def to_display
|
23
|
+
'yes'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class FalseClass
|
28
|
+
def to_display
|
29
|
+
'no'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class BigDecimal
|
34
|
+
def to_display
|
35
|
+
'$' + to_dig(2)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class PhoneDisplayString < String
|
40
|
+
def to_display
|
41
|
+
Doh::display_phone(self)
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_s
|
45
|
+
self
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class PostalDisplayString < String
|
50
|
+
def to_display
|
51
|
+
Doh::display_postal(self)
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_s
|
55
|
+
self
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class SsnDisplayString < String
|
60
|
+
def to_display
|
61
|
+
Doh::display_ssn(self)
|
62
|
+
end
|
63
|
+
|
64
|
+
def to_s
|
65
|
+
self
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
module Doh
|
70
|
+
|
71
|
+
def self.display_phone(str)
|
72
|
+
return str unless str.size == 10
|
73
|
+
str[0..2] + '-' + str[3..5] + '-' + str[6..9]
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.display_postal(str)
|
77
|
+
return str unless str.size == 9
|
78
|
+
return str[0..4] + '-' + str[5..8]
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.display_ssn(str)
|
82
|
+
return str unless str.size == 9
|
83
|
+
str[0..2] + '-' + str[3..4] + '-' + str[5..8]
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dohutil
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-02-09 00:00:00.000000000Z
|
14
14
|
dependencies: []
|
15
15
|
description: Tiny utilities, packaged together so they don't each have their own gem.
|
16
16
|
email:
|
@@ -20,12 +20,26 @@ extensions: []
|
|
20
20
|
extra_rdoc_files:
|
21
21
|
- MIT-LICENSE
|
22
22
|
files:
|
23
|
+
- lib/doh/app/common.rb
|
24
|
+
- lib/doh/app/pwd.rb
|
25
|
+
- lib/doh/array_to_hash.rb
|
26
|
+
- lib/doh/class_basename.rb
|
23
27
|
- lib/doh/config.rb
|
28
|
+
- lib/doh/core_ext/bigdecimal.rb
|
29
|
+
- lib/doh/core_ext/class/force_deep_copy.rb
|
30
|
+
- lib/doh/core_ext/date.rb
|
31
|
+
- lib/doh/core_ext/dir.rb
|
32
|
+
- lib/doh/core_ext/hash.rb
|
33
|
+
- lib/doh/core_ext/kernel.rb
|
34
|
+
- lib/doh/core_ext/string.rb
|
35
|
+
- lib/doh/current_date.rb
|
24
36
|
- lib/doh/env.rb
|
25
37
|
- lib/doh/findup.rb
|
38
|
+
- lib/doh/http_helper.rb
|
26
39
|
- lib/doh/log/stub.rb
|
27
40
|
- lib/doh/options.rb
|
28
41
|
- lib/doh/root.rb
|
42
|
+
- lib/doh/to_display.rb
|
29
43
|
- MIT-LICENSE
|
30
44
|
homepage: https://github.com/atpsoft/dohutil
|
31
45
|
licenses:
|
@@ -48,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
62
|
version: '0'
|
49
63
|
requirements: []
|
50
64
|
rubyforge_project:
|
51
|
-
rubygems_version: 1.8.
|
65
|
+
rubygems_version: 1.8.15
|
52
66
|
signing_key:
|
53
67
|
specification_version: 3
|
54
68
|
summary: assorted tiny utilities
|