Class: TwitterClient

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_client.rb

Constant Summary collapse

TCO_LENGTH =
20
MAX_CHARS =
140 - TCO_LENGTH

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTwitterClient



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

#apiObject

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

.authenticateObject



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.authorize_url + "\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.message_for(txt, url)
  message = txt.strip_tags
  while message.size>MAX_CHARS
    data = txt.split(" ")
    data.pop
    txt = data.join(" ")
    message = "#{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)
  message = self.class.message_for(txt, url)
  Rails.logger.info("Send twitter status: #{message}")
  if Rails.env.production?
    api.update(message)
  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