cinch-toolbox 0.0.5 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +5 -0
- data/README.md +15 -0
- data/Rakefile +6 -0
- data/cinch-toolbox.gemspec +7 -2
- data/lib/cinch-toolbox.rb +46 -39
- data/lib/cinch-toolbox/version.rb +1 -1
- data/spec/cinch-toolbox_spec.rb +117 -0
- data/spec/spec_helper.rb +4 -0
- metadata +77 -7
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# Cinch Toolbox
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/cinch-toolbox.png)](http://badge.fury.io/rb/cinch-dicebag)
|
4
|
+
[![Dependency Status](https://gemnasium.com/bhaberer/cinch-toolbox.png)](https://gemnasium.com/bhaberer/cinch-dicebag)
|
5
|
+
[![Build Status](https://travis-ci.org/bhaberer/cinch-toolbox.png?branch=master)](https://travis-ci.org/bhaberer/cinch-dicebag)
|
6
|
+
[![Coverage Status](https://coveralls.io/repos/bhaberer/cinch-toolbox/badge.png?branch=master)](https://coveralls.io/r/bhaberer/cinch-dicebag?branch=master)
|
7
|
+
[![Code Climate](https://codeclimate.com/github/bhaberer/cinch-toolbox.png)](https://codeclimate.com/github/bhaberer/cinch-dicebag)
|
8
|
+
|
3
9
|
This is just a gem required fro many of my plugins, it facilitates a variety of mundane operations.
|
4
10
|
|
5
11
|
* URL Shortening / Expansion.
|
@@ -35,3 +41,12 @@ Or install it yourself as:
|
|
35
41
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
42
|
4. Push to the branch (`git push origin my-new-feature`)
|
37
43
|
5. Create new Pull Request
|
44
|
+
|
45
|
+
## Changelog
|
46
|
+
|
47
|
+
* 1.0.0
|
48
|
+
* Added tests!
|
49
|
+
* Added docs!
|
50
|
+
* Cleaned up code in `Toolbox.get_html_element` to be more error resistant
|
51
|
+
* Cleaned up code in `Toolbox.time_format` to be more concise.
|
52
|
+
|
data/Rakefile
CHANGED
data/cinch-toolbox.gemspec
CHANGED
@@ -17,6 +17,11 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
|
20
|
-
gem.
|
21
|
-
gem.
|
20
|
+
gem.add_development_dependency 'rake'
|
21
|
+
gem.add_development_dependency 'rspec'
|
22
|
+
gem.add_development_dependency 'coveralls'
|
23
|
+
gem.add_development_dependency 'fakeweb', '~> 1.3'
|
24
|
+
|
25
|
+
gem.add_dependency 'nokogiri', '~> 1.5.9'
|
26
|
+
gem.add_dependency 'patron', '~> 0.4.18'
|
22
27
|
end
|
data/lib/cinch-toolbox.rb
CHANGED
@@ -6,19 +6,29 @@ require 'nokogiri'
|
|
6
6
|
|
7
7
|
module Cinch
|
8
8
|
module Toolbox
|
9
|
+
|
9
10
|
# Get an element of the supplied website
|
10
|
-
|
11
|
+
# @param [String] url The url to access.
|
12
|
+
# @param [String] selector The the selector to try an acquire on the page.
|
13
|
+
# @param [String] mode (:css) Set his to css or xpath based on the type of selector.
|
14
|
+
# @return [String] The content ofg the Element or Nil if the element could not be found.
|
15
|
+
def Toolbox.get_html_element(url, selector, mode = :css)
|
11
16
|
# Make sure the URL is legit
|
12
17
|
url = URI::extract(url, ["http", "https"]).first
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
return Nokogiri::HTML(open(url)).xpath(selector).first
|
18
|
+
url = Nokogiri::HTML(open(url))
|
19
|
+
if url.send(mode.to_sym, selector).empty?
|
20
|
+
return nil
|
21
|
+
else
|
22
|
+
return url.send(mode.to_sym, selector).first.content
|
19
23
|
end
|
24
|
+
rescue SocketError, RuntimeError
|
25
|
+
# Rescue for any kind of network sillyness
|
26
|
+
return nil
|
20
27
|
end
|
21
28
|
|
29
|
+
# Get the title of a given web page.
|
30
|
+
# @param [String] url The url of the page you want the title from.
|
31
|
+
# @return [String] Either contents of the title element or a notion for an image.
|
22
32
|
def Toolbox.get_page_title(url)
|
23
33
|
# Make sure the URL is legit
|
24
34
|
url = URI::extract(url, ["http", "https"]).first
|
@@ -26,63 +36,60 @@ module Cinch
|
|
26
36
|
# If the link is to an image, extract the filename.
|
27
37
|
if url.match(/\.jpg|jpeg|gif|png$/)
|
28
38
|
# unless it's from reddit, then change the url to the gallery to get the image's caption.
|
29
|
-
if imgur_id = url
|
39
|
+
if imgur_id = url[/https?:\/\/i\.imgur\.com.*\/([A-Za-z0-9]+)\.(jpg|jpeg|png|gif)/, 1]
|
30
40
|
url = "http://imgur.com/#{imgur_id}"
|
31
41
|
else
|
32
|
-
site = url.match(
|
42
|
+
site = url.match(/([^\.]+\.[^\/]+)/)
|
33
43
|
return site.nil? ? "Image [#{url}]!!!" : "Image from #{site[1]}"
|
34
44
|
end
|
35
45
|
end
|
36
46
|
|
37
47
|
# Grab the element, return nothing if the site doesn't have a title.
|
38
|
-
|
39
|
-
return
|
40
|
-
end
|
41
|
-
|
42
|
-
# Expand a previously shortened URL via the configured shortener
|
43
|
-
def Toolbox.expand(url)
|
44
|
-
shortener.get("forward.php?format=simple&shorturl=#{url}").body
|
48
|
+
title = Toolbox.get_html_element(url, 'title')
|
49
|
+
return title.strip.gsub(/\s+/, ' ') unless title.nil?
|
45
50
|
end
|
46
51
|
|
47
52
|
# Shorten a URL via the configured shortener
|
53
|
+
# @param [String] url The url of the page you want to shorten.
|
54
|
+
# @return [String] The shortened url.
|
48
55
|
def Toolbox.shorten(url)
|
49
56
|
return url if url.length < 45
|
50
57
|
return shortener.get("create.php?format=simple&url=#{url}").body
|
51
58
|
end
|
52
59
|
|
60
|
+
# Expand a previously shortened URL via the configured shortener
|
61
|
+
# @param [String] url A previously shortened url.
|
62
|
+
# @return [String] The expanded url.
|
63
|
+
def Toolbox.expand(url)
|
64
|
+
shortener.get("forward.php?format=simple&shorturl=#{url}").body
|
65
|
+
end
|
66
|
+
|
53
67
|
# Truncate a given block of text, used for making sure the bot doesn't flood.
|
68
|
+
# @param [String] text The block of text to check.
|
69
|
+
# @param [Fixnum] length (250) length to which to constrain the block of text.
|
54
70
|
def Toolbox.truncate(text, length = 250)
|
55
|
-
text = text.gsub(/\n/, '
|
71
|
+
text = text.gsub(/\n/, ' ')
|
56
72
|
if text.length > length
|
57
|
-
text = text[0,length -
|
73
|
+
text = text[0, (length - 3)] + '...'
|
58
74
|
end
|
59
75
|
return text
|
60
76
|
end
|
61
77
|
|
62
78
|
# Used to render a period of time in a uniform string.
|
63
79
|
# There is probably a much better way to do this, so FIXME
|
80
|
+
# @param [Fixnum] secs Number of seconds to render into a string.
|
64
81
|
def Toolbox.time_format(secs)
|
65
|
-
data =
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
string
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
def Toolbox.time_parse(secs)
|
77
|
-
days = secs / 86400
|
78
|
-
hours = (secs % 86400) / 3600
|
79
|
-
mins = (secs % 3600) / 60
|
80
|
-
secs = secs % 60
|
81
|
-
|
82
|
-
return { :days => days.floor,
|
83
|
-
:hours => hours.floor,
|
84
|
-
:mins => mins.floor,
|
85
|
-
:secs => secs.floor }
|
82
|
+
data = { :days => (secs / 86400).floor,
|
83
|
+
:hours => ((secs % 86400) / 3600).floor,
|
84
|
+
:mins => ((secs % 3600) / 60).floor,
|
85
|
+
:secs => (secs % 60).floor }
|
86
|
+
string = []
|
87
|
+
data.keys.map do |period|
|
88
|
+
if period == :secs || !(data[period].zero? && string.empty?)
|
89
|
+
string << "#{data[period]}#{period.slice(0)}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
return string.join(' ')
|
86
93
|
end
|
87
94
|
|
88
95
|
private
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fakeweb'
|
3
|
+
|
4
|
+
describe Cinch::Toolbox do
|
5
|
+
|
6
|
+
describe 'the get_html_element method' do
|
7
|
+
before(:all) do
|
8
|
+
FakeWeb.register_uri( :get, "http://example.com/",
|
9
|
+
:body => "<div id='a1' class='top'>
|
10
|
+
<div class='foo'>Bar</div>
|
11
|
+
<div id='foo1'>Baz</div>
|
12
|
+
</div>")
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should return a string with contents of a css selector of a web page' do
|
16
|
+
Cinch::Toolbox.get_html_element('http://example.com/', '.foo', :css).
|
17
|
+
should == 'Bar'
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should return a string with contents of a xpath selector of a web page' do
|
21
|
+
Cinch::Toolbox.get_html_element('http://example.com/', "//div/div[1]", :xpath).
|
22
|
+
should == 'Bar'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should return nil if the css element does not exist' do
|
26
|
+
Cinch::Toolbox.get_html_element('http://example.com/', '.foo2', :css).
|
27
|
+
should be_nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should return nil if the xpath element does not exist' do
|
31
|
+
Cinch::Toolbox.get_html_element('http://example.com/', "//div/div[3]", :xpath).
|
32
|
+
should be_nil
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should return nil if there is a problem finding the site' do
|
36
|
+
Cinch::Toolbox.get_html_element('http://baddurl.com/', '.foo2', :css).
|
37
|
+
should be_nil
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should return the page title if there is a http => https trasition' do
|
41
|
+
Cinch::Toolbox.get_html_element('http://github.com/bhaberer/', 'title', :css).
|
42
|
+
should include 'bhaberer (Brian Haberer)'
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should return nil if there is a https => http trasition' do
|
46
|
+
Cinch::Toolbox.get_html_element('https://www.amazon.com/', 'title', :css).
|
47
|
+
should be_nil
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'the get_page_title method' do
|
52
|
+
before(:all) do
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should return a page title for a given url' do
|
56
|
+
FakeWeb.register_uri( :get, "http://example.com/", :body => "<title>Page Title</table>")
|
57
|
+
Cinch::Toolbox.get_page_title("http://example.com/").
|
58
|
+
should == 'Page Title'
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should return Nil if a page is lacking a title' do
|
62
|
+
FakeWeb.register_uri( :get, "http://example.com/", :body => "<body>Page Title</body>")
|
63
|
+
Cinch::Toolbox.get_page_title("http://example.com/").
|
64
|
+
should be_nil
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should return an image title if a page is a supported image type' do
|
68
|
+
[:jpg, :jpeg, :gif, :png].each do |image|
|
69
|
+
Cinch::Toolbox.get_page_title("http://example.com/image.#{image}").
|
70
|
+
should == "Image from http://example.com"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should return Nil if a page is lacking a title' do
|
75
|
+
Cinch::Toolbox.get_page_title("http://i.imgur.com/oMndYK7.jpg").
|
76
|
+
should == "Anybody with a cat will relate. - Imgur"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe 'url shortening and expanding' do
|
81
|
+
it 'should shorten long urls' do
|
82
|
+
Cinch::Toolbox.shorten('http://en.wikipedia.org/wiki/Llanfairpwllgwyngyllgogerychwyrndrobwllllan').
|
83
|
+
should == "http://is.gd/PaUTII"
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'should not shorten urls that are already short enough (< 45 cha)' do
|
87
|
+
Cinch::Toolbox.shorten('http://en.wikipedia.org/').
|
88
|
+
should == 'http://en.wikipedia.org/'
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should be able to expand shortened links' do
|
92
|
+
url = 'http://en.wikipedia.org/wiki/Llanfairpwllgwyngyll'
|
93
|
+
Cinch::Toolbox.expand(Cinch::Toolbox.shorten(url)).should == url
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe 'truncate method' do
|
98
|
+
it 'should truncate text past the limit' do
|
99
|
+
foo = String.new
|
100
|
+
1000.times { foo << 'a' }
|
101
|
+
Cinch::Toolbox.truncate(foo).length.should == 250
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'should not truncate text within limit' do
|
105
|
+
foo = String.new
|
106
|
+
1000.times { foo << 'a' }
|
107
|
+
Cinch::Toolbox.truncate(foo, 100).length.should == 100
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe 'time parseing' do
|
112
|
+
it 'should parse seconds into a user friendly string' do
|
113
|
+
Cinch::Toolbox.time_format(126).
|
114
|
+
should == '2m 6s'
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
data/spec/spec_helper.rb
ADDED
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: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,14 +9,78 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-06-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: coveralls
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: fakeweb
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '1.3'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '1.3'
|
14
78
|
- !ruby/object:Gem::Dependency
|
15
79
|
name: nokogiri
|
16
80
|
requirement: !ruby/object:Gem::Requirement
|
17
81
|
none: false
|
18
82
|
requirements:
|
19
|
-
- -
|
83
|
+
- - ~>
|
20
84
|
- !ruby/object:Gem::Version
|
21
85
|
version: 1.5.9
|
22
86
|
type: :runtime
|
@@ -24,7 +88,7 @@ dependencies:
|
|
24
88
|
version_requirements: !ruby/object:Gem::Requirement
|
25
89
|
none: false
|
26
90
|
requirements:
|
27
|
-
- -
|
91
|
+
- - ~>
|
28
92
|
- !ruby/object:Gem::Version
|
29
93
|
version: 1.5.9
|
30
94
|
- !ruby/object:Gem::Dependency
|
@@ -32,7 +96,7 @@ dependencies:
|
|
32
96
|
requirement: !ruby/object:Gem::Requirement
|
33
97
|
none: false
|
34
98
|
requirements:
|
35
|
-
- -
|
99
|
+
- - ~>
|
36
100
|
- !ruby/object:Gem::Version
|
37
101
|
version: 0.4.18
|
38
102
|
type: :runtime
|
@@ -40,7 +104,7 @@ dependencies:
|
|
40
104
|
version_requirements: !ruby/object:Gem::Requirement
|
41
105
|
none: false
|
42
106
|
requirements:
|
43
|
-
- -
|
107
|
+
- - ~>
|
44
108
|
- !ruby/object:Gem::Version
|
45
109
|
version: 0.4.18
|
46
110
|
description: A gem of various methods used in many of my plugins. If you need the
|
@@ -52,6 +116,7 @@ extensions: []
|
|
52
116
|
extra_rdoc_files: []
|
53
117
|
files:
|
54
118
|
- .gitignore
|
119
|
+
- .travis.yml
|
55
120
|
- Gemfile
|
56
121
|
- LICENSE.txt
|
57
122
|
- README.md
|
@@ -59,6 +124,8 @@ files:
|
|
59
124
|
- cinch-toolbox.gemspec
|
60
125
|
- lib/cinch-toolbox.rb
|
61
126
|
- lib/cinch-toolbox/version.rb
|
127
|
+
- spec/cinch-toolbox_spec.rb
|
128
|
+
- spec/spec_helper.rb
|
62
129
|
homepage: https://github.com/bhaberer/cinch-toolbox
|
63
130
|
licenses: []
|
64
131
|
post_install_message:
|
@@ -83,4 +150,7 @@ rubygems_version: 1.8.24
|
|
83
150
|
signing_key:
|
84
151
|
specification_version: 3
|
85
152
|
summary: Common methods used in Cinch Plugins.
|
86
|
-
test_files:
|
153
|
+
test_files:
|
154
|
+
- spec/cinch-toolbox_spec.rb
|
155
|
+
- spec/spec_helper.rb
|
156
|
+
has_rdoc:
|