Sha256: 80438f0a1c1cff57dad38d8755107a571082d3759a3b3b607475199238c93566

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require "faraday"
require "faraday/net_http"

module TeBot
  class Wire
    class << self
      def sender(message_format, handler = nil, &block)
        @senders ||= {}
        @senders[message_format] = (block || handler)
      end

      def senders
        @senders || {}
      end
    end

    CONN = Faraday.new(
      url: "https://api.telegram.org/",
      headers: {"Content-Type" => "application/json"}
    )

    def initialize(access_token)
      @access_token = access_token
    end

    def make_request(path, params: nil, headers: nil, body: nil)
      CONN.post(url(path)) do |req|
        req.params.merge!(params) if params
        req.headers.merge!(headers) if headers
        req.body = body if body
      end
    end

    def url(path)
      "/bot#{@access_token}/#{path}"
    end

    def send_message(chat_id, **payload)
      message_format, message = payload.first
      handler = self.class.senders[message_format]

      raise ArgumentError, "Message type invalid. sender :#{message_format} not defined" if handler.nil?

      return instance_exec(chat_id, message, &handler) if handler.respond_to?(:call)

      public_send(handler, chat_id, message)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
te_bot-0.4.0 lib/te_bot/wire.rb