Sha256: a841b695813852c15e991ec2346722c7d6a9e339c4e9f04fff4036f54face572

Contents?: true

Size: 1.72 KB

Versions: 10

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module Truemail
  module Client
    class Http
      require 'uri'
      require 'net/http'
      require 'json'

      URI_ATTRS = %i[secure_connection host port endpoint uri_params].freeze
      USER_AGENT = 'Truemail Ruby client'
      MIME_TYPE = 'application/json'
      VALIDATION_ENDPOINT = '/'
      HEALTHCHECK_ENDPOINT = '/healthcheck'

      def initialize(endpoint = Truemail::Client::Http::VALIDATION_ENDPOINT, **uri_params)
        Truemail::Client::Http::URI_ATTRS[0..2].each do |attribute|
          instance_variable_set(:"@#{attribute}", Truemail::Client.configuration.public_send(attribute))
        end
        @endpoint = endpoint
        @uri_params = uri_params
      end

      def run
        ::Net::HTTP.start(uri.host, uri.port, use_ssl: secure_connection) do |http|
          request = ::Net::HTTP::Get.new(uri)
          request['User-Agent'] = Truemail::Client::Http::USER_AGENT
          request['Accept'] = Truemail::Client::Http::MIME_TYPE
          request['Content-Type'] = Truemail::Client::Http::MIME_TYPE
          unless endpoint.eql?(Truemail::Client::Http::HEALTHCHECK_ENDPOINT)
            request['Authorization'] = Truemail::Client.configuration.token
          end
          http.request(request)
        end.body
      rescue => error
        { truemail_client_error: error }.to_json
      end

      private

      attr_reader(*Truemail::Client::Http::URI_ATTRS)

      def request_uri
        ::URI::HTTP.build(
          path: endpoint,
          query: uri_params.empty? ? nil : ::URI.encode_www_form(uri_params)
        ).request_uri
      end

      def uri
        @uri ||= URI("#{secure_connection ? 'https' : 'http'}://#{host}:#{port}#{request_uri}")
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
truemail-client-0.6.1 lib/truemail/client/http.rb
truemail-client-0.6.0 lib/truemail/client/http.rb
truemail-client-0.5.2 lib/truemail/client/http.rb
truemail-client-0.5.1 lib/truemail/client/http.rb
truemail-client-0.5.0 lib/truemail/client/http.rb
truemail-client-0.4.1 lib/truemail/client/http.rb
truemail-client-0.4.0 lib/truemail/client/http.rb
truemail-client-0.3.4 lib/truemail/client/http.rb
truemail-client-0.3.3 lib/truemail/client/http.rb
truemail-client-0.3.2 lib/truemail/client/http.rb