Class: RSlack::APIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/rslack/api.rb

Constant Summary collapse

BASEURL =
"https://slack.com/api/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: BASEURL, token:) ⇒ APIClient

Returns a new instance of APIClient.



14
15
16
17
# File 'lib/rslack/api.rb', line 14

def initialize(url: BASEURL, token:)
  @url   = url
  @token = token
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



12
13
14
# File 'lib/rslack/api.rb', line 12

def token
  @token
end

#urlObject (readonly)

Returns the value of attribute url.



11
12
13
# File 'lib/rslack/api.rb', line 11

def url
  @url
end

Instance Method Details

#call(endpoint, **params) ⇒ Object



40
41
42
43
# File 'lib/rslack/api.rb', line 40

def call(endpoint, **params)
  resp = HTTP.post(BASEURL + endpoint, params: { token: @token }.merge(params))
  JSON.parse(resp.body)
end

#channel(id) ⇒ Object



36
37
38
# File 'lib/rslack/api.rb', line 36

def channel(id)
  call('channels.info', channel: id)
end

#meObject



24
25
26
# File 'lib/rslack/api.rb', line 24

def me
  call('auth.test')
end

#post(text: '', **params) ⇒ Object



28
29
30
# File 'lib/rslack/api.rb', line 28

def post(text: '', **params)
  call('chat.postMessage', params.merge(text: text))
end

#user(id) ⇒ Object



32
33
34
# File 'lib/rslack/api.rb', line 32

def user(id)
  call('users.info', user: id)
end

#websocketObject



19
20
21
22
# File 'lib/rslack/api.rb', line 19

def websocket
  rtm = call('rtm.start')
  Faye::WebSocket::Client.new(rtm['url'], {}, { ping: 60 })
end