twitter_client 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +4 -0
  2. data/lib/twitter_client.rb +94 -0
  3. metadata +78 -0
data/README ADDED
@@ -0,0 +1,4 @@
1
+ TwitterClient
2
+ =============
3
+
4
+ Personal ultra basic twitter client tailored for my specific needs on some projects
@@ -0,0 +1,94 @@
1
+ require 'oauth'
2
+ require 'twitter_oauth'
3
+ require 'twitter_client'
4
+
5
+ class TwitterClient
6
+ attr_accessor :api, :settings
7
+ TCO_LENGTH = 20
8
+ MAX_CHARS = 140 - TCO_LENGTH
9
+
10
+ def initialize
11
+ self.api = TwitterOAuth::Client.new(
12
+ :consumer_key => settings[:consumer_key],
13
+ :consumer_secret => settings[:consumer_secret],
14
+ :token => settings[:token],
15
+ :secret => settings[:token_secret]
16
+ )
17
+ end
18
+
19
+ def send_update(txt, url)
20
+ message = self.class.message_for(txt, url)
21
+ Rails.logger.info("Send twitter status: #{message}")
22
+ if Rails.env.production?
23
+ api.update(message)
24
+ else
25
+ Rails.logger.info("Skipped, only activated in production")
26
+ {}
27
+ end
28
+ rescue StandardError => err
29
+ Rails.logger.info("Twitter error: #{err.message} (status: #{message})")
30
+ end
31
+
32
+ def friends(all = false)
33
+ all ? api.all_friends : api.friends
34
+ end
35
+
36
+ def follow(screen_name)
37
+ api.send(:post, "/friendships/create.json?screen_name=#{screen_name}&follow=true")
38
+ end
39
+
40
+ def unfollow(id)
41
+ api.unfriend(id)
42
+ end
43
+
44
+ def self.authenticate
45
+ consumer = OAuth::Consumer.new(settings[:consumer_key], settings[:consumer_secret],
46
+ :site => "https://api.twitter.com",
47
+ :request_token_path => "/oauth/request_token",
48
+ :authorize_path => "/oauth/authorize",
49
+ :access_token_path => "/oauth/access_token"
50
+ )
51
+
52
+ rt = consumer.get_request_token({:oauth_callback => "oob"})
53
+ print "Goto following url then enter verification code\n"
54
+ print rt.authorize_url + "\n"
55
+ print "Enter verification code: "
56
+ code = $stdin.gets.strip
57
+ access_token = rt.get_access_token(:oauth_verifier => code)
58
+
59
+ data = settings.merge(:token => access_token.params[:oauth_token], :token_secret => access_token.params[:oauth_token_secret])
60
+
61
+ File.open(config_file, "w") {|f|f.write(YAML.dump(data))}
62
+ puts "Token saved"
63
+ end
64
+
65
+ def self.message_for(txt, url)
66
+ message = txt.strip_tags
67
+ while message.size>MAX_CHARS
68
+ data = txt.split(" ")
69
+ data.pop
70
+ txt = data.join(" ")
71
+ message = "#{txt}..."
72
+ end
73
+ "#{message} #{url}"
74
+ end
75
+
76
+ private
77
+ def self.settings
78
+ @settings ||= YAML.load_file(config_file)
79
+ end
80
+
81
+ def self.config_file
82
+ @config_file ||= Rails.root.join("config/oauth_twitter.yml")
83
+ end
84
+
85
+ def settings
86
+ self.class.settings
87
+ end
88
+
89
+ def update_settings(key, value)
90
+ @settings[key] = value
91
+ File.open(config_file, "w") {|f| f.write(YAML.dump(@settings))}
92
+ @settings
93
+ end
94
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitter_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Olivier Ruffin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: oauth
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
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: twitter_oauth
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
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
+ description:
47
+ email: olivier@muweb.fr
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - README
53
+ - lib/twitter_client.rb
54
+ homepage: http://muweb.fr
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.23
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Minimalist twitter client for my specific use
78
+ test_files: []