Module: Doh

Defined in:
lib/doh/home.rb,
lib/doh/util/jsval.rb,
lib/doh/util/xml_util.rb,
lib/doh/util/file_edit.rb,
lib/doh/util/post_hash.rb,
lib/doh/util/source_ip.rb,
lib/doh/util/time_util.rb,
lib/doh/util/doh_socket.rb,
lib/doh/util/to_display.rb,
lib/doh/logger/interface.rb,
lib/doh/util/http_helper.rb,
lib/doh/util/internal_ip.rb,
lib/doh/util/num_or_self.rb,
lib/doh/util/current_date.rb,
lib/doh/util/class_basename.rb

Defined Under Namespace

Classes: DohSocket, FileEdit, HttpHelper, TimeoutError

Constant Summary collapse

@@source_ip =
nil
@@logger_interface =
DohLogger::NullInterface.new
@@save_logger_interface =
nil
@@current_date_objs =
[]

Class Method Summary collapse

Class Method Details

.class_basename(klass) ⇒ Object



5
6
7
8
# File 'lib/doh/util/class_basename.rb', line 5

def self.class_basename(klass)
  full_name = klass.to_s
  full_name.rafter('::') || full_name
end

.clear_current_dateObject



27
28
29
# File 'lib/doh/util/current_date.rb', line 27

def self.clear_current_date
  @@current_date_objs.clear
end

.current_date(dflt = Date.today) ⇒ Object



7
8
9
10
# File 'lib/doh/util/current_date.rb', line 7

def self.current_date(dflt = Date.today)
  return dflt if @@current_date_objs.empty?
  @@current_date_objs.last.date_only
end

.current_datetime(dflt = DateTime.zow) ⇒ Object



12
13
14
15
16
17
# File 'lib/doh/util/current_date.rb', line 12

def self.current_datetime(dflt = DateTime.zow)
  return dflt if @@current_date_objs.empty?
  cdo = @@current_date_objs.last
  return cdo if cdo.respond_to?(:hour)
  DateTime.new(cdo.year, cdo.month, cdo.day, dflt.hour, dflt.min, dflt.sec, dflt.zone)
end

.date_as(date_obj, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/doh/util/current_date.rb', line 31

def self.date_as(date_obj, &block)
  push_current_date(date_obj)
  begin
    retval = block.call
  rescue
    raise
  ensure
    pop_current_date
  end
  retval
end

.disable_loggingObject



15
16
17
18
19
# File 'lib/doh/logger/interface.rb', line 15

def self.disable_logging
  return if @@logger_interface.is_a?(DohLogger::NullInterface)
  @@save_logger_interface = @@logger_interface
  @@logger_interface = DohLogger::NullInterface.new
end

.display_phone(str) ⇒ Object



72
73
74
75
# File 'lib/doh/util/to_display.rb', line 72

def self.display_phone(str)
  return str unless str.size == 10
  str[0..2] + '-' + str[3..5] + '-' + str[6..9]
end

.display_postal(str) ⇒ Object



77
78
79
80
# File 'lib/doh/util/to_display.rb', line 77

def self.display_postal(str)
  return str unless str.size == 9
  return str[0..4] + '-' + str[5..8]
end

.display_ssn(str) ⇒ Object



82
83
84
85
# File 'lib/doh/util/to_display.rb', line 82

def self.display_ssn(str)
  return str unless str.size == 9
  str[0..2] + '-' + str[3..4] + '-' + str[5..8]
end

.elem_to_hash(elem) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/doh/util/xml_util.rb', line 35

def self.elem_to_hash(elem)
  return {} if !elem.is_a?(REXML::Element)
  if elem.has_elements?
    children_hash = {}
    elem.children.each do |child|
      children_hash.merge!(elem_to_hash(child))
    end
    {elem.name => children_hash}
  else
    {elem.name => elem.text}
  end
end

.elem_to_xml(elem, node) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/doh/util/xml_util.rb', line 15

def self.elem_to_xml(elem, node)
  elem.each_pair do |field, value|
    if value.class == Hash
      elem_to_xml(value, node.add_element(field))
    else
      node.add_element(field).add_text(value.to_s)
    end
  end
end

.enable_loggingObject



21
22
23
24
25
# File 'lib/doh/logger/interface.rb', line 21

def self.enable_logging
  return if @@save_logger_interface.nil? || @@save_logger_interface.is_a?(DohLogger::NullInterface)
  @@logger_interface = @@save_logger_interface
  @@save_logger_interface = nil
end

.find_file_in_parents(dir, filename, max_tries = 20) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/doh/home.rb', line 6

def self.find_file_in_parents(dir, filename, max_tries = 20)
  path = File.expand_path(dir)
  raise "unable to find #{filename.inspect}" if (path == '/') || (max_tries <= 0)

  if File.exist?(File.join(path, filename))
    return path
  else
    return find_file_in_parents(File.join(path, '..'), filename, max_tries - 1)
  end
end

.hash_to_xml(hash, root_node_name = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/doh/util/xml_util.rb', line 5

def self.hash_to_xml(hash, root_node_name = nil)
  doc = REXML::Document.new
  doc << REXML::XMLDecl.new
  if root_node_name
    doc = doc.add_element(root_node_name)
  end
  elem_to_xml(hash, doc)
  doc.to_s
end

.homeObject



2
3
4
# File 'lib/doh/home.rb', line 2

def self.home
  @@home ||= find_file_in_parents(File.dirname(__FILE__), 'dohapp_home')
end

.internal_ipObject



5
6
7
# File 'lib/doh/util/internal_ip.rb', line 5

def self.internal_ip
  @@internal_ip ||= Socket.getaddrinfo(Socket.gethostname, nil, Socket::AF_INET, Socket::SOCK_STREAM)[0][3]
end

.jsval(value) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/doh/util/jsval.rb', line 5

def self.jsval(value)
  if value.is_a?(Date)
    "new Date(#{value.year},#{value.month-1},#{value.mday})"
  else
    value
  end
end

.logObject



10
11
12
# File 'lib/doh/logger/interface.rb', line 10

def self.log
  @@logger_interface
end

.num_or_self(obj) ⇒ Object



3
4
5
6
7
8
# File 'lib/doh/util/num_or_self.rb', line 3

def self.num_or_self(obj)
  if obj.class == String && obj =~ /^\d+$/
    return obj.to_i
  end
  return obj
end

.pop_current_dateObject



23
24
25
# File 'lib/doh/util/current_date.rb', line 23

def self.pop_current_date
  @@current_date_objs.pop
end

.post_hash_as_xml(hash, base_node = nil, url = 'http://localhost:4000/') ⇒ Object



9
10
11
12
# File 'lib/doh/util/post_hash.rb', line 9

def self.post_hash_as_xml(hash, base_node = nil, url = 'http://localhost:4000/')
  helper = HttpHelper.new(url).xml
  helper.post("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>#{hash_to_xml(hash, base_node)}")
end

.push_current_date(date_obj) ⇒ Object



19
20
21
# File 'lib/doh/util/current_date.rb', line 19

def self.push_current_date(date_obj)
  @@current_date_objs.push(date_obj)
end

.seconds_to_days(seconds) ⇒ Object



9
10
11
# File 'lib/doh/util/time_util.rb', line 9

def self.seconds_to_days(seconds)
  seconds.to_f / DateTime::DOHRUBY_SECONDS_IN_DAY.to_f
end

.set_logger_interface(logger_interface) ⇒ Object



6
7
8
# File 'lib/doh/logger/interface.rb', line 6

def self.set_logger_interface(logger_interface)
  @@logger_interface = logger_interface
end

.set_source_ip(source_ip) ⇒ Object



8
9
10
# File 'lib/doh/util/source_ip.rb', line 8

def self.set_source_ip(source_ip)
  @@source_ip = source_ip
end

.source_ipObject



4
5
6
# File 'lib/doh/util/source_ip.rb', line 4

def self.source_ip
  @@source_ip ||= 'unknown'
end

.time_blockObject



3
4
5
6
7
# File 'lib/doh/util/time_util.rb', line 3

def self.time_block
  start_time = Time.now
  yield
  return Time.now - start_time
end

.xml_to_hash(xml) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/doh/util/xml_util.rb', line 25

def self.xml_to_hash(xml)
  doc = REXML::Document.new(xml)
  doc.children.each do |child|
    if child.respond_to?('name')
      return elem_to_hash(child)
    end
  end
  raise "unable to find root node in xml: #{xml.inspect}"
end