cinch-links-titles 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cinch-links-titles.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Brian Haberer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # Cinch::Plugins::LinksTitles
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/cinch-links-titles.png)](http://badge.fury.io/rb/cinch-links-titles)
4
+ [![Dependency Status](https://gemnasium.com/bhaberer/cinch-links-titles.png)](https://gemnasium.com/bhaberer/cinch-links-titles)
5
+ [![Build Status](https://travis-ci.org/bhaberer/cinch-links-titles.png?branch=master)](https://travis-ci.org/bhaberer/cinch-links-titles)
6
+ [![Coverage Status](https://coveralls.io/repos/bhaberer/cinch-links-titles/badge.png?branch=master)](https://coveralls.io/r/bhaberer/cinch-links-titles?branch=master)
7
+ [![Code Climate](https://codeclimate.com/github/bhaberer/cinch-links-titles.png)](https://codeclimate.com/github/bhaberer/cinch-links-titles)
8
+
9
+ Cinch Plugin for logging links and printing titles / stats for linked urls.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'cinch-links-titles'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install cinch-links-titles
24
+
25
+ ## Usage
26
+
27
+ You will need to add the Plugin and config to your list first;
28
+
29
+ @bot = Cinch::Bot.new do
30
+ configure do |c|
31
+ c.plugins.plugins = [Cinch::Plugins::LinksLogger]
32
+ c.plugins.options[Cinch::Plugins::LinksTumblr] = { :stats => true,
33
+ :filename => 'yaml/links.yml',
34
+ :whitelist => nil,
35
+ :blacklist => nil }
36
+ end
37
+ end
38
+
39
+ The configuration variables are all optional, what's listed are their defaults
40
+
41
+ :stats (boolean) - Setting this to true will print the name of the user who first linked
42
+ the URL, if applicable.
43
+
44
+ :blacklist - An array of domains that you want to ignore, e.g.
45
+
46
+ :blackist => ['twitter', 'reddit']
47
+
48
+ :whitelist - An array of domains that you want limit title printing and logging to, e.g.
49
+
50
+ :whitelist => ['youtube']
51
+
52
+ :filename - the file to store previously linked urls.
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cinch/plugins/links-titles/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cinch-links-titles"
8
+ spec.version = Cinch::Plugins::LinksTitles::VERSION
9
+ spec.authors = ['Brian Haberer']
10
+ spec.email = ['bhaberer@gmail.com']
11
+ spec.description = %q{Cinch Plugin to print link titles to the channel}
12
+ spec.summary = %q{Link Titles}
13
+ spec.homepage = 'https://github.com/bhaberer/cinch-links-titles'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'rspec'
23
+ spec.add_development_dependency 'coveralls'
24
+ spec.add_development_dependency 'cinch-test'
25
+
26
+ spec.add_dependency 'cinch', '~> 2.0.5'
27
+ spec.add_dependency 'cinch-cooldown', '~> 1.0.0'
28
+ spec.add_dependency 'cinch-storage', '~> 1.0.1'
29
+ spec.add_dependency 'cinch-toolbox', '~> 1.0.3'
30
+ spec.add_dependency 'time-lord', '~> 1.0.1'
31
+ end
@@ -0,0 +1,3 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'cinch/plugins/links-titles/version'
3
+ require 'cinch/plugins/links-titles'
@@ -0,0 +1,118 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'open-uri'
3
+ require 'cinch'
4
+ require 'cinch/toolbox'
5
+ require 'cinch-storage'
6
+ require 'time-lord'
7
+
8
+ module Cinch::Plugins
9
+ # Plugin to print URL titles in the channel.
10
+ class LinksTitles
11
+ include Cinch::Plugin
12
+ attr_accessor :storage
13
+
14
+ # Simple struct object for tracking Links.
15
+ class Link < Struct.new(:nick, :title, :count, :short_url, :url, :time)
16
+ def to_yaml
17
+ { nick: nick, title: title, count: count, short_url: short_url,
18
+ url: url, time: time }
19
+ end
20
+ end
21
+
22
+ listen_to :channel
23
+
24
+ def initialize(*args)
25
+ super
26
+ @storage = CinchStorage.new(config[:filename] ||
27
+ 'yaml/links-titles.yaml')
28
+ @storage.data[:history] ||= {}
29
+ @post_stats = config[:stats].nil? ? false : config[:stats]
30
+ end
31
+
32
+ def listen(m)
33
+ urls = URI.extract(m.message, %w(http https))
34
+ urls.each do |url|
35
+ # Ensure we have a Channel Object in the History to dump links into.
36
+ @storage.data[:history][m.channel.name] ||= Hash.new
37
+
38
+ # Process link
39
+ link = process_link(url, m.channel.name, m.user.nick)
40
+
41
+ # Send link title to channel
42
+ post_title(m, link)
43
+
44
+ # Send link stats to channel
45
+ post_stats(m, link)
46
+ end
47
+
48
+ # Don't save unless we found some urls to process
49
+ @storage.synced_save(@bot) if urls
50
+ end
51
+
52
+ private
53
+
54
+ def process_link(url, channel, nick)
55
+ # Make sure it conforms to white/black lists before bothering.
56
+ if whitelisted?(url) && !blacklisted?(url)
57
+ return get_or_query_link(url, channel, nick)
58
+ else
59
+ if blacklisted?(url)
60
+ debug "Blacklisted URL was not logged #{url}"
61
+ else
62
+ debug "Domain not Whitelisted #{url}"
63
+ end
64
+ return nil
65
+ end
66
+ end
67
+
68
+ def get_or_query_link(url, channel, nick)
69
+ # If the link was posted already, get the old info
70
+ if @storage.data[:history][channel].key?(url)
71
+ @storage.data[:history][channel][url][:count] += 1
72
+ link = @storage.data[:history][channel][url]
73
+ else
74
+ link = Link.new(nick, Cinch::Toolbox.get_page_title(url) || nil,
75
+ 1, Cinch::Toolbox.shorten(url), url, Time.now)
76
+ @storage.data[:history][channel][url] = link
77
+ end
78
+ link
79
+ end
80
+
81
+ def post_title(m, link)
82
+ # Only spam the channel if you have a title
83
+ unless link.nil? || link.title.nil?
84
+ m.reply "#{link.short_url || link.url} ∴ #{link.title}"
85
+ end
86
+ end
87
+
88
+ def post_stats(m, link)
89
+ # Check to see if we should post stats and if it's been linked
90
+ # more than once.
91
+ if config[:stats]
92
+ if link.count > 1
93
+ # No stats if this person was the first one to link it
94
+ unless link.nick == m.user.nick
95
+ m.reply "That was already linked by #{link.nick} " +
96
+ "#{link.time.ago.to_words}.", true
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ def whitelisted?(url)
103
+ return true unless config[:whitelist]
104
+ debug "Checking Whitelist! #{config[:whitelist]} url: #{url}"
105
+ return true if url.match(
106
+ Regexp.new("https?:\/\/.*\.?#{config[:whitelist].join('|')}\."))
107
+ false
108
+ end
109
+
110
+ def blacklisted?(url)
111
+ return false unless config[:blacklist]
112
+ debug "Checking Blacklist! #{config[:blacklist]} url: #{url}"
113
+ return true if url.match(
114
+ Regexp.new("https?:\/\/.*\.?#{config[:blacklist].join('|')}\."))
115
+ false
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,9 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Cinch
3
+ module Plugins
4
+ # Versioning info
5
+ class LinksTitles
6
+ VERSION = '1.0.0'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,70 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Cinch::Plugins::LinksTitles do
5
+ include Cinch::Test
6
+
7
+ before(:each) do
8
+ @bot = make_bot(Cinch::Plugins::LinksTitles, { filename: '/dev/null' })
9
+ end
10
+
11
+ it 'should print titles' do
12
+ get_replies(make_message(@bot, 'http://github.com',
13
+ { channel: '#foo', nick: 'bar' })).first.text
14
+ .should include('GitHub · Build software better, together.')
15
+ end
16
+
17
+ it 'should obey blacklist and not title blacklisted links' do
18
+ bot = make_bot(Cinch::Plugins::LinksTitles,
19
+ { filename: '/dev/null',
20
+ blacklist: ['twitter'] })
21
+ get_replies(make_message(bot, 'http://twitter.com',
22
+ { channel: '#foo', nick: 'bar' })).first
23
+ .should be_nil
24
+ end
25
+
26
+ it 'should obey blacklist and title unblacklisted links' do
27
+ bot = make_bot(Cinch::Plugins::LinksTitles,
28
+ { filename: '/dev/null',
29
+ blacklist: ['twitter'] })
30
+ get_replies(make_message(bot, 'http://facebook.com',
31
+ { channel: '#foo', nick: 'bar' })).first
32
+ .should_not be_nil
33
+ end
34
+
35
+ it 'should obey whitelist and title whitelisted links' do
36
+ bot = make_bot(Cinch::Plugins::LinksTitles,
37
+ { filename: '/dev/null',
38
+ whitelist: ['twitter'] })
39
+ get_replies(make_message(bot, 'http://twitter.com',
40
+ { channel: '#foo', nick: 'bar' })).first
41
+ .should_not be_nil
42
+ end
43
+
44
+ it 'should obey whitelist and not title unwhitelisted links' do
45
+ bot = make_bot(Cinch::Plugins::LinksTitles,
46
+ { filename: '/dev/null',
47
+ whitelist: ['twitter'] })
48
+ get_replies(make_message(bot, 'http://facebook.com',
49
+ { channel: '#foo', nick: 'bar' })).first
50
+ .should be_nil
51
+ end
52
+
53
+ it 'should not display stats if they are not enabled' do
54
+ get_replies(make_message(@bot, 'http://facebook.com',
55
+ { channel: '#foo', nick: 'bar1' }))
56
+ get_replies(make_message(@bot, 'http://facebook.com',
57
+ { channel: '#foo', nick: 'bar2' })).last
58
+ .should_not include('That was already linked by')
59
+ end
60
+
61
+ it 'should display stats if they are enabled' do
62
+ bot = make_bot(Cinch::Plugins::LinksTitles,
63
+ { filename: '/dev/null', stats: true })
64
+ get_replies(make_message(bot, 'http://facebook.com',
65
+ { channel: '#foo', nick: 'bar1' }))
66
+ get_replies(make_message(bot, 'http://facebook.com',
67
+ { channel: '#foo', nick: 'bar2' })).last.text
68
+ .should include('That was already linked by')
69
+ end
70
+ end
@@ -0,0 +1,16 @@
1
+ require 'coveralls'
2
+ require 'simplecov'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+ SimpleCov.start
9
+
10
+ if File::exist? File.join(File.dirname(__FILE__), "secret.rb")
11
+ require 'secret'
12
+ end
13
+
14
+ require 'json'
15
+ require 'cinch-links-titles'
16
+ require 'cinch/test'
metadata ADDED
@@ -0,0 +1,205 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cinch-links-titles
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian Haberer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-06 00:00:00.000000000 Z
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: cinch-test
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
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: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: cinch
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 2.0.5
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 2.0.5
94
+ - !ruby/object:Gem::Dependency
95
+ name: cinch-cooldown
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 1.0.0
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.0.0
110
+ - !ruby/object:Gem::Dependency
111
+ name: cinch-storage
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 1.0.1
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 1.0.1
126
+ - !ruby/object:Gem::Dependency
127
+ name: cinch-toolbox
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: 1.0.3
134
+ type: :runtime
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 1.0.3
142
+ - !ruby/object:Gem::Dependency
143
+ name: time-lord
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ~>
148
+ - !ruby/object:Gem::Version
149
+ version: 1.0.1
150
+ type: :runtime
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: 1.0.1
158
+ description: Cinch Plugin to print link titles to the channel
159
+ email:
160
+ - bhaberer@gmail.com
161
+ executables: []
162
+ extensions: []
163
+ extra_rdoc_files: []
164
+ files:
165
+ - .gitignore
166
+ - .travis.yml
167
+ - Gemfile
168
+ - LICENSE.txt
169
+ - README.md
170
+ - Rakefile
171
+ - cinch-links-titles.gemspec
172
+ - lib/cinch-links-titles.rb
173
+ - lib/cinch/plugins/links-titles.rb
174
+ - lib/cinch/plugins/links-titles/version.rb
175
+ - spec/cinch-links-titles_spec.rb
176
+ - spec/spec_helper.rb
177
+ homepage: https://github.com/bhaberer/cinch-links-titles
178
+ licenses:
179
+ - MIT
180
+ post_install_message:
181
+ rdoc_options: []
182
+ require_paths:
183
+ - lib
184
+ required_ruby_version: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ! '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ required_rubygems_version: !ruby/object:Gem::Requirement
191
+ none: false
192
+ requirements:
193
+ - - ! '>='
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ requirements: []
197
+ rubyforge_project:
198
+ rubygems_version: 1.8.25
199
+ signing_key:
200
+ specification_version: 3
201
+ summary: Link Titles
202
+ test_files:
203
+ - spec/cinch-links-titles_spec.rb
204
+ - spec/spec_helper.rb
205
+ has_rdoc: