Sha256: 989f3efeacbfba4fbc0776adcaf0bb5225bdf085f03f4bf86e6156c9997b9fed

Contents?: true

Size: 1.25 KB

Versions: 21

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module Multiwoven
  module Integrations::Core
    class HttpClient
      class << self
        def request(url, method, payload: nil, headers: {})
          uri = URI(url)
          http = Net::HTTP.new(uri.host, uri.port)
          http.use_ssl = (uri.scheme == "https")
          request = build_request(method, uri, payload, headers)
          http.request(request)
        end

        private

        def build_request(method, uri, payload, headers)
          request_class = case method.upcase
                          when Constants::HTTP_GET then Net::HTTP::Get
                          when Constants::HTTP_POST then Net::HTTP::Post
                          when Constants::HTTP_PUT then Net::HTTP::Put
                          when Constants::HTTP_PATCH then Net::HTTP::Patch
                          when Constants::HTTP_DELETE then Net::HTTP::Delete
                          else raise ArgumentError, "Unsupported HTTP method: #{method}"
                          end

          request = request_class.new(uri)
          headers.each { |key, value| request[key] = value }
          request.body = payload.to_json if payload && %w[POST PUT PATCH].include?(method.upcase)
          request
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
multiwoven-integrations-0.13.1 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.13.0 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.12.0 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.11.6 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.11.5 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.11.4 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.11.3 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.11.2 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.11.0 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.10.1 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.10.0 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.9.2 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.9.1 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.9.0 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.8.6 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.8.5 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.8.4 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.8.3 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.8.2 lib/multiwoven/integrations/core/http_client.rb
multiwoven-integrations-0.8.1 lib/multiwoven/integrations/core/http_client.rb