Class: Telegram::Bot::Client
- Inherits:
-
Object
- Object
- Telegram::Bot::Client
- Includes:
- Async, ApiHelper, DebugClient
- Defined in:
- lib/telegram/bot/client.rb,
lib/telegram/bot/client/api_helper.rb,
lib/telegram/bot/client/typed_response.rb,
lib/telegram/bot/client/request_body_formatter.rb
Direct Known Subclasses
Defined Under Namespace
Modules: ApiHelper, RequestBodyFormatter, TypedResponse
Constant Summary collapse
- SERVER =
'https://api.telegram.org'.freeze
- URL_TEMPLATE =
'%<server>s/bot%<token>s/'.freeze
Constants included from ApiHelper
Constants included from Async
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
readonly
Returns the value of attribute base_uri.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Attributes included from Async
Class Method Summary collapse
- .by_id(id) ⇒ Object
- .error_for_response(response) ⇒ Object
- .prepare_async_args(action, body = {}) ⇒ Object
-
.typed_response! ⇒ Object
Prepend TypedResponse module.
-
.wrap(input, **options) ⇒ Object
Accepts different options to initialize bot.
Instance Method Summary collapse
-
#http_request(uri, body) ⇒ Object
Endpoint for low-level request.
-
#initialize(token = nil, username = nil, server: SERVER, **options) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
- #request(action, body = {}) ⇒ Object
Methods included from ApiHelper
Methods included from DebugClient
Methods included from Async
#async, #async=, prepare_hash, prepended, thread_store
Constructor Details
#initialize(token = nil, username = nil, server: SERVER, **options) ⇒ Client
Returns a new instance of Client.
58 59 60 61 62 63 |
# File 'lib/telegram/bot/client.rb', line 58 def initialize(token = nil, username = nil, server: SERVER, **) @client = HTTPClient.new @token = token || [:token] @username = username || [:username] @base_uri = format(URL_TEMPLATE, server: server, token: self.token) end |
Instance Attribute Details
#base_uri ⇒ Object (readonly)
Returns the value of attribute base_uri.
56 57 58 |
# File 'lib/telegram/bot/client.rb', line 56 def base_uri @base_uri end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
56 57 58 |
# File 'lib/telegram/bot/client.rb', line 56 def client @client end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
56 57 58 |
# File 'lib/telegram/bot/client.rb', line 56 def token @token end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
56 57 58 |
# File 'lib/telegram/bot/client.rb', line 56 def username @username end |
Class Method Details
.by_id(id) ⇒ Object
30 31 32 |
# File 'lib/telegram/bot/client.rb', line 30 def by_id(id) Telegram.bots[id] end |
.error_for_response(response) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/telegram/bot/client.rb', line 43 def error_for_response(response) result = JSON.parse(response.body) rescue nil # rubocop:disable RescueModifier return Error.new(response.reason) unless result = result['description'] || '-' # This errors are raised only for valid responses from Telegram case response.status when 403 then Forbidden.new() when 404 then NotFound.new() else Error.new("#{response.reason}: #{}") end end |
.prepare_async_args(action, body = {}) ⇒ Object
39 40 41 |
# File 'lib/telegram/bot/client.rb', line 39 def prepare_async_args(action, body = {}) [action.to_s, Async.prepare_hash(RequestBodyFormatter.format(body, action))] end |
.typed_response! ⇒ Object
Prepend TypedResponse module.
35 36 37 |
# File 'lib/telegram/bot/client.rb', line 35 def typed_response! prepend TypedResponse end |
.wrap(input, **options) ⇒ Object
Accepts different options to initialize bot.
21 22 23 24 25 26 27 28 |
# File 'lib/telegram/bot/client.rb', line 21 def wrap(input, **) case input when Symbol then by_id(input) or raise "#{name} #{input.inspect} not configured" when self then input when Hash then new(**input.symbolize_keys, **) else new(input, **) end 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.
74 75 76 |
# File 'lib/telegram/bot/client.rb', line 74 def http_request(uri, body) client.post(uri, body) end |
#inspect ⇒ Object
78 79 80 |
# File 'lib/telegram/bot/client.rb', line 78 def inspect "#<#{self.class.name}##{object_id}(#{@username})>" end |
#request(action, body = {}) ⇒ Object
65 66 67 68 69 |
# File 'lib/telegram/bot/client.rb', line 65 def request(action, body = {}) response = http_request("#{base_uri}#{action}", RequestBodyFormatter.format(body, action)) raise self.class.error_for_response(response) if response.status >= 300 JSON.parse(response.body) end |