cinch-toolbox 0.0.4 → 0.0.5
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/README.md +8 -0
- data/lib/cinch-toolbox/version.rb +1 -1
- data/lib/cinch-toolbox.rb +26 -20
- metadata +1 -1
data/README.md
CHANGED
@@ -3,9 +3,17 @@
|
|
3
3
|
This is just a gem required fro many of my plugins, it facilitates a variety of mundane operations.
|
4
4
|
|
5
5
|
* URL Shortening / Expansion.
|
6
|
+
* URL Title Scraping.
|
6
7
|
* Webpage DOM element retrieval (via xpath or css selectors).
|
7
8
|
* Output truncation for sanity proof channel output.
|
8
9
|
|
10
|
+
Note: There is a small monkey patch to OpenURI contained in this gem. It allows for redirection
|
11
|
+
on urls that require https. For example, normally if you link to an `http://github.com/...` url on
|
12
|
+
GitHub you will get redirected to the https version of that link, and OpenURI will lose it's shit.
|
13
|
+
|
14
|
+
Note that this *only* honors redirection requests from HTTP => HTTPS and *not* HTTPS => HTTP.
|
15
|
+
|
16
|
+
|
9
17
|
## Installation
|
10
18
|
|
11
19
|
Add this line to your application's Gemfile:
|
data/lib/cinch-toolbox.rb
CHANGED
@@ -19,6 +19,26 @@ module Cinch
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
+
def Toolbox.get_page_title(url)
|
23
|
+
# Make sure the URL is legit
|
24
|
+
url = URI::extract(url, ["http", "https"]).first
|
25
|
+
|
26
|
+
# If the link is to an image, extract the filename.
|
27
|
+
if url.match(/\.jpg|jpeg|gif|png$/)
|
28
|
+
# unless it's from reddit, then change the url to the gallery to get the image's caption.
|
29
|
+
if imgur_id = url.match(/https?:\/\/i\.imgur\.com.*\/([A-Za-z0-9]+)\.(jpg|jpeg|png|gif)/)[1]
|
30
|
+
url = "http://imgur.com/#{imgur_id}"
|
31
|
+
else
|
32
|
+
site = url.match(/\.([^\.]+\.[^\/]+)/)
|
33
|
+
return site.nil? ? "Image [#{url}]!!!" : "Image from #{site[1]}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Grab the element, return nothing if the site doesn't have a title.
|
38
|
+
page = Nokogiri::HTML(open(url)).css('title')
|
39
|
+
return page.first.content.strip.gsub(/\s+/, ' ') unless page.empty?
|
40
|
+
end
|
41
|
+
|
22
42
|
# Expand a previously shortened URL via the configured shortener
|
23
43
|
def Toolbox.expand(url)
|
24
44
|
shortener.get("forward.php?format=simple&shorturl=#{url}").body
|
@@ -73,26 +93,12 @@ module Cinch
|
|
73
93
|
return short
|
74
94
|
end
|
75
95
|
|
76
|
-
|
77
|
-
|
78
|
-
url = URI::extract(url, ["http", "https"]).first
|
79
|
-
|
80
|
-
# If the link is to an image, extract the filename.
|
81
|
-
if url.match(/\.jpg|jpeg|gif|png$/)
|
82
|
-
# unless it's from reddit, then change the url to the gallery to get the image's caption.
|
83
|
-
if url.match(/https?:\/\/i\.imgur\.com.+\/([A-Za-z0-9]+)\.(jpg|jpeg|png|gif)/)
|
84
|
-
imgur_id = url.match(/https?:\/\/i\.imgur\.com.+\/([A-Za-z0-9]+)\.(jpg|jpeg|png|gif)/)[1]
|
85
|
-
url = "http://imgur.com/#{imgur_id}"
|
86
|
-
else
|
87
|
-
site = url.match(/\.([^\.]+\.[^\/]+)/)
|
88
|
-
return site.nil? ? "Image [#{url}]!!!" : "Image from #{site[1]}"
|
89
|
-
end
|
90
|
-
end
|
96
|
+
end
|
97
|
+
end
|
91
98
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
end
|
99
|
+
module OpenURI
|
100
|
+
def OpenURI.redirectable?(uri1, uri2) # :nodoc:
|
101
|
+
uri1.scheme.downcase == uri2.scheme.downcase ||
|
102
|
+
(/\A(?:http|ftp)\z/i =~ uri1.scheme && /\A(?:http|ftp|https)\z/i =~ uri2.scheme)
|
97
103
|
end
|
98
104
|
end
|