console_tweet 0.0.1
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/bin/twitter +5 -0
- data/lib/console_tweet/cli.rb +124 -0
- data/lib/console_tweet/console-tweet.rb +3 -0
- data/lib/console_tweet/version.rb +5 -0
- data/lib/console_tweet.rb +5 -0
- metadata +98 -0
data/bin/twitter
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
module ConsoleTweet
|
2
|
+
|
3
|
+
class CLI
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'twitter_oauth'
|
7
|
+
require 'yaml'
|
8
|
+
|
9
|
+
AllowedMethods = [:setup, :help, :status, :tweet]
|
10
|
+
ConsumerKey = 'MvVdCyl6xCVtEUVdcp4rw'
|
11
|
+
ConsumerSecret = '3xD0oy47WhWYUIBCU6QzcIBqsrAAL3KnYWKhd6ALk2k'
|
12
|
+
|
13
|
+
TOKEN_PATH = File.expand_path('~/.twitter')
|
14
|
+
|
15
|
+
NameColor = "\e[33m"
|
16
|
+
CommandColor = "\e[36m"
|
17
|
+
DefaultColor = "\e[0m"
|
18
|
+
NoteColor = "\e[37m"
|
19
|
+
ErrorColor = "\e[31m"
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
@commands = []
|
23
|
+
@arguments = {}
|
24
|
+
end
|
25
|
+
|
26
|
+
def start
|
27
|
+
ARGV.each do |arg|
|
28
|
+
unless arg.index('-') === 0
|
29
|
+
@commands << arg
|
30
|
+
end
|
31
|
+
end
|
32
|
+
# get the first command as the method, and the rest of the commands as args
|
33
|
+
method = @commands.empty? ? :help : @commands[0].to_sym
|
34
|
+
return method_missing(method) unless AllowedMethods.include?(method)
|
35
|
+
self.send(method, @commands[1..@commands.size])
|
36
|
+
end
|
37
|
+
|
38
|
+
# Prompt the user for a PIN using a request token, and see if we can successfully authenticate them
|
39
|
+
def get_access_token
|
40
|
+
@client = TwitterOAuth::Client.new(:consumer_key => ConsumerKey, :consumer_secret => ConsumerSecret)
|
41
|
+
request_token = @client.request_token
|
42
|
+
# ask the user to visit the auth url
|
43
|
+
puts "To authenticate your client, visit the URL: #{request_token.authorize_url}"
|
44
|
+
# wait for the user to give us the PIN back
|
45
|
+
print 'Enter PIN: '
|
46
|
+
begin
|
47
|
+
@client.authorize(request_token.token, request_token.secret, :oauth_verifier => self.class.get_input.chomp)
|
48
|
+
rescue OAuth::Unauthorized
|
49
|
+
false # Didn't get an access token
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def tweet(*args)
|
54
|
+
load_default_token
|
55
|
+
return failtown("Unauthorized, re-run setup!") unless @client.authorized?
|
56
|
+
@client.update(args.join(' '))
|
57
|
+
puts "Tweet Posted!"
|
58
|
+
end
|
59
|
+
|
60
|
+
def status(*args)
|
61
|
+
load_default_token
|
62
|
+
return failtown("Unauthorized, re-run setup!") unless @client.authorized?
|
63
|
+
user = @client.info
|
64
|
+
status = user['status']
|
65
|
+
puts "#{user['name']} (at #{status['created_at']}) #{status['text']}"
|
66
|
+
end
|
67
|
+
|
68
|
+
def setup(*args)
|
69
|
+
# Keep trying to get the access token
|
70
|
+
until @access_token = self.get_access_token
|
71
|
+
print "Try again? [Y/n] "
|
72
|
+
return false if self.class.get_input.downcase == 'n'
|
73
|
+
end
|
74
|
+
# When we finally get it, record it in a dotfile
|
75
|
+
tokens = {:default => { :token => @access_token.token, :secret => @access_token.secret }}
|
76
|
+
save_tokens(tokens)
|
77
|
+
end
|
78
|
+
|
79
|
+
def help(*args)
|
80
|
+
puts "#{NameColor}console-tweet#{DefaultColor} by John Crepezzi <john.crepezzi@gmail.com>"
|
81
|
+
puts 'github: http://github.com/seejohnrun/console-tweet'
|
82
|
+
puts
|
83
|
+
puts "#{CommandColor}twitter setup#{DefaultColor} Setup your account"
|
84
|
+
puts "#{CommandColor}twitter status#{DefaultColor} Get your most recent status"
|
85
|
+
puts "#{CommandColor}twitter tweet \"Hello World\"#{DefaultColor} Send out a tweet"
|
86
|
+
end
|
87
|
+
|
88
|
+
def failtown(message = nil)
|
89
|
+
puts "#{ErrorColor}Uh-oh! #{message}#{DefaultColor}\n" if message
|
90
|
+
help
|
91
|
+
end
|
92
|
+
|
93
|
+
def method_missing(command, *arguments)
|
94
|
+
failtown "Unknown command: #{command}\n"
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
def load_default_token
|
100
|
+
tokens = load_tokens
|
101
|
+
default_hash = tokens[:default]
|
102
|
+
@client = TwitterOAuth::Client.new(:consumer_key => ConsumerKey, :consumer_secret => ConsumerSecret, :token => default_hash[:token], :secret => default_hash[:secret])
|
103
|
+
end
|
104
|
+
|
105
|
+
def load_tokens
|
106
|
+
f = File.open(TOKEN_PATH, 'r')
|
107
|
+
tokens = YAML::load(f)
|
108
|
+
f.close
|
109
|
+
tokens
|
110
|
+
end
|
111
|
+
|
112
|
+
def save_tokens(tokens)
|
113
|
+
f = File.open(TOKEN_PATH, 'w')
|
114
|
+
YAML::dump(tokens, f)
|
115
|
+
f.close
|
116
|
+
end
|
117
|
+
|
118
|
+
def self.get_input
|
119
|
+
STDIN.gets.strip.chomp
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: console_tweet
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- John Crepezzi
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-29 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: twitter_oauth
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
description: CLI Twitter Client - with OAuth
|
50
|
+
email: john@crepezzi.com
|
51
|
+
executables:
|
52
|
+
- twitter
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files: []
|
56
|
+
|
57
|
+
files:
|
58
|
+
- lib/console_tweet/cli.rb
|
59
|
+
- lib/console_tweet/console-tweet.rb
|
60
|
+
- lib/console_tweet/version.rb
|
61
|
+
- lib/console_tweet.rb
|
62
|
+
- bin/twitter
|
63
|
+
has_rdoc: true
|
64
|
+
homepage: http://seejohnrun.github.com/console_tweet/
|
65
|
+
licenses: []
|
66
|
+
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project: console_tweet
|
93
|
+
rubygems_version: 1.3.7
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: CLI Twitter Client
|
97
|
+
test_files: []
|
98
|
+
|