class Valvat::Lookup::Base

Public Class Methods

new(vat, options = {}) click to toggle source
# File lib/valvat/lookup/base.rb, line 9
def initialize(vat, options = {})
  @vat = Valvat(vat)
  @options = Valvat::Options(options)
  @requester = @options[:requester] && Valvat(@options[:requester])
end

Public Instance Methods

perform() click to toggle source
# File lib/valvat/lookup/base.rb, line 15
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 Instance Methods

build_request(uri) click to toggle source
# File lib/valvat/lookup/base.rb, line 32
def build_request(uri)
  raise NotImplementedError
end
endpoint_uri() click to toggle source
# File lib/valvat/lookup/base.rb, line 28
def endpoint_uri
  raise NotImplementedError
end
fetch(uri, limit = 0) click to toggle source
# File lib/valvat/lookup/base.rb, line 40
def fetch(uri, limit = 0)
  response = send_request(uri)

  if response == Net::HTTPRedirection && 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
options_for(uri) click to toggle source
# File lib/valvat/lookup/base.rb, line 62
def options_for(uri)
  @options[:http].merge({ use_ssl: URI::HTTPS === uri })
end
parse(body) click to toggle source
# File lib/valvat/lookup/base.rb, line 36
def parse(body)
  raise NotImplementedError
end
send_request(uri) click to toggle source
# File lib/valvat/lookup/base.rb, line 54
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