cinch-toolbox 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/cinch-toolbox.rb CHANGED
@@ -1,74 +1,75 @@
1
1
  # -*- coding: utf-8 -*-
2
- require "cinch-toolbox/version"
2
+ require 'cinch-toolbox/version'
3
3
  require 'open-uri'
4
4
  require 'patron'
5
5
 
6
- module Toolbox
6
+ module Cinch
7
+ module Toolbox
8
+ # Get an element of the supplied website
9
+ def Toolbox.get_html_element(url, selector, mode = 'css')
10
+ # Make sure the URL is legit
11
+ url = URI::extract(url, ["http", "https"]).first
7
12
 
8
- # Get an element of the supplied website
9
- def Toolbox.get_html_element(url, selector, mode = 'css')
10
- # Make sure the URL is legit
11
- url = URI::extract(url, ["http", "https"]).first
12
-
13
- case mode
14
- when 'css'
15
- return Nokogiri::HTML(open(url)).css(selector).first.content
16
- when 'xpath'
17
- return Nokogiri::HTML(open(url)).xpath(selector).first
13
+ case mode
14
+ when 'css'
15
+ return Nokogiri::HTML(open(url)).css(selector).first.content
16
+ when 'xpath'
17
+ return Nokogiri::HTML(open(url)).xpath(selector).first
18
+ end
18
19
  end
19
- end
20
20
 
21
- # Expand a previously shortened URL via the configured shortener
22
- def Toolbox.expand(url)
23
- shortener.get("forward.php?format=simple&shorturl=#{url}").body
24
- end
21
+ # Expand a previously shortened URL via the configured shortener
22
+ def Toolbox.expand(url)
23
+ shortener.get("forward.php?format=simple&shorturl=#{url}").body
24
+ end
25
25
 
26
- # Shorten a URL via the configured shortener
27
- def Toolbox.shorten(url)
28
- return url if url.length < 45
29
- return shortener.get("create.php?format=simple&url=#{url}").body
30
- end
26
+ # Shorten a URL via the configured shortener
27
+ def Toolbox.shorten(url)
28
+ return url if url.length < 45
29
+ return shortener.get("create.php?format=simple&url=#{url}").body
30
+ end
31
31
 
32
- # Truncate a given block of text, used for making sure the bot doesn't flood.
33
- def Toolbox.truncate(text, length = 250)
34
- text = text.gsub(/\n/, ' · ')
35
- if text.length > length
36
- text = text[0,length - 1] + '...'
32
+ # Truncate a given block of text, used for making sure the bot doesn't flood.
33
+ def Toolbox.truncate(text, length = 250)
34
+ text = text.gsub(/\n/, ' · ')
35
+ if text.length > length
36
+ text = text[0,length - 1] + '...'
37
+ end
38
+ return text
37
39
  end
38
- return text
39
- end
40
40
 
41
- # Used to render a period of time in a uniform string.
42
- # THere is probably a much better way to do this, so FIXME
43
- def Toolbox.time_format(secs)
44
- data = time_parse(secs)
45
- string = ''
41
+ # Used to render a period of time in a uniform string.
42
+ # There is probably a much better way to do this, so FIXME
43
+ def Toolbox.time_format(secs)
44
+ data = time_parse(secs)
45
+ string = ''
46
46
 
47
- string << "#{data[:days]}d " unless data[:days].zero? && string == ''
48
- string << "#{data[:hours]}h " unless data[:hours].zero? && string == ''
49
- string << "#{data[:mins]}m " unless data[:mins].zero? && string == ''
50
- string << "#{data[:secs]}s"
47
+ string << "#{data[:days]}d " unless data[:days].zero? && string == ''
48
+ string << "#{data[:hours]}h " unless data[:hours].zero? && string == ''
49
+ string << "#{data[:mins]}m " unless data[:mins].zero? && string == ''
50
+ string << "#{data[:secs]}s"
51
51
 
52
- return string
53
- end
52
+ return string
53
+ end
54
54
 
55
- def Toolbox.time_parse(secs)
56
- days = secs / 86400
57
- hours = (secs % 86400) / 3600
58
- mins = (secs % 3600) / 60
59
- secs = secs % 60
55
+ def Toolbox.time_parse(secs)
56
+ days = secs / 86400
57
+ hours = (secs % 86400) / 3600
58
+ mins = (secs % 3600) / 60
59
+ secs = secs % 60
60
60
 
61
- return { :days => days.floor,
62
- :hours => hours.floor,
63
- :mins => mins.floor,
64
- :secs => secs.floor }
65
- end
61
+ return { :days => days.floor,
62
+ :hours => hours.floor,
63
+ :mins => mins.floor,
64
+ :secs => secs.floor }
65
+ end
66
66
 
67
- private
67
+ private
68
68
 
69
- def Toolbox.shortener
70
- short = Patron::Session.new
71
- short.base_url = 'http://is.gd/'
72
- return short
69
+ def Toolbox.shortener
70
+ short = Patron::Session.new
71
+ short.base_url = 'http://is.gd/'
72
+ return short
73
+ end
73
74
  end
74
75
  end
@@ -1,5 +1,5 @@
1
1
  module Cinch
2
2
  module Toolbox
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinch-toolbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-01 00:00:00.000000000 Z
12
+ date: 2013-05-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri