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

Class Method Details

.expand(url) ⇒ String

Expand a previously shortened URL via the configured shortener

Parameters:

  • url (String)

    A previously shortened url.

Returns:

  • (String)

    The expanded url.



76
77
78
79
80
# File 'lib/cinch/toolbox.rb', line 76

def self.expand(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

Parameters:

  • url (String)

    String to parse URIs from.

Returns:

  • (URI)

    URI created from the url.



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

Parameters:

  • url (String)

    String to parse URIs from.

Returns:

  • (Array)

    List of URIs created from the 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

Parameters:

  • url (String)

    The url to access.

  • selector (String)

    The the selector to try an acquire on the page.

  • mode (String) (defaults to: :css)

    (:css) Set this to the kind of selection you want to do.

  • [String] (Hash)

    a customizable set of options

Returns:

  • (String)

    The content ofg the Element or Nil if the element could not be found.



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.

Parameters:

  • url (String)

    The url of the page you want the title from.

Returns:

  • (String)

    Either contents of the title element or a notion for an image.



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.

Returns:

  • (Boolean)


95
96
97
98
99
100
# File 'lib/cinch/toolbox.rb', line 95

def self.sent_via_private_message?(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

Parameters:

  • url (String)

    The url of the page you want to shorten.

Returns:

  • (String)

    The shortened url.



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

Parameters:

  • secs (Fixnum)

    Number of seconds to render into a string.



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.

Parameters:

  • text (String)

    The block of text to check.

  • length (Fixnum) (defaults to: 250)

    (250) length to which to constrain the block of text.



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