lib/telegram/bot/api.rb in telegram-bot-ruby-0.21.1 vs lib/telegram/bot/api.rb in telegram-bot-ruby-0.22.0

- old
+ new

@@ -27,15 +27,16 @@ unbanChatSenderChat answerWebAppQuery setChatMenuButton getChatMenuButton setMyDefaultAdministratorRights getMyDefaultAdministratorRights createInvoiceLink ].freeze - attr_reader :token, :url + attr_reader :token, :url, :environment - def initialize(token, url: 'https://api.telegram.org') + def initialize(token, url: 'https://api.telegram.org', environment: :production) @token = token @url = url + @environment = environment.downcase.to_sym end def method_missing(method_name, *args, &block) endpoint = method_name.to_s endpoint = camelize(endpoint) if endpoint.include?('_') @@ -50,24 +51,30 @@ ENDPOINTS.include?(method_name) || super end def call(endpoint, raw_params = {}) params = build_params(raw_params) - response = conn.post("/bot#{token}/#{endpoint}", params) - if response.status == 200 - JSON.parse(response.body) - else - raise Exceptions::ResponseError.new(response), - 'Telegram API has returned the error.' + 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 + + JSON.parse(response.body) end private def build_params(params) params.transform_values do |value| sanitize_value(value) end + end + + def build_path(endpoint) + path = "/bot#{token}/" + path += 'test/' if environment == :test + path + endpoint end def sanitize_value(value) jsonify_value(value) end