Sha256: 5939d1927f568d546ba0b3d367768d71cb19d03e10dccba6e673992d37f9e830
Contents?: true
Size: 1.61 KB
Versions: 1
Compression:
Stored size: 1.61 KB
Contents
# frozen_string_literal: true require 'net/http' class Valvat class Lookup class Base def initialize(vat, options = {}) @vat = Valvat(vat) @options = options @requester = @options[:requester] && Valvat(@options[:requester]) end def perform response = fetch(endpoint_uri) case response when Net::HTTPSuccess parse(response.body) else { error: Valvat::HTTPError.new(response.code, self.class) } end end private def endpoint_uri raise NotImplementedError end def build_request(uri) raise NotImplementedError end def parse(body) raise NotImplementedError end def fetch(uri, limit = 0) response = send_request(uri) if Net::HTTPRedirection == response && limit < 5 fetch(URI.parse(response['Location']), limit + 1) else response end rescue Errno::ECONNRESET, IOError raise if limit > 5 fetch(uri, limit + 1) end def send_request(uri) request = build_request(uri) Net::HTTP.start(uri.host, uri.port, options_for(uri)) do |http| http.request(request) end end def options_for(uri) options = if @options.key?(:savon) puts 'DEPRECATED: The option :savon is deprecated. Use :http instead.' @options[:savon] else @options[:http] end || {} options.merge({ use_ssl: URI::HTTPS === uri }) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
valvat-1.2.1 | lib/valvat/lookup/base.rb |