Class: Botaku::Client

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

Defined Under Namespace

Classes: WebApiModule

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/botaku/client.rb', line 12

def initialize(opts)

  fail ArgumentError.new('missing :token') unless opts[:token]

  @opts = opts

  @base_uri = 'https://slack.com/api'
  @modules = {}
  @http_client = HTTPClient.new
  @ws_client = nil
  @rtm = nil
  @handlers = {}

  @http_client.debug_dev = $stderr if ENV['BOTAKU_DEBUG_HTTP']

  if @opts[:token].match(/[\\\/.]/) || @opts[:token].count('-') != 2
    @opts[:token_path] = @opts[:token]
    @opts[:token] = File.read(@opts[:token]).strip
  end
end

Instance Method Details

#_selfObject



33
34
35
36
# File 'lib/botaku/client.rb', line 33

def _self

  @rtm['self']
end

#channel(o) ⇒ Object



124
# File 'lib/botaku/client.rb', line 124

def channel(o); obj(o, :channel); end

#channel_id(o) ⇒ Object



125
# File 'lib/botaku/client.rb', line 125

def channel_id(o); obj_id(o, :channel); end

#channel_name(o) ⇒ Object



126
# File 'lib/botaku/client.rb', line 126

def channel_name(o); obj_name(o, :channel); end

#channelsObject



86
# File 'lib/botaku/client.rb', line 86

def channels; @rtm['channels']; end

#groupsObject



87
# File 'lib/botaku/client.rb', line 87

def groups; @rtm['groups']; end

#obj(o, cat = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/botaku/client.rb', line 96

def obj(o, cat=nil)

  if o.is_a?(Hash)
    cat = cat ? cat.to_s : nil
    o = o[cat]
  end

  case o
  when /\AU[0-9A-Z]+\z/
    users.find { |u| u['id'] == o }
  when /\A@[^\s]+\z/
    users.find { |u| u['name'] == o[1..-1] }
  when /\A[CG][0-9A-Z]+\z/
    (channels + groups).find { |c| c['id'] == o }
  when /\A#[^\s]+\z/
    (channels + groups).find { |c| c['name'] == o[1..-1] }
  else
    objects[o] ||
    objects.values.find { |x| x['name'] == o }
  end
end

#obj_id(o, cat = nil) ⇒ Object



118
# File 'lib/botaku/client.rb', line 118

def obj_id(o, cat=nil); h = obj(o, cat); h ? h['id'] : nil; end

#obj_name(o, cat = nil) ⇒ Object



119
# File 'lib/botaku/client.rb', line 119

def obj_name(o, cat=nil); h = obj(o, cat); h ? h['name'] : nil; end

#objectsObject



90
91
92
93
94
# File 'lib/botaku/client.rb', line 90

def objects

  @objects ||=
    (channels + groups + users).inject({}) { |h, o| h[o['id']] = o; h }
end

#on(type, &block) ⇒ Object



45
46
47
48
# File 'lib/botaku/client.rb', line 45

def on(type, &block)

  (@handlers[type] ||= []) << block
end

#runObject Also known as: join



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/botaku/client.rb', line 50

def run

  EM.run do
    @rtm = rtm.start
    @ws_client = Faye::WebSocket::Client.new(@rtm['url'])
    [ :open, :error, :close ].each do |event_type|
      @ws_client.on(event_type) { |event| dispatch(event_type, event) }
    end
    @ws_client.on(:message) { |event| dispatch_message(event) }
  end
end

#say(*as) ⇒ Object

say(‘hello’) say(‘hello’, ‘#test_channel’) say(‘hello’, channel: ‘C12345ABC’) say(‘hello’, channel: ‘#test_channel’)



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/botaku/client.rb', line 68

def say(*as)

  args = rework_args(as)
  args[:as_user] = true unless args.has_key?(:as_user)

  args[:text] = args[:text]
    .gsub(/&/, '&amp;').gsub(/</, '&lt;').gsub(/>/, '&gt;')

  args[:mrkdwn] = true

  #chat.postMessage(args)

  args[:type] = 'message'
  args[:id] = next_id

  do_send(args)
end

#typing(args) ⇒ Object



128
129
130
131
# File 'lib/botaku/client.rb', line 128

def typing(args)

  do_send(type: 'typing', id: next_id, channel: channel_id(args[:channel]))
end

#user(o) ⇒ Object



121
# File 'lib/botaku/client.rb', line 121

def user(o); obj(o, :user); end

#user_id(o) ⇒ Object



122
# File 'lib/botaku/client.rb', line 122

def user_id(o); obj_id(o, :user); end

#user_name(o) ⇒ Object



123
# File 'lib/botaku/client.rb', line 123

def user_name(o); obj_name(o, :user); end

#usersObject



88
# File 'lib/botaku/client.rb', line 88

def users; @rtm['users']; end