Sha256: 3fec00c27c4fbb58f650841e291fe2b2a90fb001d13f7c3c6fd7aeec5d010016

Contents?: true

Size: 833 Bytes

Versions: 5

Compression:

Stored size: 833 Bytes

Contents

require 'rest-client'
require 'json'
require 'active_support/inflector'

class TelegramBot
  module Request
    module PrependMethods
      attr_accessor :token

      def initialize(*args, token:, &block)
        @token = token
        super(*args, &block)
      end
    end

    API_BASE = 'https://api.telegram.org/bot'

    def self.included(clazz)
      clazz.prepend PrependMethods
    end

    def request(method, params)
      url    = construct_url(method)
      resp   = RestClient.post(url, params)
      json   = JSON.parse(resp.body)
      result = json['result']
      return result if resp.code.to_s =~ /^2\d\d$/
      raise Exception.new(resp)
    end

    private

    def construct_url(method)
      method_camelized = method.to_s.camelize(:lower)
      "#{API_BASE}#{@token}/#{method_camelized}"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
telegram_bot_ruby-0.1.7 lib/telegram_bot/request.rb
telegram_bot_ruby-0.1.6 lib/telegram_bot/request.rb
telegram_bot_ruby-0.1.5 lib/telegram_bot/request.rb
telegram_bot_ruby-0.1.3 lib/telegram_bot/request.rb
telegram_bot_ruby-0.1.1 lib/telegram_bot/request.rb