cinch-twitter 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Version 0.2.0
2
+ * The plugin can now be included via `Cinch::Plugins::Twitter`.
3
+
4
+ # Version 0.1.0
5
+ * Initial release
data/README.md CHANGED
@@ -3,8 +3,6 @@ cinch-twitter
3
3
 
4
4
  A Cinch plugin for accessing Twitter, using [John Nunemaker's](https://github.com/jnunemaker) wonderful [Twitter gem](https://github.com/jnunemaker/twitter).
5
5
 
6
- Requiring the gem: `require 'cinch/plugins/twitter'`
7
-
8
6
  Usage
9
7
  -----
10
8
 
@@ -16,17 +14,18 @@ Usage
16
14
  Shorthand commands are also available:
17
15
  * `@[username]<+D>`, @#[id]
18
16
 
19
- Loading
20
- -------
21
-
22
- This should be obvious, right? Not so.
23
-
24
- `c.plugins.plugins << Cinch::Plugins::Twitter::Client`
17
+ Requiring and including the plugin
18
+ ----------------------------------
19
+ require 'cinch/plugins/twitter'
20
+ # ...
21
+ Bot.config {|c|
22
+ # ...
23
+ c.plugins.plugins << Cinch::Plugins::Twitter
25
24
 
26
25
  Configuration
27
26
  -------------
28
27
 
29
- To configure the plugin, you must insert your access keys for the client. If you wish, you may use `JSON` or `YAML` to store and deserialize them.
28
+ To configure the plugin, you must insert your access keys for the client.
30
29
 
31
30
  To retrieve your access keys and `oauth` tokens, if you already have an application set up, please visit https://dev.twitter.com/apps, otherwise visit https://dev.twitter.com/apps/new and follow the instructions.
32
31
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -5,19 +5,21 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "cinch-twitter"
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Mark Seymour"]
12
- s.date = "2012-06-27"
12
+ s.date = "2012-06-28"
13
13
  s.description = "A Twitter plugin for Cinch."
14
14
  s.email = "mark.seymour.ns@gmail.com"
15
15
  s.extra_rdoc_files = [
16
+ "CHANGELOG.md",
16
17
  "LICENSE.txt",
17
18
  "README.md"
18
19
  ]
19
20
  s.files = [
20
21
  ".document",
22
+ "CHANGELOG.md",
21
23
  "Gemfile",
22
24
  "Gemfile.lock",
23
25
  "LICENSE.txt",
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Cinch
4
4
  module Plugins
5
- module Twitter
5
+ class Twitter
6
6
  module ErrorHandler
7
7
 
8
8
  class Warnings < StandardError
@@ -1,22 +1,23 @@
1
1
  # coding: utf-8
2
2
  require 'cgi'
3
+ require 'cinch/formatting'
3
4
 
4
5
  module Cinch
5
6
  module Plugins
6
- module Twitter
7
+ class Twitter
7
8
  module Formatter
8
9
 
9
10
  def format_tweet(tweet)
10
11
  tweet_text = expand_uris(tweet.text, tweet.attrs["entities"]["urls"])
11
12
  parts, head, body, tail, urls = [], [], [], [], []
12
- head = Format(:bold,"#{tweet.user.screen_name} »")
13
+ head = Cinch::Formatting.format(:bold,"#{tweet.user.screen_name} »")
13
14
  body << CGI::unescapeHTML(tweet_text.gsub("\n", " ").squeeze(" "))
14
- body << Format(:aqua,"*twoosh*") if tweet.text.length == 140
15
+ body << Cinch::Formatting.format(:aqua,"*twoosh*") if tweet.text.length == 140
15
16
  tail << "From #{tweet.place.full_name}" if !tweet.place.blank?
16
17
  tail << "at #{tweet.created_at.strftime("%B %-d, %Y, %-I:%m%P")}"
17
18
  tail << "via #{tweet.source.gsub( %r{</?[^>]+?>}, '' )}"
18
19
  urls << "https://twitter.com/#{tweet.user.screen_name}"
19
- urls << Format(:grey,"in reply to") if !tweet.in_reply_to_screen_name.blank?
20
+ urls << Cinch::Formatting.format(:grey,"in reply to") if !tweet.in_reply_to_screen_name.blank?
20
21
  urls << "http://twitter.com/#{tweet.in_reply_to_screen_name}#{"/statuses/#{tweet.in_reply_to_status_id.to_s}" if !tweet.in_reply_to_status_id.blank?}" if !tweet.in_reply_to_screen_name.blank?
21
22
  parts = [head, body, ["(", tail.join(" "), ")"].join, urls].flatten
22
23
  parts.join(" ")
@@ -25,9 +26,9 @@ module Cinch
25
26
  def format_search(tweet)
26
27
  tweet_text = expand_uris(tweet.text, tweet.attrs["entities"]["urls"])
27
28
  parts, head, body, tail, urls = [], [], [], [], []
28
- head = Format(:bold,"#{tweet.from_user} »")
29
+ head = Cinch::Formatting.format(:bold,"#{tweet.from_user} »")
29
30
  body << CGI::unescapeHTML(tweet_text.gsub("\n", " ").squeeze(" "))
30
- body << Format(:aqua,"*twoosh*") if tweet.text.length == 140
31
+ body << Cinch::Formatting.format(:aqua,"*twoosh*") if tweet.text.length == 140
31
32
  tail << "at #{tweet.created_at.strftime("%B %-d, %Y, %-I:%m%P")}"
32
33
  urls << "https://twitter.com/#{tweet.from_user}"
33
34
  parts = [head, body, ["(", tail.join(" "), ")"].join, urls].flatten
@@ -36,11 +37,11 @@ module Cinch
36
37
 
37
38
  def format_tweep_info(tweep)
38
39
  tweep_status_text = expand_uris(tweep.status.text, tweep.status.attrs["entities"]["urls"])
39
- head = "#{Format(:aqua,tweep.name)}" + Format(:silver," (#{tweep.screen_name})") + Format(:grey," - #{tweep.url} https://twitter.com/#{tweep.screen_name}")
40
+ head = "#{Cinch::Formatting.format(:aqua,tweep.name)}" + Cinch::Formatting.format(:silver," (#{tweep.screen_name})") + Cinch::Formatting.format(:grey," - #{tweep.url} https://twitter.com/#{tweep.screen_name}")
40
41
  bio = ""
41
- bio = Format(:aqua,"\"#{tweep.description.strip}\"") if !tweep.description.blank?
42
+ bio = Cinch::Formatting.format(:aqua,"\"#{tweep.description.strip}\"") if !tweep.description.blank?
42
43
  location = ""
43
- location = "They are from #{Format(:aqua,tweep.location.strip)}" if !tweep.location.blank?
44
+ location = "They are from #{Cinch::Formatting.format(:aqua,tweep.location.strip)}" if !tweep.location.blank?
44
45
  desc = [] << "has made #{tweep.statuses_count} tweets since #{tweep.created_at.strftime("%B %-d, %Y")}"
45
46
  desc << "follows #{tweep.friends_count} tweeps" if tweep.friends_count > 0
46
47
  desc << "has #{tweep.followers_count} followers" if tweep.followers_count > 0
@@ -51,14 +52,14 @@ module Cinch
51
52
  flags << "is a translator for Twitter" if tweep.is_translator?
52
53
  flags << "is verified" if tweep.verified?
53
54
  flags << "would rather keep their life secret" if tweep.protected?
54
- tweet = [] << Format(:aqua,"Their latest tweet:")
55
+ tweet = [] << Cinch::Formatting.format(:aqua,"Their latest tweet:")
55
56
  tweet << CGI::unescapeHTML(tweep_status_text.gsub("\n", " ").squeeze(" "))
56
57
  tweet_tail = []
57
58
  tweet_tail << "from #{tweep.status.place.full_name}" if !tweep.status.place.blank?
58
59
  tweet_tail << "at #{tweep.status.created_at.strftime("%B %-d, %Y, %-I:%m%P")}"
59
60
 
60
61
  parts = [head, bio, location, desc, flags].reject(&:blank?).map {|e| e.is_a?(Array) ? "#{tweep.name} " + e.to_sentence + "." : e }
61
- parts << [tweet, Format(:silver,["(", tweet_tail.join(" "), ")"].join)].join(" ")
62
+ parts << [tweet, Cinch::Formatting.format(:silver,["(", tweet_tail.join(" "), ")"].join)].join(" ")
62
63
  parts.join("\n")
63
64
  end
64
65
 
@@ -4,69 +4,71 @@ require 'cinch/plugins/twitter/error_handler'
4
4
 
5
5
  module Cinch
6
6
  module Plugins
7
- module Twitter
7
+ class Twitter
8
8
  module TweetHandler
9
- include Formatter
10
- include ErrorHandler
11
-
12
- EXCEPTIONS = [::Twitter::Error, ErrorHandler::Warnings]
13
- AMessage = Struct.new(:message,:type)
14
-
15
9
  # Handler methods
10
+ class << self
11
+ include Formatter
12
+ include ErrorHandler
16
13
 
17
- def tweet_by_username(params={})
18
- params = { username: "Twitter", nth_tweet: 0 }.merge(params)
19
- begin
20
- raise Warnings::TooManyTweets if params[:nth_tweet].to_i > 20
21
- timeline = ::Twitter.user_timeline params[:username], include_entities: true, include_rts: true, count: params[:nth_tweet].to_i.succ
22
- raise Warnings::NoTweets if timeline.blank?
23
- tweet = timeline.last
24
- params[:username] = tweet.user.screen_name if !tweet.user.nil? # For proper case.
14
+ EXCEPTIONS = [::Twitter::Error, ErrorHandler::Warnings]
15
+ AMessage = Struct.new(:message,:type)
25
16
 
26
- return "No tweets!" if timeline.blank?
27
- return "Protected!" if tweet.user.protected?
17
+ def tweet_by_username(params={})
18
+ params = { username: "Twitter", nth_tweet: 0 }.merge(params)
19
+ begin
20
+ raise Warnings::TooManyTweets if params[:nth_tweet].to_i > 20
21
+ timeline = ::Twitter.user_timeline params[:username], include_entities: true, include_rts: true, count: params[:nth_tweet].to_i.succ
22
+ raise Warnings::NoTweets if timeline.blank?
23
+ tweet = timeline.last
24
+ params[:username] = tweet.user.screen_name if !tweet.user.nil? # For proper case.
28
25
 
29
- AMessage.new format_tweet(tweet) # The fun starts here. If there is ever a problem, it'll bubble up here and be caught.
26
+ return "No tweets!" if timeline.blank?
27
+ return "Protected!" if tweet.user.protected?
30
28
 
31
- rescue *EXCEPTIONS => ex
32
- AMessage.new handle_error(ex, params[:username], @bot.nick), :notice
29
+ AMessage.new format_tweet(tweet) # The fun starts here. If there is ever a problem, it'll bubble up here and be caught.
30
+
31
+ rescue *EXCEPTIONS => ex
32
+ AMessage.new handle_error(ex, params[:username], @bot.nick), :notice
33
+ end
33
34
  end
34
- end
35
35
 
36
- def tweet_by_id(params={})
37
- params = {id: 0 }.merge(params)
38
- begin
39
- tweet = ::Twitter.status params[:id], include_entities: true
40
- params[:username] = tweet.user.screen_name if !tweet.user.nil? # For easy access.
41
- AMessage.new format_tweet(tweet) # If there is ever a problem, it'll bubble up here and be caught.
42
- rescue *EXCEPTIONS => ex
43
- AMessage.new handle_error(ex, params[:username], @bot.nick), :notice
36
+ def tweet_by_id(params={})
37
+ params = {id: 0 }.merge(params)
38
+ begin
39
+ tweet = ::Twitter.status params[:id], include_entities: true
40
+ params[:username] = tweet.user.screen_name if !tweet.user.nil? # For easy access.
41
+ AMessage.new format_tweet(tweet) # If there is ever a problem, it'll bubble up here and be caught.
42
+ rescue *EXCEPTIONS => ex
43
+ AMessage.new handle_error(ex, params[:username], @bot.nick), :notice
44
+ end
44
45
  end
45
- end
46
46
 
47
- def tweep_info(params={})
48
- params = {username: "Twitter"}.merge(params)
49
- begin
50
- tweep = ::Twitter.user params[:username], include_entities: true
51
- AMessage.new format_tweep_info(tweep)
52
- rescue *EXCEPTIONS => ex
53
- AMessage.new handle_error(ex, params[:username], @bot.nick), :notice
47
+ def tweep_info(params={})
48
+ params = {username: "Twitter"}.merge(params)
49
+ begin
50
+ tweep = ::Twitter.user params[:username], include_entities: true
51
+ AMessage.new format_tweep_info(tweep)
52
+ rescue *EXCEPTIONS => ex
53
+ AMessage.new handle_error(ex, params[:username], @bot.nick), :notice
54
+ end
54
55
  end
55
- end
56
56
 
57
- def search_by_term(params={})
58
- params = {term: "cat"}.merge(params)
59
- begin
60
- results = []
61
- ::Twitter.search(params[:term], include_entities: true, rpp: 3, result_type: "recent").each {|status|
62
- params[:username] = status.from_user
63
- results << format_search(status)
64
- }
65
- results << "There are no results for \"#{params[:term]}\"." if results.empty?
66
- AMessage.new results.join("\n")
67
- rescue *EXCEPTIONS => ex
68
- AMessage.new handle_error(ex, params[:username], @bot.nick), :notice
57
+ def search_by_term(params={})
58
+ params = {term: "cat"}.merge(params)
59
+ begin
60
+ results = []
61
+ ::Twitter.search(params[:term], include_entities: true, rpp: 3, result_type: "recent").each {|status|
62
+ params[:username] = status.from_user
63
+ results << format_search(status)
64
+ }
65
+ results << "There are no results for \"#{params[:term]}\"." if results.empty?
66
+ AMessage.new results.join("\n")
67
+ rescue *EXCEPTIONS => ex
68
+ AMessage.new handle_error(ex, params[:username], @bot.nick), :notice
69
+ end
69
70
  end
71
+
70
72
  end
71
73
 
72
74
  end
@@ -7,80 +7,77 @@ require 'cinch/plugins/twitter/tweet_handler'
7
7
 
8
8
  module Cinch
9
9
  module Plugins
10
- module Twitter
10
+ class Twitter
11
+ include Cinch::Plugin
12
+ #include Twitter::TweetHandler
11
13
 
12
- class Client
13
- include Cinch::Plugin
14
- include Twitter::TweetHandler
14
+ set(
15
+ plugin_name: "Twitter",
16
+ help: "Access Twitter from the comfort of your IRC client! Usage:\n* `!tw <username><+D>` - Gets the latest tweet of the specified user, or the tweet 'D' tweets back, between 1 and 20.\n* `!tw #[id]` - Gets the tweet at the specified ID\n* `?tw [username]` - Gets the specified user's Twitter profile\n* `?ts [term]` - Searches for three of the most recent tweets regarding the specified query\n\nShorthand: `@[username]<+D>`, @#[id]",
17
+ required_options: [:access_keys])
15
18
 
16
- set(
17
- plugin_name: "Twitter",
18
- help: "Access Twitter from the comfort of your IRC client! Usage:\n* `!tw <username><+D>` - Gets the latest tweet of the specified user, or the tweet 'D' tweets back, between 1 and 20.\n* `!tw #[id]` - Gets the tweet at the specified ID\n* `?tw [username]` - Gets the specified user's Twitter profile\n* `?ts [term]` - Searches for three of the most recent tweets regarding the specified query\n\nShorthand: `@[username]<+D>`, @#[id]",
19
- required_options: [:access_keys])
20
-
21
- def initialize(*args)
22
- super
23
- keys = config[:access_keys]
24
- ::Twitter.configure do |c|
25
- c.consumer_key = keys["consumer_key"]
26
- c.consumer_secret = keys["consumer_secret"]
27
- c.oauth_token = keys["oauth_token"]
28
- c.oauth_token_secret = keys["oauth_token_secret"]
29
- end
19
+ def initialize(*args)
20
+ super
21
+ keys = config[:access_keys]
22
+ ::Twitter.configure do |c|
23
+ c.consumer_key = keys["consumer_key"]
24
+ c.consumer_secret = keys["consumer_secret"]
25
+ c.oauth_token = keys["oauth_token"]
26
+ c.oauth_token_secret = keys["oauth_token_secret"]
30
27
  end
28
+ end
31
29
 
32
- def is_notice?(m)
33
- m.type == :notice ? true : false
34
- end
30
+ def is_notice?(m)
31
+ m.type == :notice ? true : false
32
+ end
35
33
 
36
34
 
37
- match /tw$/, method: :execute_tweet
38
- match /tw (\w+)(?:-(\d+))?$/, method: :execute_tweet
39
- match /^@(\w+)(?:-(\d+))?$/, method: :execute_tweet, use_prefix: false
40
- def execute_tweet(m, username = nil, nth_tweet = nil)
41
- options = {}
42
- options[:username] = username unless username.nil?
43
- options[:nth_tweet] = nth_tweet unless nth_tweet.nil?
44
- result = tweet_by_username(options)
45
- if is_notice?(result)
46
- m.user.notice result.message
47
- else
48
- m.reply result.message
49
- end
35
+ match /tw$/, method: :execute_tweet
36
+ match /tw (\w+)(?:-(\d+))?$/, method: :execute_tweet
37
+ match /^@(\w+)(?:-(\d+))?$/, method: :execute_tweet, use_prefix: false
38
+ def execute_tweet(m, username = nil, nth_tweet = nil)
39
+ options = {}
40
+ options[:username] = username unless username.nil?
41
+ options[:nth_tweet] = nth_tweet unless nth_tweet.nil?
42
+ result = TweetHandler.tweet_by_username(options)
43
+ if is_notice?(result)
44
+ m.user.notice result.message
45
+ else
46
+ m.reply result.message
50
47
  end
48
+ end
51
49
 
52
- match /tw #(\d+)$/, method: :execute_id
53
- match /^@#(\d+)$/, method: :execute_id, use_prefix: false
54
- def execute_id(m, id)
55
- result = tweet_by_id(id: id)
56
- if is_notice?(result)
57
- m.user.notice result.message
58
- else
59
- m.reply result.message
60
- end
50
+ match /tw #(\d+)$/, method: :execute_id
51
+ match /^@#(\d+)$/, method: :execute_id, use_prefix: false
52
+ def execute_id(m, id)
53
+ result = TweetHandler.tweet_by_id(id: id)
54
+ if is_notice?(result)
55
+ m.user.notice result.message
56
+ else
57
+ m.reply result.message
61
58
  end
59
+ end
62
60
 
63
- match /\?tw (\w+)$/, method: :execute_info, use_prefix: false
64
- def execute_info(m, username)
65
- result = tweep_info(username: username)
66
- if is_notice?(result)
67
- m.user.notice result.message
68
- else
69
- m.reply result.message
70
- end
61
+ match /\?tw (\w+)$/, method: :execute_info, use_prefix: false
62
+ def execute_info(m, username)
63
+ result = TweetHandler.tweep_info(username: username)
64
+ if is_notice?(result)
65
+ m.user.notice result.message
66
+ else
67
+ m.reply result.message
71
68
  end
69
+ end
72
70
 
73
- match /\?ts (.+)$/, method: :execute_search, use_prefix: false
74
- def execute_search(m, term)
75
- result = search_by_term(term: term)
76
- if is_notice?(result)
77
- m.user.notice result.message
78
- else
79
- m.reply result.message
80
- end
71
+ match /\?ts (.+)$/, method: :execute_search, use_prefix: false
72
+ def execute_search(m, term)
73
+ result = TweetHandler.search_by_term(term: term)
74
+ if is_notice?(result)
75
+ m.user.notice result.message
76
+ else
77
+ m.reply result.message
81
78
  end
82
-
83
79
  end
80
+
84
81
  end
85
82
  end
86
83
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinch-twitter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
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: 2012-06-27 00:00:00.000000000 Z
12
+ date: 2012-06-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -128,10 +128,12 @@ email: mark.seymour.ns@gmail.com
128
128
  executables: []
129
129
  extensions: []
130
130
  extra_rdoc_files:
131
+ - CHANGELOG.md
131
132
  - LICENSE.txt
132
133
  - README.md
133
134
  files:
134
135
  - .document
136
+ - CHANGELOG.md
135
137
  - Gemfile
136
138
  - Gemfile.lock
137
139
  - LICENSE.txt
@@ -158,7 +160,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
158
160
  version: '0'
159
161
  segments:
160
162
  - 0
161
- hash: -858696623
163
+ hash: -173112557
162
164
  required_rubygems_version: !ruby/object:Gem::Requirement
163
165
  none: false
164
166
  requirements: