Class: Telegram::Bot::Client
- Inherits:
-
Object
- Object
- Telegram::Bot::Client
- Extended by:
- Initializers
- Includes:
- Async, Botan::ClientHelpers, ApiHelper, DebugClient
- Defined in:
- lib/telegram/bot/client.rb,
lib/telegram/bot/client/api_helper.rb,
lib/telegram/bot/client/typed_response.rb
Direct Known Subclasses
Defined Under Namespace
Modules: ApiHelper, TypedResponse
Constant Summary collapse
- URL_TEMPLATE =
'https://api.telegram.org/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
Attributes included from Botan::ClientHelpers
Class Method Summary collapse
- .by_id(id) ⇒ Object
- .error_for_response(response) ⇒ Object
- .prepare_async_args(action, body = {}) ⇒ Object
-
.prepare_body(body) ⇒ Object
Encodes nested hashes as json.
-
.typed_response! ⇒ Object
Prepend TypedResponse module.
Instance Method Summary collapse
-
#http_request(uri, body) ⇒ Object
Endpoint for low-level request.
-
#initialize(token = nil, username = nil, **options) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
- #request(action, body = {}) ⇒ Object
Methods included from Initializers
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, **options) ⇒ Client
Returns a new instance of Client.
55 56 57 58 59 60 |
# File 'lib/telegram/bot/client.rb', line 55 def initialize(token = nil, username = nil, **) @client = HTTPClient.new @token = token || [:token] @username = username || [:username] @base_uri = format(URL_TEMPLATE, token: self.token) end |
Instance Attribute Details
#base_uri ⇒ Object (readonly)
Returns the value of attribute base_uri.
53 54 55 |
# File 'lib/telegram/bot/client.rb', line 53 def base_uri @base_uri end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
53 54 55 |
# File 'lib/telegram/bot/client.rb', line 53 def client @client end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
53 54 55 |
# File 'lib/telegram/bot/client.rb', line 53 def token @token end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
53 54 55 |
# File 'lib/telegram/bot/client.rb', line 53 def username @username end |
Class Method Details
.by_id(id) ⇒ Object
19 20 21 |
# File 'lib/telegram/bot/client.rb', line 19 def by_id(id) Telegram.bots[id] end |
.error_for_response(response) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/telegram/bot/client.rb', line 40 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
36 37 38 |
# File 'lib/telegram/bot/client.rb', line 36 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.
29 30 31 32 33 34 |
# File 'lib/telegram/bot/client.rb', line 29 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.
24 25 26 |
# File 'lib/telegram/bot/client.rb', line 24 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.
71 72 73 |
# File 'lib/telegram/bot/client.rb', line 71 def http_request(uri, body) client.post(uri, body) end |
#inspect ⇒ Object
75 76 77 |
# File 'lib/telegram/bot/client.rb', line 75 def inspect "#<#{self.class.name}##{object_id}(#{@username})>" end |
#request(action, body = {}) ⇒ Object
62 63 64 65 66 |
# File 'lib/telegram/bot/client.rb', line 62 def request(action, body = {}) response = http_request("#{base_uri}#{action}", self.class.prepare_body(body)) raise self.class.error_for_response(response) if response.status >= 300 JSON.parse(response.body) end |