Class: Lita::Handlers::Tweet::Data
- Inherits:
-
Struct
- Object
- Struct
- Lita::Handlers::Tweet::Data
- Defined in:
- lib/lita/handlers/tweet/data.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#redis ⇒ Object
Returns the value of attribute redis.
-
#robot ⇒ Object
Returns the value of attribute robot.
Instance Method Summary collapse
- #account(username) ⇒ Object
- #add_account(token, secret, username = nil) ⇒ Object
- #authorize_account(token, verifier) ⇒ Object
- #bot_uri(path = "") ⇒ Object
- #channel_map ⇒ Object
- #clear_channel_map(channel) ⇒ Object
- #create_request_token(callback_path) ⇒ Object
- #default_account ⇒ Object
- #get_channel_account(channel) ⇒ Object
- #get_channel_map(channel) ⇒ Object
- #get_last_tweet(username) ⇒ Object
- #remove_account(username) ⇒ Object
- #set_channel_map(channel, username) ⇒ Object
- #set_default(username) ⇒ Object
- #set_last_tweet(username, tweet_id) ⇒ Object
- #usernames ⇒ Object
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config
7 8 9 |
# File 'lib/lita/handlers/tweet/data.rb', line 7 def config @config end |
#redis ⇒ Object
Returns the value of attribute redis
7 8 9 |
# File 'lib/lita/handlers/tweet/data.rb', line 7 def redis @redis end |
#robot ⇒ Object
Returns the value of attribute 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 account(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 } Account.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 add_account(token, secret, username = nil) account = Account.new(token: token, secret: secret, config: config, username: username) username ||= account.lookup_username! redis.setnx("default_username", username) redis.sadd("twitter_accounts", username) redis.hmset("twitter_accounts:#{username}", "username", username, "token", token, "secret", secret ) account 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) add_account(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_map ⇒ Object
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_account ⇒ Object
12 13 14 |
# File 'lib/lita/handlers/tweet/data.rb', line 12 def default_account account(redis.get("default_username")) end |
#get_channel_account(channel) ⇒ Object
66 67 68 69 |
# File 'lib/lita/handlers/tweet/data.rb', line 66 def get_channel_account(channel) return nil unless channel account(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 remove_account(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 |
#usernames ⇒ Object
8 9 10 |
# File 'lib/lita/handlers/tweet/data.rb', line 8 def usernames redis.smembers("twitter_accounts") end |