Module: Cinch::Toolbox
- Defined in:
- lib/cinch/toolbox.rb,
lib/cinch/toolbox/version.rb
Overview
Version Info
Constant Summary collapse
- VERSION =
'1.1.7'
Class Method Summary collapse
-
.expand(url) ⇒ String
Expand a previously shortened URL via the configured shortener.
-
.extract_url(url) ⇒ URI
Extract the first url from a string.
-
.extract_urls(url) ⇒ Array
Extract the urls from a string.
-
.get_html_element(url, selector, mode = :css) ⇒ String
Get an element of the supplied website.
-
.get_page_title(url) ⇒ String
Get the title of a given web page.
-
.sent_via_private_message?(m, error = nil) ⇒ Boolean
Simple check to make sure people are using a command via the main channel for plugins that require a channel for context.
-
.shorten(url) ⇒ String
Shorten a URL via the configured shortener.
-
.time_format(secs, units = nil, format = :long) ⇒ Object
Used to render a period of time in a uniform string.
-
.truncate(text, length = 250) ⇒ Object
Truncate a given block of text, used for making sure the bot doesn’t flood.
Class Method Details
.expand(url) ⇒ String
Expand a previously shortened URL via the configured shortener
76 77 78 79 80 |
# File 'lib/cinch/toolbox.rb', line 76 def self.(url) uri = URI.parse("http://is.gd/forward.php?format=simple&shorturl=#{url}") unshortened = Net::HTTP.get(uri) unshortened unless unshortened.match(%r{https?://is.gd/}) end |
.extract_url(url) ⇒ URI
Extract the first url from a string
113 114 115 |
# File 'lib/cinch/toolbox.rb', line 113 def self.extract_url(url) extract_urls(url).first end |
.extract_urls(url) ⇒ Array
Extract the urls from a string
120 121 122 |
# File 'lib/cinch/toolbox.rb', line 120 def self.extract_urls(url) URI.extract(url, %w(http https)) end |
.get_html_element(url, selector, mode = :css) ⇒ String
Get an element of the supplied website
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cinch/toolbox.rb', line 26 def self.get_html_element(url, selector, mode = :css) # Make sure the URL is legit url = Nokogiri.HTML(open(extract_url(url))) case mode when :css, :xpath page = url.send(mode.to_sym, selector) page.first.content unless page.first.nil? when :css_full, :xpath_full url.send("at_#{mode.to_s.gsub(/_full/, '')}", selector).to_html end # Rescue for any kind of network sillyness rescue SocketError, RuntimeError, Net::HTTPError nil end |
.get_page_title(url) ⇒ String
Get the title of a given web page.
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/cinch/toolbox.rb', line 46 def self.get_page_title(url) # Make sure the URL is legit url = extract_url(url) if url.match(/([^\s]+(\.(?i)(jpe?g|png|gif|bmp))$)/) # If the link is to an image, extract the filename. title = get_image_title(url) else # Grab the element, return nothing if the site doesn't have a title. title = Toolbox.get_html_element(url, 'title') end title.strip.gsub(/\s+/, ' ') unless title.nil? end |
.sent_via_private_message?(m, error = nil) ⇒ Boolean
Simple check to make sure people are using a command via the main channel
for plugins that require a channel for context.
95 96 97 98 99 100 |
# File 'lib/cinch/toolbox.rb', line 95 def self.(m, error = nil) return false unless m.channel.nil? error = 'You must use that command in the main channel.' if error.nil? m.user.msg error true end |
.shorten(url) ⇒ String
Shorten a URL via the configured shortener
63 64 65 66 67 68 69 70 71 |
# File 'lib/cinch/toolbox.rb', line 63 def self.shorten(url) return url if url.length < 45 uri = URI.parse("http://is.gd/create.php?format=simple&url=#{url}") shortened = Net::HTTP.get(uri) shortened if shortened.match(%r{https?://is.gd/}) rescue Errno::ETIMEDOUT # if the URL shortener is down, handle it. url end |
.time_format(secs, units = nil, format = :long) ⇒ Object
Used to render a period of time in a uniform string. There is probably a much better way to do this
105 106 107 108 |
# File 'lib/cinch/toolbox.rb', line 105 def self.time_format(secs, units = nil, format = :long) time = build_time_hash(secs, units) parse_time_hash(time, format) end |
.truncate(text, length = 250) ⇒ Object
Truncate a given block of text, used for making sure the bot doesn’t
flood.
87 88 89 90 91 |
# File 'lib/cinch/toolbox.rb', line 87 def self.truncate(text, length = 250) text = text.gsub(/\n/, ' ') text = text[0, (length - 3)] + '...' if text.length > length text end |