Class: Palmade::Tapsilog::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/palmade/tapsilog/utils.rb

Class Method Summary collapse

Class Method Details

.hash_to_query_string(hash) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/palmade/tapsilog/utils.rb', line 161

def self.hash_to_query_string(hash)
  # Check for frozenness
  if hash == nil
    return nil
  end
  if !hash.respond_to?(:to_hash)
    raise TypeError, "Can't convert #{hash.class} into Hash."
  end
  hash = hash.to_hash
  hash = hash.map do |key, value|
    key = key.to_s if key.kind_of?(Symbol)
    [key, value]
  end
  hash.sort! # Useful default for OAuth and caching

  # Algorithm shamelessly stolen from Julien Genestoux, slightly modified
  buffer = ""
  stack = []
  e = lambda do |component|
    CGI::escape(component.to_s)
  end
  hash.each do |key, value|
    if value.kind_of?(Hash)
      stack << [key, value]
    elsif value.kind_of?(Array)
      stack << [
        key,
        value.inject({}) { |accu, x| accu[accu.size.to_s] = x; accu }
      ]
    elsif value == true
      buffer << "#{e.call(key)}&"
    else
      buffer << "#{e.call(key)}=#{e.call(value)}&"
    end
  end
  stack.each do |(parent, hash)|
    (hash.sort_by { |key| key.to_s }).each do |(key, value)|
      if value.kind_of?(Hash)
        stack << ["#{parent}[#{key}]", value]
      elsif value == true
        buffer << "#{parent}[#{e.call(key)}]&"
      else
        buffer << "#{parent}[#{e.call(key)}]=#{e.call(value)}&"
      end
    end
  end
  buffer.chop
end

.is_port_open?(ip, port) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/palmade/tapsilog/utils.rb', line 15

def self.is_port_open?(ip, port)
  begin
    ::Timeout::timeout(1) do
      begin
        s = TCPSocket.new(ip, port)
        s.close
        return true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        return false
      end
    end
  rescue ::Timeout::Error
  end

  return false
end

.pidf_clean(pid_file) ⇒ Object



90
91
92
93
94
# File 'lib/palmade/tapsilog/utils.rb', line 90

def self.pidf_clean(pid_file)
  unless pidf_running?(pid_file)
    File.delete(pid_file) if File.exists?(pid_file)
  end
end

.pidf_force_kill(pid_file) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/palmade/tapsilog/utils.rb', line 80

def self.pidf_force_kill(pid_file)
  if pid = pidf_read(pid_file)
    Process.kill("KILL", pid)
    File.delete(pid_file) if File.exist?(pid_file)
    pid
  else
    nil
  end
end

.pidf_kill(pid_file, timeout = 30) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/palmade/tapsilog/utils.rb', line 54

def self.pidf_kill(pid_file, timeout = 30)
  if timeout == 0
    pidf_send_signal('INT', pid_file, timeout)
  else
    pidf_send_signal('QUIT', pid_file, timeout)
  end
end

.pidf_read(pid_file) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/palmade/tapsilog/utils.rb', line 46

def self.pidf_read(pid_file)
  if File.exists?(pid_file) && File.file?(pid_file) && pid = File.read(pid_file)
    pid.to_i
  else
    nil
  end
end

.pidf_running?(pid_file) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/palmade/tapsilog/utils.rb', line 38

def self.pidf_running?(pid_file)
  if pid = pidf_read(pid_file)
    process_running?(pid) ? pid : false
  else
    nil
  end
end

.pidf_send_signal(signal, pid_file, timeout = 30) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/palmade/tapsilog/utils.rb', line 62

def self.pidf_send_signal(signal, pid_file, timeout = 30)
  if pid = pidf_read(pid_file)
    Process.kill(signal, pid)
    ::Timeout.timeout(timeout) do
      sleep 0.1 while process_running?(pid)
    end
    pid
  else
    nil
  end
rescue ::Timeout::Error
  pidf_force_kill pid_file
rescue Interrupt
  pidf_force_kill pid_file
rescue Errno::ESRCH # No such process
  pidf_force_kill pid_file
end

.process_running?(pid) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/palmade/tapsilog/utils.rb', line 32

def self.process_running?(pid)
  Process.getpgid(pid) != -1
rescue Errno::ESRCH
  false
end

.query_string_to_hash(query_string, options = {}) ⇒ Object

Taken from github accumulator/uri



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/palmade/tapsilog/utils.rb', line 98

def self.query_string_to_hash(query_string, options={})
  defaults = {:notation => :subscript}
  options = defaults.merge(options)
  if ![:flat, :dot, :subscript].include?(options[:notation])
    raise ArgumentError,
      "Invalid notation. Must be one of: [:flat, :dot, :subscript]."
  end
  dehash = lambda do |hash|
    hash.each do |(key, value)|
      if value.kind_of?(Hash)
        hash[key] = dehash.call(value)
      end
    end
    if hash != {} && hash.keys.all? { |key| key =~ /^\d+$/ }
      hash.sort.inject([]) do |accu, (key, value)|
        accu << value; accu
      end
    else
      hash
    end
  end
  return nil if query_string == nil
  return ((query_string.split("&").map do |pair|
    pair.split("=", -1) if pair && pair != ""
  end).compact.inject({}) do |accumulator, (key, value)|
    value = true if value.nil?
    key = CGI::unescape(key)
    if value != true
      value = CGI::unescape(value).gsub(/\+/, " ")
    end
    if options[:notation] == :flat
      if accumulator[key]
        raise ArgumentError, "Key was repeated: #{key.inspect}"
      end
      accumulator[key] = value
    else
      if options[:notation] == :dot
        array_value = false
        subkeys = key.split(".")
      elsif options[:notation] == :subscript
        array_value = !!(key =~ /\[\]$/)
        subkeys = key.split(/[\[\]]+/)
      end
      current_hash = accumulator
      for i in 0...(subkeys.size - 1)
        subkey = subkeys[i]
        current_hash[subkey] = {} unless current_hash[subkey]
        current_hash = current_hash[subkey]
      end
      if array_value
        current_hash[subkeys.last] = [] unless current_hash[subkeys.last]
        current_hash[subkeys.last] << value
      else
        current_hash[subkeys.last] = value
      end
    end
    accumulator
  end).inject({}) do |accumulator, (key, value)|
    accumulator[key] = value.kind_of?(Hash) ? dehash.call(value) : value
    accumulator
  end
end

.symbolize_keys(hash) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/palmade/tapsilog/utils.rb', line 6

def self.symbolize_keys(hash)
  hash.inject({}){|result, (key, value)|
    new_key = key.kind_of?(String) ? key.to_sym : key
    new_value = value.kind_of?(Hash) ? symbolize_keys(value) : value
    result[new_key] = new_value
    result
  }
end