Class: Lita::Adapters::Twitter::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/lita/adapters/twitter/connector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(robot, api_key: nil, api_secret: nil, access_token: nil, access_token_secret: nil) ⇒ Connector

Returns a new instance of Connector.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/lita/adapters/twitter/connector.rb', line 7

def initialize(robot, api_key: nil, api_secret: nil, access_token: nil, access_token_secret: nil)
  @robot = robot
  @streaming_client = ::Twitter::Streaming::Client.new do |config|
    config.consumer_key        = api_key
    config.consumer_secret     = api_secret
    config.access_token        = access_token
    config.access_token_secret = access_token_secret
  end

  @rest_client = ::Twitter::REST::Client.new do |config|
    config.consumer_key        = api_key
    config.consumer_secret     = api_secret
    config.access_token        = access_token
    config.access_token_secret = access_token_secret
  end
end

Instance Attribute Details

#rest_clientObject (readonly)

Returns the value of attribute rest_client.



23
24
25
# File 'lib/lita/adapters/twitter/connector.rb', line 23

def rest_client
  @rest_client
end

#robotObject (readonly)

Returns the value of attribute robot.



23
24
25
# File 'lib/lita/adapters/twitter/connector.rb', line 23

def robot
  @robot
end

#streaming_clientObject (readonly)

Returns the value of attribute streaming_client.



23
24
25
# File 'lib/lita/adapters/twitter/connector.rb', line 23

def streaming_client
  @streaming_client
end

Instance Method Details

#connectObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lita/adapters/twitter/connector.rb', line 25

def connect
  streaming_client.user do |object|
    case object
    when ::Twitter::Tweet
      tweet = object
      if tweet.user.screen_name != robot.name
        text = tweet.text.dup
        mention = false
        if text.match(/\A@#{robot.name} /)
          text.gsub!(/\A@#{robot.name} /, '')
          mention = true
        end
        user    = User.new(tweet.user.id, name: tweet.user.screen_name, mention: mention)
        source  = Source.new(user: user, room: tweet.id.to_s)
        message = Message.new(robot, text, source)
        message.command! if mention
        robot.receive(message)
      end
    when ::Twitter::DirectMessage
      dm = object
      dm.text
      if dm.sender.screen_name != robot.name
        user    = User.new(dm.sender.id, name: dm.sender.screen_name)
        source  = Source.new(user: user)
        message = Message.new(robot, dm.text.dup, source)
        message.command!
        robot.receive(message)
      end
    when ::Twitter::Streaming::Event
      event = object
      on_streaming_event(event) if event.source.screen_name != robot.name
    end
  end
end

#join(user) ⇒ Object



82
83
84
# File 'lib/lita/adapters/twitter/connector.rb', line 82

def join(user)
  rest_client.follow(user)
end

#message(target, strings) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/lita/adapters/twitter/connector.rb', line 60

def message(target, strings)
  tries = 0
  text  = strings.join("\n")[0...137]
  begin
    if target.private_message
      rest_client.create_direct_message(target.user.name, text)
    elsif target.user
      rest_client.update(text, in_reply_to_status_id: target.room)
    else
      rest_client.update(text)
    end
  rescue ::Twitter::Error::DuplicateStatus => e
    tries += 1
    text  += "" #U+2800
    retry if tries <= 3
  end
end

#part(user) ⇒ Object



86
87
88
# File 'lib/lita/adapters/twitter/connector.rb', line 86

def part(user)
  rest_client.unfollow(user)
end

#update_name(name) ⇒ Object



78
79
80
# File 'lib/lita/adapters/twitter/connector.rb', line 78

def update_name(name)
  rest_client.update_profile(name: topic)
end