cinch-toolbox 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/cinch-toolbox.rb +56 -55
- data/lib/cinch-toolbox/version.rb +1 -1
- metadata +2 -2
data/lib/cinch-toolbox.rb
CHANGED
@@ -1,74 +1,75 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
|
-
require
|
2
|
+
require 'cinch-toolbox/version'
|
3
3
|
require 'open-uri'
|
4
4
|
require 'patron'
|
5
5
|
|
6
|
-
module
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
42
|
-
#
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
-
|
53
|
-
|
52
|
+
return string
|
53
|
+
end
|
54
54
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
61
|
+
return { :days => days.floor,
|
62
|
+
:hours => hours.floor,
|
63
|
+
:mins => mins.floor,
|
64
|
+
:secs => secs.floor }
|
65
|
+
end
|
66
66
|
|
67
|
-
|
67
|
+
private
|
68
68
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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
|
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.
|
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-
|
12
|
+
date: 2013-05-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|