Class: Telegram::Bot::Client

Inherits:
Object
  • Object
show all
Extended by:
Initializers
Includes:
Async, Botan::ClientHelpers, DebugClient
Defined in:
lib/telegram/bot/client.rb,
lib/telegram/bot/client/typed_response.rb

Direct Known Subclasses

ClientStub

Defined Under Namespace

Modules: TypedResponse

Constant Summary collapse

URL_TEMPLATE =
'https://api.telegram.org/bot%s/'.freeze

Constants included from Async

Async::MISSING_VALUE

Instance Attribute Summary collapse

Attributes included from Async

#id

Attributes included from Botan::ClientHelpers

#botan

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, username = nil, **options) ⇒ Client

Returns a new instance of Client.



42
43
44
45
46
47
# File 'lib/telegram/bot/client.rb', line 42

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

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



40
41
42
# File 'lib/telegram/bot/client.rb', line 40

def base_uri
  @base_uri
end

#clientObject (readonly)

Returns the value of attribute client.



40
41
42
# File 'lib/telegram/bot/client.rb', line 40

def client
  @client
end

#tokenObject (readonly)

Returns the value of attribute token.



40
41
42
# File 'lib/telegram/bot/client.rb', line 40

def token
  @token
end

#usernameObject (readonly)

Returns the value of attribute username.



40
41
42
# File 'lib/telegram/bot/client.rb', line 40

def username
  @username
end

Class Method Details

.by_id(id) ⇒ Object



18
19
20
# File 'lib/telegram/bot/client.rb', line 18

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

.prepare_async_args(action, body = {}) ⇒ Object



35
36
37
# File 'lib/telegram/bot/client.rb', line 35

def prepare_async_args(action, body = {})
  [action.to_s, Async.prepare_hash(prepare_body(body))]
end

.prepare_body(body) ⇒ Object

Encodes nested hashes as json.



28
29
30
31
32
33
# File 'lib/telegram/bot/client.rb', line 28

def prepare_body(body)
  body = body.dup
  body.each do |k, val|
    body[k] = val.to_json if val.is_a?(Hash) || val.is_a?(Array)
  end
end

.typed_response!Object

Prepend TypedResponse module.



23
24
25
# File 'lib/telegram/bot/client.rb', line 23

def typed_response!
  prepend TypedResponse
end

Instance Method Details

#http_request(uri, body) ⇒ Object

Endpoint for low-level request. For easy host highjacking & instrumentation. Params are not used directly but kept for instrumentation purpose. You probably don’t want to use this method directly.



112
113
114
# File 'lib/telegram/bot/client.rb', line 112

def http_request(uri, body)
  client.post(uri, body)
end

#inspectObject



116
117
118
# File 'lib/telegram/bot/client.rb', line 116

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

#request(action, body = {}) ⇒ Object

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/telegram/bot/client.rb', line 49

def request(action, body = {})
  res = http_request("#{base_uri}#{action}", self.class.prepare_body(body))
  status = res.status
  return JSON.parse(res.body) if 300 > status
  result = JSON.parse(res.body) rescue nil # rubocop:disable RescueModifier
  err_msg = result && result['description'] || '-'
  if result
    # This errors are raised only for valid responses from Telegram
    case status
    when 403 then raise Forbidden, err_msg
    when 404 then raise NotFound, err_msg
    end
  end
  raise Error, "#{res.reason}: #{err_msg}"
end