Module: ModSpox::Helpers
- Defined in:
- lib/mod_spox/Helpers.rb
Class Method Summary collapse
-
.find_model(string, create = true) ⇒ Object
- string
-
name of target Locates target model and returns it.
-
.format_seconds(secs) ⇒ Object
- secs
-
number of seconds Converts seconds into a human readable string.
- .initialize_caches ⇒ Object
-
.safe_exec(command, timeout = 10) ⇒ Object
- command
- command to execute timeout
-
maximum number of seconds to run Execute a system command (use with care).
-
.tinyurl(url) ⇒ Object
- url
-
URL to shorten Gets a tinyurl for given URL.
Class Method Details
.find_model(string, create = true) ⇒ Object
- string
-
name of target
Locates target model and returns it. String can be a nick or channel name. If the string given does not match the required pattern for a channel or nick, the string is returned.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/mod_spox/Helpers.rb', line 68 def Helpers.find_model(string, create=true) Helpers.initialize_caches if(string =~ /^[A-Za-z\|\\\{\}\[\]\^\`~\_\-]+[A-Za-z0-9\|\\\{\}\[\]\^\`~\_\-]*$/) Logger.log("Model: #{string} -> Nick", 30) nick = nil if(@@nick_cache.has_key?(string.downcase.to_sym)) begin nick = Models::Nick[@@nick_cache[string.downcase.to_sym]] Logger.log("Handler cache hit for nick: #{string}", 30) if(nick.nick.downcase != string.downcase) Logger.log("Nick returned from cache invalid. Expecting #{string} but got #{nick.nick}", 30) nick = nil end rescue Object => boom Logger.log("Failed to grab cached nick: #{boom}", 30) end end unless(nick) nick = Models::Nick.locate(string, create) @@nick_cache[string.downcase.to_sym] = nick.pk if nick.is_a?(Models::Nick) Logger.log("Nick was retrieved from database", 30) end return nick elsif(string =~ /^[&#+!]/) Logger.log("Model: #{string} -> Channel", 30) if(@@channel_cache.has_key?(string.downcase.to_sym)) begin channel = Models::Channel[@@channel_cache[string.downcase.to_sym]] Logger.log("Handler cache hit for channel: #{string}", 30) if(string.downcase != channel.name.downcase) Logger.log("Channel returned from cache invalid. Expecting #{string} but got #{channel.name}", 30) channel = nil end rescue Object => boom Logger.log("Failed to grab cached channel: #{boom}", 30) end end unless(channel) channel = Models::Channel.locate(string, create) @@channel_cache[string.downcase.to_sym] = channel.pk if channel.is_a?(Models::Channel) Logger.log("Channel was retrieved from database", 30) end return channel elsif(model = Models::Server.filter(:host => string, :connected => true).first) Logger.log("Model: #{string} -> Server", 30) return model else Logger.log("FAIL Model: #{string} -> No match", 30) return string end end |
.format_seconds(secs) ⇒ Object
- secs
-
number of seconds
Converts seconds into a human readable string
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/mod_spox/Helpers.rb', line 13 def Helpers.format_seconds(secs) str = [] d = (secs / 86400).to_i secs = secs % 86400 h = (secs / 3600).to_i secs = secs % 3600 m = (secs / 60).to_i secs = secs % 60 {:day => d, :hour => h, :minute => m, :second => secs}.each_pair do |type, value| if(value > 0) str << "#{value} #{type}#{value == 1 ? '':'s'}" end end return str.join(' ') end |
.initialize_caches ⇒ Object
120 121 122 123 |
# File 'lib/mod_spox/Helpers.rb', line 120 def Helpers.initialize_caches @@nick_cache = Cache.new(20) unless Helpers.class_variable_defined?(:@@nick_cache) @@channel_cache = Cache.new(5) unless Helpers.class_variable_defined?(:@@channel_cache) end |
.safe_exec(command, timeout = 10) ⇒ Object
- command
-
command to execute
- timeout
-
maximum number of seconds to run
Execute a system command (use with care)
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mod_spox/Helpers.rb', line 32 def Helpers.safe_exec(command, timeout=10) begin Timeout::timeout(timeout) do result = `#{command}` end rescue Timeout::Error => boom Logger.log("Command execution exceeded allowed time (command: #{command} | timeout: #{timeout})") rescue Object => boom Logger.log("Command generated an exception (command: #{command} | error: #{boom})") end end |
.tinyurl(url) ⇒ Object
- url
-
URL to shorten
Gets a tinyurl for given URL
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mod_spox/Helpers.rb', line 46 def Helpers.tinyurl(url) begin connection = Net::HTTP.new('tinyurl.com', 80) resp, data = connection.get("/create.php?url=#{url}", nil) if(resp.code !~ /^200$/) raise "Failed to make the URL small." end data.gsub!(/[\n\r]/, '') if(data =~ /<input type=hidden name=tinyurl value="(.+?)">/) return $1 else raise "Failed to locate the small URL." end rescue Object => boom raise "Failed to process URL. #{boom}" end end |