# frozen_string_literal: true module Truemail module Client class Http require 'uri' require 'net/http' URI_ATTRS = %i[secure_connection host port email].freeze USER_AGENT = 'Truemail Ruby client' MIME_TYPE = 'application/json' def initialize(email) Truemail::Client::Http::URI_ATTRS[0..-2].each do |attribute| instance_variable_set(:"@#{attribute}", Truemail::Client.configuration.public_send(attribute)) end @email = email 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 request['Authorization'] = Truemail::Client.configuration.token http.request(request) end.body rescue => error { truemail_client_error: error }.to_json end private attr_reader(*Truemail::Client::Http::URI_ATTRS) def uri @uri ||= URI("#{secure_connection ? 'https' : 'http'}://#{host}:#{port}/?email=#{email}") end end end end