Class: Telegram::Bot::Client
- Inherits:
-
Object
- Object
- Telegram::Bot::Client
- Defined in:
- lib/telegram/bot/client.rb,
lib/telegram/bot/client/typed_response.rb
Direct Known Subclasses
Defined Under Namespace
Modules: TypedResponse
Constant Summary collapse
- URL_TEMPLATE =
'https://api.telegram.org/bot%s/'.freeze
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.
Class Method Summary collapse
-
.prepare_body(body) ⇒ Object
Encodes nested hashes as json.
-
.typed_response! ⇒ Object
Prepend TypedResponse module.
-
.wrap(input) ⇒ Object
Accepts different options to initialize bot.
Instance Method Summary collapse
- #debug!(dev = STDOUT) ⇒ Object
- #debug_off! ⇒ Object
-
#http_request(uri, body) ⇒ Object
Endpoint for low-level request.
-
#initialize(token, username = nil) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
-
#request(action, body = {}) ⇒ Object
rubocop:disable PerceivedComplexity.
Constructor Details
#initialize(token, username = nil) ⇒ Client
Returns a new instance of Client.
46 47 48 49 50 51 |
# File 'lib/telegram/bot/client.rb', line 46 def initialize(token, username = nil) @client = HTTPClient.new @token = token @username = username @base_uri = format URL_TEMPLATE, token end |
Instance Attribute Details
#base_uri ⇒ Object (readonly)
Returns the value of attribute base_uri.
44 45 46 |
# File 'lib/telegram/bot/client.rb', line 44 def base_uri @base_uri end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
44 45 46 |
# File 'lib/telegram/bot/client.rb', line 44 def client @client end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
44 45 46 |
# File 'lib/telegram/bot/client.rb', line 44 def token @token end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
44 45 46 |
# File 'lib/telegram/bot/client.rb', line 44 def username @username end |
Class Method Details
.prepare_body(body) ⇒ Object
Encodes nested hashes as json.
36 37 38 39 40 41 |
# File 'lib/telegram/bot/client.rb', line 36 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.
31 32 33 |
# File 'lib/telegram/bot/client.rb', line 31 def typed_response! prepend TypedResponse end |
.wrap(input) ⇒ Object
Accepts different options to initialize bot.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/telegram/bot/client.rb', line 15 def wrap(input) case input when self then input when Array then input.map(&method(__callee__)) when Hash then input = input.stringify_keys new input['token'], input['username'] when Symbol Telegram.bots[input] or raise "Bot #{input} not configured, check Telegram.bots_config." else new(input) end end |
Instance Method Details
#debug!(dev = STDOUT) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/telegram/bot/client.rb', line 53 def debug!(dev = STDOUT) if block_given? begin old_dev = client.debug_dev client.debug_dev = dev yield ensure client.debug_dev = old_dev end else client.debug_dev = dev end end |
#debug_off! ⇒ Object
67 68 69 |
# File 'lib/telegram/bot/client.rb', line 67 def debug_off! client.debug_dev = nil end |
#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.
109 110 111 |
# File 'lib/telegram/bot/client.rb', line 109 def http_request(uri, body) client.post(uri, body) end |
#inspect ⇒ Object
113 114 115 |
# File 'lib/telegram/bot/client.rb', line 113 def inspect "#<#{self.class.name}##{object_id}(#{@username})>" end |
#request(action, body = {}) ⇒ Object
rubocop:disable PerceivedComplexity
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/telegram/bot/client.rb', line 71 def request(action, body = {}) # rubocop:disable PerceivedComplexity 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 = "#{res.reason}: #{result && result['description'] || '-'}" if result # NotFound is raised only for valid responses from Telegram raise NotFound, err_msg if 404 == status raise StaleChat, err_msg if StaleChat.match_response?(result) end raise Error, err_msg end |