summaryrefslogtreecommitdiff
path: root/lib/rbot/utils.rb
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2006-10-15 20:38:25 +0000
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2006-10-15 20:38:25 +0000
commitb50bc708c4be90d85328d52ba23755a58f9b59a4 (patch)
tree03b16436ebb70c35d2543e3010f267eab4b35f7c /lib/rbot/utils.rb
parentb26ebd542b1b8ff5168636851763b09ad5051b19 (diff)
Utils.safe_save does a 'safe' saving (save to tempfile, move tempfile to new location)
Diffstat (limited to 'lib/rbot/utils.rb')
-rw-r--r--lib/rbot/utils.rb22
1 files changed, 20 insertions, 2 deletions
diff --git a/lib/rbot/utils.rb b/lib/rbot/utils.rb
index eabe5c17..ae45e5d5 100644
--- a/lib/rbot/utils.rb
+++ b/lib/rbot/utils.rb
@@ -1,10 +1,14 @@
require 'net/http'
require 'uri'
+require 'tempfile'
+
begin
$we_have_html_entities_decoder = require 'htmlentities'
rescue LoadError
$we_have_html_entities_decoder = false
- UNESCAPE_TABLE = {
+ module Irc
+ module Utils
+ UNESCAPE_TABLE = {
'raquo' => '>>',
'quot' => '"',
'apos' => '\'',
@@ -268,9 +272,12 @@ rescue LoadError
'sigma' => '&#963;',
'oacute' => '\xf3',
=end
- }
+ }
+ end
+ end
end
+
module Irc
# miscellaneous useful functions
@@ -312,6 +319,17 @@ module Irc
}
end
+
+ def Utils.safe_save(file)
+ basename = File.basename(file)
+ temp = Tempfile.new(basename)
+ temp.binmode
+ yield temp if block_given?
+ temp.close
+ File.rename(temp.path, file)
+ end
+
+
# returns a string containing the result of an HTTP GET on the uri
def Utils.http_get(uristr, readtimeout=8, opentimeout=4)