Sha256: 597c71b455c64bef297f10e0f3f70bd682aab06eb2d67c3914cfa84f285303e9

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module Tinybird
  module Requestable
    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      DEFAULT_REQUEST_METHODS = %i[get post delete].freeze

      private

      def requestable(**options)
        method = options.fetch(:method, DEFAULT_REQUEST_METHODS)
        define_http_method(method)
      end

      def define_http_method(method)
        define_method(:http_method) do
          method
        end
      end
    end

    def call(**args)
      uri = URI(endpoint)
      client = Tinybird::HttpClient.new(uri)
      if http_method == :get
        client.send(http_method, headers)
      else
        client.send(http_method, body, headers)
      end
    end

    private

    def base_url
      Tinybird.config.base_url.sub(/\/$/, "")
    end

    def endpoint
      [
        base_url,
        path_segment
      ].join("/")
    end

    def path_segment
      raise NotImplementedError, "#{self.class} must implement #path_segment"
    end

    def body
      raise NotImplementedError, "#{self.class} must implement #body"
    end

    def headers
      raise NotImplementedError, "#{self.class} must implement #headers"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tinybird-0.1.1 lib/tinybird/requestable.rb