Class: Telegram::Bot::Botan

Inherits:
Object
  • Object
show all
Extended by:
Initializers
Includes:
Async, DebugClient
Defined in:
lib/telegram/bot/botan.rb,
lib/telegram/bot/botan/client_helpers.rb,
lib/telegram/bot/botan/controller_helpers.rb

Defined Under Namespace

Modules: ClientHelpers, ControllerHelpers Classes: Error

Constant Summary collapse

TRACK_URI =
'https://api.botan.io/track'.freeze

Constants included from Async

Async::MISSING_VALUE

Instance Attribute Summary collapse

Attributes included from Async

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Initializers

by_id, wrap

Methods included from DebugClient

#debug!, #debug_off!

Methods included from Async

#async, #async=, prepare_hash, prepended, thread_store

Constructor Details

#initialize(token = nil, **options) ⇒ Botan

Returns a new instance of Botan.



26
27
28
29
# File 'lib/telegram/bot/botan.rb', line 26

def initialize(token = nil, **options)
  @client = HTTPClient.new
  @token = token || options[:token]
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



24
25
26
# File 'lib/telegram/bot/botan.rb', line 24

def client
  @client
end

#tokenObject (readonly)

Returns the value of attribute token.



24
25
26
# File 'lib/telegram/bot/botan.rb', line 24

def token
  @token
end

Class Method Details

.by_id(id) ⇒ Object



15
16
17
# File 'lib/telegram/bot/botan.rb', line 15

def by_id(id)
  Telegram.botans[id]
end

.prepare_async_args(method, uri, query = {}, body = nil) ⇒ Object



19
20
21
# File 'lib/telegram/bot/botan.rb', line 19

def prepare_async_args(method, uri, query = {}, body = nil)
  [method.to_s, uri.to_s, Async.prepare_hash(query), body]
end

Instance Method Details

#http_request(method, uri, query, body) ⇒ Object



44
45
46
# File 'lib/telegram/bot/botan.rb', line 44

def http_request(method, uri, query, body)
  client.request(method, uri, query, body)
end

#inspectObject



48
49
50
# File 'lib/telegram/bot/botan.rb', line 48

def inspect
  "#<#{self.class.name}##{object_id}(#{@id})>"
end

#request(method, uri, query = {}, body = nil) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
# File 'lib/telegram/bot/botan.rb', line 35

def request(method, uri, query = {}, body = nil)
  res = http_request(method, uri, query.merge(token: token), body)
  status = res.status
  return JSON.parse(res.body) if 300 > status
  result = JSON.parse(res.body) rescue nil # rubocop:disable RescueModifier
  err_msg = "#{res.reason}: #{result && result['info'] || '-'}"
  raise Error, err_msg
end

#track(event, uid, payload = {}) ⇒ Object



31
32
33
# File 'lib/telegram/bot/botan.rb', line 31

def track(event, uid, payload = {})
  request(:post, TRACK_URI, {name: event, uid: uid}, payload.to_json)
end