Class: TwitterClient
- Inherits:
-
Object
- Object
- TwitterClient
- Defined in:
- lib/twitter_client.rb
Constant Summary collapse
- TCO_LENGTH =
20- MAX_CHARS =
140 - TCO_LENGTH
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
-
#settings ⇒ Object
writeonly
Sets the attribute settings.
Class Method Summary collapse
Instance Method Summary collapse
- #follow(screen_name) ⇒ Object
- #friends(reload = false) ⇒ Object
-
#initialize ⇒ TwitterClient
constructor
A new instance of TwitterClient.
- #send_update(txt, url) ⇒ Object
- #unfollow(id) ⇒ Object
Constructor Details
#initialize ⇒ TwitterClient
9 10 11 12 13 14 15 16 |
# File 'lib/twitter_client.rb', line 9 def initialize self.api = TwitterOAuth::Client.new( consumer_key: settings[:consumer_key], consumer_secret: settings[:consumer_secret], token: settings[:token], secret: settings[:token_secret] ) end |
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
5 6 7 |
# File 'lib/twitter_client.rb', line 5 def api @api end |
#settings=(value) ⇒ Object
Sets the attribute settings
5 6 7 |
# File 'lib/twitter_client.rb', line 5 def settings=(value) @settings = value end |
Class Method Details
.authenticate ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/twitter_client.rb', line 46 def self.authenticate consumer = OAuth::Consumer.new(settings[:consumer_key], settings[:consumer_secret], site: "https://api.twitter.com", request_token_path: "/oauth/request_token", authorize_path: "/oauth/authorize", access_token_path: "/oauth/access_token" ) rt = consumer.get_request_token({oauth_callback: "oob"}) print "Goto following url then enter verification code\n" print rt. + "\n" print "Enter verification code: " code = $stdin.gets.strip access_token = rt.get_access_token(oauth_verifier: code) data = settings.merge(token: access_token.params[:oauth_token], token_secret: access_token.params[:oauth_token_secret]) File.open(config_file, "w") {|f|f.write(YAML.dump(data))} puts "Token saved" end |
.message_for(txt, url) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/twitter_client.rb', line 67 def self.(txt, url) = txt. while .size>MAX_CHARS data = txt.split(" ") data.pop txt = data.join(" ") = "#{txt}..." end "#{message} #{url}" end |
Instance Method Details
#follow(screen_name) ⇒ Object
38 39 40 |
# File 'lib/twitter_client.rb', line 38 def follow(screen_name) api.send(:post, "/friendships/create.json?screen_name=#{screen_name}&follow=true") end |
#friends(reload = false) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/twitter_client.rb', line 31 def friends(reload = false) Rails.cache.delete("twitter_friends") if reload Rails.cache.fetch("twitter_friends", expires_in: 12.hours) do api.all_friends end end |
#send_update(txt, url) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/twitter_client.rb', line 18 def send_update(txt, url) = self.class.(txt, url) Rails.logger.info("Send twitter status: #{message}") if Rails.env.production? api.update() else Rails.logger.info("Skipped, only activated in production") {} end rescue StandardError => err Rails.logger.info("Twitter error: #{err.message} (status: #{message})") end |
#unfollow(id) ⇒ Object
42 43 44 |
# File 'lib/twitter_client.rb', line 42 def unfollow(id) api.unfriend(id) end |