Class: Lita::Handlers::Tweet::Data

Inherits:
Struct
  • Object
show all
Defined in:
lib/lita/handlers/tweet/data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config

Returns:

  • (Object)

    the current value of config



7
8
9
# File 'lib/lita/handlers/tweet/data.rb', line 7

def config
  @config
end

#redisObject

Returns the value of attribute redis

Returns:

  • (Object)

    the current value of redis



7
8
9
# File 'lib/lita/handlers/tweet/data.rb', line 7

def redis
  @redis
end

#robotObject

Returns the value of attribute robot

Returns:

  • (Object)

    the current value of robot



7
8
9
# File 'lib/lita/handlers/tweet/data.rb', line 7

def robot
  @robot
end

Instance Method Details

#account(username) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/lita/handlers/tweet/data.rb', line 16

def (username)
  return nil if username.nil?

  hash = redis.hgetall("twitter_accounts:#{username}")
  return nil if hash.empty?

  data = hash.each_with_object({}){|(k,v),h| h[k.to_sym] = v }
  .new(**data.merge(config: config))
end

#add_account(token, secret, username = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lita/handlers/tweet/data.rb', line 26

def (token, secret, username = nil)
   = .new(token: token, secret: secret,
                        config: config, username: username)
  username ||= .lookup_username!

  redis.setnx("default_username", username)
  redis.sadd("twitter_accounts", username)
  redis.hmset("twitter_accounts:#{username}",
    "username", username,
    "token", token,
    "secret", secret
  )

  
end

#authorize_account(token, verifier) ⇒ Object



103
104
105
106
107
# File 'lib/lita/handlers/tweet/data.rb', line 103

def (token, verifier)
  request_token = find_request_token(token)
  access_token = request_token.get_access_token(oauth_verifier: verifier)
  (access_token.token, access_token.secret)
end

#bot_uri(path = "") ⇒ Object



109
110
111
# File 'lib/lita/handlers/tweet/data.rb', line 109

def bot_uri(path = "")
  base_uri + path
end

#channel_mapObject



60
61
62
63
64
# File 'lib/lita/handlers/tweet/data.rb', line 60

def channel_map
  redis.smembers("channels").map do |name|
    [name, get_channel_map(name)]
  end.to_h
end

#clear_channel_map(channel) ⇒ Object



81
82
83
84
# File 'lib/lita/handlers/tweet/data.rb', line 81

def clear_channel_map(channel)
  redis.del("channels:#{channel}")
  redis.srem("channels", channel)
end

#create_request_token(callback_path) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/lita/handlers/tweet/data.rb', line 91

def create_request_token(callback_path)
  request_token = consumer.get_request_token(
    oauth_callback: bot_uri(callback_path).to_s)
  request_hash = request_token.params

  key = "request_token:#{request_hash[:oauth_token]}"
  redis.hmset(key, *request_hash.to_a.flatten)
  redis.expire(key, 120)

  request_token
end

#default_accountObject



12
13
14
# File 'lib/lita/handlers/tweet/data.rb', line 12

def 
  (redis.get("default_username"))
end

#get_channel_account(channel) ⇒ Object



66
67
68
69
# File 'lib/lita/handlers/tweet/data.rb', line 66

def (channel)
  return nil unless channel
  (get_channel_map(channel))
end

#get_channel_map(channel) ⇒ Object



71
72
73
# File 'lib/lita/handlers/tweet/data.rb', line 71

def get_channel_map(channel)
  redis.get("channels:#{channel}")
end

#get_last_tweet(username) ⇒ Object



56
57
58
# File 'lib/lita/handlers/tweet/data.rb', line 56

def get_last_tweet(username)
  redis.hget("twitter_accounts:#{username}", "last_tweet")
end

#remove_account(username) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/lita/handlers/tweet/data.rb', line 42

def (username)
  redis.del("twitter_accounts:#{username}")
  redis.srem("twitter_accounts", username)

  if redis.get("default_username") == username
    next_username = usernames.first
    redis.set("default_username", next_username)
  end
end

#set_channel_map(channel, username) ⇒ Object



75
76
77
78
79
# File 'lib/lita/handlers/tweet/data.rb', line 75

def set_channel_map(channel, username)
  return false unless usernames.include?(username)
  redis.sadd("channels", channel)
  redis.set("channels:#{channel}", username)
end

#set_default(username) ⇒ Object



86
87
88
89
# File 'lib/lita/handlers/tweet/data.rb', line 86

def set_default(username)
  return false unless usernames.include?(username)
  redis.set("default_username", username)
end

#set_last_tweet(username, tweet_id) ⇒ Object



52
53
54
# File 'lib/lita/handlers/tweet/data.rb', line 52

def set_last_tweet(username, tweet_id)
  redis.hset("twitter_accounts:#{username}", "last_tweet", tweet_id)
end

#usernamesObject



8
9
10
# File 'lib/lita/handlers/tweet/data.rb', line 8

def usernames
  redis.smembers("twitter_accounts")
end