lib/truemail/client/http.rb in truemail-client-0.1.1 vs lib/truemail/client/http.rb in truemail-client-0.2.0

- old
+ new

@@ -3,20 +3,24 @@ module Truemail module Client class Http require 'uri' require 'net/http' + require 'json' - URI_ATTRS = %i[secure_connection host port email].freeze + 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(email) - Truemail::Client::Http::URI_ATTRS[0..-2].each do |attribute| + 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 - @email = email + @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) @@ -32,11 +36,18 @@ 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.gsub(/%40/, '@') # TODO: should be removed after new Truemail server release with encoding uri param values + end + def uri - @uri ||= URI("#{secure_connection ? 'https' : 'http'}://#{host}:#{port}/?email=#{email}") + @uri ||= URI("#{secure_connection ? 'https' : 'http'}://#{host}:#{port}#{request_uri}") end end end end