lib/telegram/bot/api.rb in telegram-bot-ruby-1.0.0 vs lib/telegram/bot/api.rb in telegram-bot-ruby-2.0.0
- old
+ new
@@ -1,67 +1,53 @@
# frozen_string_literal: true
module Telegram
module Bot
class Api
- ENDPOINTS = %w[
- getUpdates setWebhook deleteWebhook getWebhookInfo getMe sendMessage
- forwardMessage sendPhoto sendAudio sendDocument sendVideo sendVoice
- sendVideoNote sendMediaGroup sendLocation editMessageLiveLocation
- stopMessageLiveLocation sendVenue sendContact sendChatAction
- getUserProfilePhotos getFile kickChatMember unbanChatMember
- restrictChatMember promoteChatMember leaveChat getChat
- getChatAdministrators exportChatInviteLink setChatPhoto deleteChatPhoto
- setChatTitle setChatDescription pinChatMessage unpinChatMessage
- getChatMembersCount getChatMember setChatStickerSet deleteChatStickerSet
- answerCallbackQuery editMessageText editMessageCaption
- editMessageReplyMarkup deleteMessage sendSticker getStickerSet
- uploadStickerFile createNewStickerSet addStickerToSet
- setStickerPositionInSet deleteStickerFromSet answerInlineQuery
- sendInvoice answerShippingQuery answerPreCheckoutQuery
- sendGame setGameScore getGameHighScores setPassportDataErrors
- editMessageMedia sendAnimation sendPoll stopPoll setChatPermissions
- setChatAdministratorCustomTitle sendDice getMyCommands setMyCommands
- deleteMyCommands setStickerSetThumb logOut close copyMessage
- createChatInviteLink editChatInviteLink revokeChatInviteLink
- approveChatJoinRequest declineChatJoinRequest banChatSenderChat
- unbanChatSenderChat answerWebAppQuery setChatMenuButton
- getChatMenuButton setMyDefaultAdministratorRights
- getMyDefaultAdministratorRights createInvoiceLink editGeneralForumTopic
- closeGeneralForumTopic reopenGeneralForumTopic hideGeneralForumTopic
- unhideGeneralForumTopic
- ].freeze
-
attr_reader :token, :url, :environment
def initialize(token, url: 'https://api.telegram.org', environment: :production)
@token = token
@url = url
@environment = environment.downcase.to_sym
end
+ def connection
+ @connection ||= Faraday.new(url: url) do |faraday|
+ faraday.request :multipart
+ faraday.request :url_encoded
+ faraday.adapter Telegram::Bot.configuration.adapter
+ faraday.options.timeout = Telegram::Bot.configuration.connection_timeout
+ faraday.options.open_timeout = Telegram::Bot.configuration.connection_open_timeout
+ end
+ end
+
def method_missing(method_name, *args, &block)
endpoint = method_name.to_s
endpoint = camelize(endpoint) if endpoint.include?('_')
- ENDPOINTS.include?(endpoint) ? call(endpoint, *args) : super
+ return super unless ENDPOINTS.key?(endpoint)
+
+ result = call(endpoint, *args)
+
+ return result['ok'] unless (result = result['result'])
+
+ ENDPOINTS[endpoint].call(result)
end
def respond_to_missing?(*args)
method_name = args[0].to_s
method_name = camelize(method_name) if method_name.include?('_')
- ENDPOINTS.include?(method_name) || super
+ ENDPOINTS.key?(method_name) || super
end
def call(endpoint, raw_params = {})
params = build_params(raw_params)
path = build_path(endpoint)
- response = conn.post(path, params)
- unless response.status == 200
- raise Exceptions::ResponseError.new(response), 'Telegram API has returned the error.'
- end
+ response = connection.post(path, params)
+ raise Exceptions::ResponseError.new(response: response) unless response.status == 200
JSON.parse(response.body)
end
private
@@ -92,19 +78,9 @@
def camelize(method_name)
words = method_name.split('_')
words.drop(1).map(&:capitalize!)
words.join
- end
-
- def conn
- @conn ||= Faraday.new(url: url) do |faraday|
- faraday.request :multipart
- faraday.request :url_encoded
- faraday.adapter Telegram::Bot.configuration.adapter
- faraday.options.timeout = Telegram::Bot.configuration.connection_timeout
- faraday.options.open_timeout = Telegram::Bot.configuration.connection_open_timeout
- end
end
end
end
end