Class: IRC::Server::Modules::TinyURL
- Inherits:
-
IRC::Server::Module
- Object
- IRC::Server::Module
- IRC::Server::Modules::TinyURL
- Defined in:
- lib/failirc/server/modules/TinyURL.rb
Instance Attribute Summary
Attributes inherited from IRC::Server::Module
Instance Method Summary collapse
-
#initialize(server) ⇒ TinyURL
constructor
A new instance of TinyURL.
- #rehash ⇒ Object
- #tinyurl(chain, fromRef, toRef, message, level = nil) ⇒ Object
- #tinyurlify(url) ⇒ Object
Methods inherited from IRC::Server::Module
Constructor Details
Instance Method Details
#rehash ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/failirc/server/modules/TinyURL.rb', line 44 def rehash if tmp = @server.config.elements['config/modules/module[@name="TinyURL"]/length'] @length = tmp.text.to_i else @length = 42 end if tmp = @server.config.elements['config/modules/module[@name="TinyURL"]/timeout'] @timeout = tmp.text.to_i else @timeout = 5 end end |
#tinyurl(chain, fromRef, toRef, message, level = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/failirc/server/modules/TinyURL.rb', line 58 def tinyurl (chain, fromRef, toRef, , level=nil) if chain != :input return end from = fromRef.value to = toRef.value URI.extract().each {|uri| if uri.length <= @length next end if tiny = tinyurlify(uri) rescue nil .gsub!(/#{Regexp.escape(uri)}/, tiny) end } end |
#tinyurlify(url) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/failirc/server/modules/TinyURL.rb', line 77 def tinyurlify (url) begin content = timeout @timeout do Net::HTTP.post_form(URI.parse('http://tinyurl.com/create.php'), { 'url' => url }).body end match = content.match('<blockquote><b>(http://tinyurl.com/\w+)</b>') if match return match[1] end rescue Timeout::Error return nil end end |