Module: ModSpox::Helpers
- Defined in:
- lib/mod_spox/Helpers.rb
Constant Summary collapse
- Suff =
- bytes
-
number of bytes
Converts bytes into easy human readable form O(1) version by Ryan “pizza_milkshake” Flynn
[ "", # 1024^0 "Kilo", # 1024^1 "Mega", # 1024^2 "Giga", # 1024^3 "Tera", # 1024^4 "Peta", # 1024^5 "Exa", # 1024^6 "Zetta", # 1024^7 "Yotta" # 1024^8 ]
Class Method Summary collapse
-
.convert_entities(string) ⇒ Object
- string
-
string to convert Converts HTML entities found in a string.
-
.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.
- .format_size(bytes) ⇒ 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
.convert_entities(string) ⇒ Object
- string
-
string to convert
Converts HTML entities found in a string
105 106 107 108 109 110 111 112 113 |
# File 'lib/mod_spox/Helpers.rb', line 105 def Helpers.convert_entities(string) begin require 'htmlentities' @@coder = HTMLEntities.new unless Helpers.class_variable_defined?(:@@coder) return @@coder.decode(string) rescue Object return string end end |
.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.
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/mod_spox/Helpers.rb', line 89 def Helpers.find_model(string, create=true) result = nil if(string =~ /^[A-Za-z\|\\\{\}\[\]\^\`~\_\-]+[A-Za-z0-9\|\\\{\}\[\]\^\`~\_\-]*$/) result = Models::Nick.find_or_create(:nick => string.downcase) elsif(['&', '#', '+', '!'].include?(string[0])) result = Models::Channel.find_or_create(:name => string.downcase) elsif(Models::Server.filter(:host => string, :connected => true).count > 0) result = Models::Server.filter(:host => string, :connected => true).first else Logger.warn("Failed to match string to model: #{string} -> No match") end return result 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 28 29 30 |
# File 'lib/mod_spox/Helpers.rb', line 13 def Helpers.format_seconds(secs) arg = {:year => 29030400, :month => 2419200, :week => 604800, :day => 86400, :hour => 3600, :minute => 60, :second => 1} res = '' arg.each_pair do |k,v| z = (secs / v).to_i next unless z > 0 res += " #{z} #{k}#{z == 1 ? '':'s'}" secs = secs % v end res = '0 seconds' if res.empty? return res.strip end |
.format_size(bytes) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/mod_spox/Helpers.rb', line 46 def Helpers.format_size(bytes) mag = (Math.log(bytes) / Math.log(1024)).floor mag = [ Suff.length - 1, mag ].min val = bytes.to_f / (1024 ** mag) "%7.3f %sbyte%s" % [ val, Suff[mag], val == 1 ? "" : "s" ] 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)
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mod_spox/Helpers.rb', line 56 def Helpers.safe_exec(command, timeout=10) begin Timeout::timeout(timeout) do result = `#{command}` end rescue Timeout::Error => boom Logger.warn("Command execution exceeded allowed time (command: #{command} | timeout: #{timeout})") raise boom rescue Object => boom Logger.warn("Command generated an exception (command: #{command} | error: #{boom})") raise boom end end |
.tinyurl(url) ⇒ Object
- url
-
URL to shorten
Gets a tinyurl for given URL
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/mod_spox/Helpers.rb', line 72 def Helpers.tinyurl(url) begin connection = Net::HTTP.new('tinyurl.com', 80) resp, data = connection.get("/api-create.php?url=#{url}") if(resp.code !~ /^200$/) raise "Failed to make the URL small." end return data.strip rescue Object => boom raise "Failed to process URL. #{boom}" end end |