cinch-links-logger 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
data/README.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Cinch::Plugins::LinksLogger
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/cinch-links-logger.png)](http://badge.fury.io/rb/cinch-links-logger)
4
+ [![Dependency Status](https://gemnasium.com/bhaberer/cinch-links-logger.png)](https://gemnasium.com/bhaberer/cinch-links-logger)
5
+ [![Build Status](https://travis-ci.org/bhaberer/cinch-links-logger.png?branch=master)](https://travis-ci.org/bhaberer/cinch-links-logger)
6
+ [![Coverage Status](https://coveralls.io/repos/bhaberer/cinch-links-logger/badge.png?branch=master)](https://coveralls.io/r/bhaberer/cinch-links-logger?branch=master)
7
+ [![Code Climate](https://codeclimate.com/github/bhaberer/cinch-links-logger.png)](https://codeclimate.com/github/bhaberer/cinch-links-logger)
8
+
3
9
  Cinch Plugin for logging links and printing titles / stats for linked urls.
4
10
 
5
11
  ## Installation
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
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
@@ -17,7 +17,14 @@ 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.add_dependency('cinch-storage', '>= 0.0.1')
21
- gem.add_dependency('cinch-toolbox', '>= 0.0.5')
22
- gem.add_dependency('time-lord', '>= 1.0.1')
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'coveralls'
23
+ gem.add_development_dependency 'cinch-test'
24
+
25
+ gem.add_dependency 'cinch', '~> 2.0.5'
26
+ gem.add_dependency 'cinch-cooldown', '~> 1.0.0'
27
+ gem.add_dependency 'cinch-storage', '~> 1.0.1'
28
+ gem.add_dependency 'cinch-toolbox', '~> 1.0.3'
29
+ gem.add_dependency 'time-lord', '~> 1.0.1'
23
30
  end
@@ -1,12 +1,20 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require 'open-uri'
3
+ require 'cinch'
4
+ require 'cinch/toolbox'
3
5
  require 'cinch-storage'
4
- require 'cinch-toolbox'
5
6
  require 'time-lord'
6
7
 
8
+ class Link < Struct.new(:nick, :title, :count, :short_url, :time)
9
+ def to_yaml
10
+ {nick: nick, title: title, count: count, short_url: short_url, time: time }
11
+ end
12
+ end
13
+
7
14
  module Cinch::Plugins
8
15
  class LinksLogger
9
16
  include Cinch::Plugin
17
+ attr_reader :storage
10
18
 
11
19
  listen_to :channel
12
20
 
@@ -17,94 +25,55 @@ module Cinch::Plugins
17
25
  def initialize(*args)
18
26
  super
19
27
  @storage = CinchStorage.new(config[:filename] || 'yaml/links.yaml')
20
- @storage.data[:history] ||= Hash.new
21
- @post_titles = config[:titles].nil? ? true : config[:titles]
22
- @post_stats = config[:stats].nil? ? true : config[:stats]
28
+ @storage.data ||= Hash.new
23
29
  end
24
30
 
25
31
  def execute(m)
26
- if m.channel.nil?
27
- m.user.msg "You must use that command in the main channel."
28
- return
29
- end
30
-
31
- m.user.send "Recent Links in #{m.channel}"
32
- last = @storage.data[:history][m.channel.name].values.sort {|a,b| b[:time] <=> a[:time] }
33
- last[0,10].each_with_index do |link, i|
34
- msg = "#{i + 1} - "
35
- if link[:title].nil?
36
- msg << Cinch::Toolbox.expand(@link[:short_url])
37
- else
38
- msg << "#{link[:short_url]} ∴ #{link[:title]}"
39
- end
40
- m.user.send msg
41
- end
32
+ return if Cinch::Toolbox.sent_via_private_message?(m)
33
+ get_recent_links(m.channel.name).each { |line| m.user.send line }
42
34
  end
43
35
 
44
36
  def listen(m)
45
37
  urls = URI.extract(m.message, ["http", "https"])
46
38
  urls.each do |url|
47
39
  # Ensure we have a Channel Object in the History to dump links into.
48
- @storage.data[:history][m.channel.name] ||= Hash.new
49
-
50
- # Make sure it conforms to white/black lists before bothering.
51
- if whitelisted?(url) && !blacklisted?(url)
52
- # If the link was posted already, get the old info instead of getting new
53
- if @storage.data[:history][m.channel.name].key?(url)
54
- @storage.data[:history][m.channel.name][url][:count] += 1
55
- @link = @storage.data[:history][m.channel.name][url]
56
- else
57
- @link = { :nick => m.user.nick,
58
- :title => Cinch::Toolbox.get_page_title(url) || nil,
59
- :count => 1,
60
- :short_url => Cinch::Toolbox.shorten(url),
61
- :time => Time.now }
62
- @storage.data[:history][m.channel.name][url] = @link
63
- end
64
- else
65
- debug "#{blacklisted?(url) ? 'Blacklisted URL was not logged' : 'Domain not Whitelisted'} #{url}"
66
- return
67
- end
68
-
69
- # Check to see if we should post titles
70
- if @post_titles
71
- # Only spam the channel if you have a title
72
- unless @link[:title].nil?
73
- m.reply "#{@link[:short_url] || url} ∴ #{@link[:title]}"
74
- end
75
- end
76
-
77
- # Check to see if we should post stats and if it'ss been linked more than once.
78
- if @post_stats && @link[:count] > 1
79
- # No stats if this person was the first one to link it
80
- unless @link[:nick] == m.user.nick
81
- m.reply "That was already linked by #{@link[:nick]} #{@link[:time].ago.to_words}.", true
82
- end
83
- end
84
- end
85
-
86
- # Don't save unless we found some urls to process
87
- if urls
88
- synchronize(:save_links) do
89
- @storage.save
90
- end
40
+ @storage.data[m.channel.name] ||= Hash.new
41
+ @link = get_or_create_link(m, url)
91
42
  end
43
+ # Save if we matched urls.
44
+ @storage.synced_save(@bot) if urls
92
45
  end
93
46
 
94
47
  private
95
48
 
96
- def whitelisted?(url)
97
- return true unless config[:whitelist]
98
- debug "Checking Whitelist! #{config[:whitelist]} url: #{url}"
99
- return true if url.match(Regexp.new("https:?\/\/.*\.?#{config[:whitelist].join('|')}\."))
100
- false
49
+ def get_or_create_link(m, url)
50
+ channel = m.channel.name
51
+ # If the link was posted already, get the old info instead of getting new
52
+ if @storage.data[channel].key?(url)
53
+ @storage.data[channel][url][:count] += 1
54
+ link = @storage.data[channel][url]
55
+ else
56
+ link = Link.new(m.user.nick,
57
+ Cinch::Toolbox.get_page_title(url),
58
+ 1, Cinch::Toolbox.shorten(url), Time.now)
59
+ @storage.data[channel][url] = link
60
+ end
61
+ return link
101
62
  end
102
63
 
103
- def blacklisted?(url)
104
- return false unless config[:blacklist]
105
- debug "Checking Blacklist! #{config[:blacklist]} url: #{url}"
106
- return true if url.match(Regexp.new("https:?\/\/.*\.?#{config[:blacklist].join('|')}\."))
107
- false
64
+ def get_recent_links(channel)
65
+ message = ["Recent Links in #{channel}"]
66
+ links = @storage.data[channel].values.reverse[0..9]
67
+ puts links.to_s
68
+ puts message
69
+ links.each_with_index do |link|
70
+ message << if link.title.nil?
71
+ Cinch::Toolbox.expand(link.short_url)
72
+ else
73
+ "#{link.short_url} - #{link.title}"
74
+ end
75
+ end
76
+ return message
108
77
  end
109
78
  end
110
79
  end
@@ -1,7 +1,7 @@
1
1
  module Cinch
2
2
  module Plugins
3
3
  class LinksLogger
4
- VERSION = "0.0.1"
4
+ VERSION = "1.0.0"
5
5
  end
6
6
  end
7
7
  end
@@ -0,0 +1,30 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Cinch::Plugins::LinksLogger do
5
+ include Cinch::Test
6
+
7
+ before(:each) do
8
+ @bot = make_bot(Cinch::Plugins::LinksLogger, { :filename => '/dev/null' })
9
+ end
10
+
11
+ it 'should capture links' do
12
+ get_replies(make_message(@bot, 'http://github.com', { channel: '#foo', nick: 'bar' }))
13
+ @bot.plugins.first.storage.data['#foo'].keys.first.
14
+ should == 'http://github.com'
15
+ end
16
+
17
+ it 'should not capture malformed URLS' do
18
+ get_replies(make_message(@bot, 'htp://github.com', { channel: '#foo', nick: 'bar' }))
19
+ get_replies(make_message(@bot, 'http/github.com', { channel: '#foo', nick: 'bar' }))
20
+ @bot.plugins.first.storage.data['#foo'].
21
+ should be_nil
22
+ end
23
+
24
+ it 'should allow users to get a list of recently linked URLS' do
25
+ get_replies(make_message(@bot, 'http://github.com', { channel: '#foo', nick: 'bar' }))
26
+ replies = get_replies(make_message(@bot, '!links', { channel: '#foo', nick: 'test' }))
27
+ replies.first.text.should == 'Recent Links in #foo'
28
+ replies.last.text.should == 'http://github.com - GitHub · Build software better, together.'
29
+ end
30
+ end
@@ -0,0 +1,4 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+ require 'cinch-links-logger'
4
+ require 'cinch/test'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinch-links-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,46 +9,110 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-21 00:00:00.000000000 Z
12
+ date: 2013-07-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: cinch-storage
15
+ name: rake
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 0.0.1
22
- type: :runtime
21
+ version: '0'
22
+ type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.0.1
29
+ version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
- name: cinch-toolbox
31
+ name: rspec
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
35
  - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
- version: 0.0.5
38
- type: :runtime
37
+ version: '0'
38
+ type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
- version: 0.0.5
45
+ version: '0'
46
46
  - !ruby/object:Gem::Dependency
47
- name: time-lord
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
48
64
  requirement: !ruby/object:Gem::Requirement
49
65
  none: false
50
66
  requirements:
51
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
+ - - ~>
52
116
  - !ruby/object:Gem::Version
53
117
  version: 1.0.1
54
118
  type: :runtime
@@ -56,7 +120,39 @@ dependencies:
56
120
  version_requirements: !ruby/object:Gem::Requirement
57
121
  none: false
58
122
  requirements:
59
- - - ! '>='
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
+ - - ~>
60
156
  - !ruby/object:Gem::Version
61
157
  version: 1.0.1
62
158
  description: Cinch Plugin to track links in the channel
@@ -67,6 +163,7 @@ extensions: []
67
163
  extra_rdoc_files: []
68
164
  files:
69
165
  - .gitignore
166
+ - .travis.yml
70
167
  - Gemfile
71
168
  - LICENSE.txt
72
169
  - README.md
@@ -75,6 +172,8 @@ files:
75
172
  - lib/cinch-links-logger.rb
76
173
  - lib/cinch/plugins/links-logger/links-logger.rb
77
174
  - lib/cinch/plugins/links-logger/version.rb
175
+ - spec/cinch-links-logger_spec.rb
176
+ - spec/spec_helper.rb
78
177
  homepage: https://github.com/bhaberer/cinch-links-logger
79
178
  licenses: []
80
179
  post_install_message:
@@ -95,8 +194,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
194
  version: '0'
96
195
  requirements: []
97
196
  rubyforge_project:
98
- rubygems_version: 1.8.24
197
+ rubygems_version: 1.8.25
99
198
  signing_key:
100
199
  specification_version: 3
101
200
  summary: Cinch Plugin for links logging
102
- test_files: []
201
+ test_files:
202
+ - spec/cinch-links-logger_spec.rb
203
+ - spec/spec_helper.rb