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



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

def (username)
  hash = redis.hgetall("twitter_accounts:#{username}")
  data = hash.each_with_object({}){|(k,v),h| h[k.to_sym] = v }
  Account.new(**data.merge(config: config))
end

#add_account(token, secret) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lita/handlers/tweet/data.rb', line 18

def (token, secret)
   = Account.new(token: token, secret: secret, config: config)
  username = .username

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

  
end

#authorize_account(token, verifier) ⇒ Object



57
58
59
60
61
# File 'lib/lita/handlers/tweet/data.rb', line 57

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



63
64
65
# File 'lib/lita/handlers/tweet/data.rb', line 63

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

#create_request_token(callback_path) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lita/handlers/tweet/data.rb', line 45

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

#get_last_tweet(username) ⇒ Object



41
42
43
# File 'lib/lita/handlers/tweet/data.rb', line 41

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

#remove_account(username) ⇒ Object



32
33
34
35
# File 'lib/lita/handlers/tweet/data.rb', line 32

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

#set_last_tweet(username, tweet_id) ⇒ Object



37
38
39
# File 'lib/lita/handlers/tweet/data.rb', line 37

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