Sha256: fa262f052749627224391e1b993a120a06996eea498de08f193761ecb38c82de

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

require 'net/http'
require 'json'

module Ibanomat
  class ResourceNotFoundError < RuntimeError; end

  URL = 'https://www.sparkasse.de/bin/servlets/sparkasse/iban'

  def self.find(options)
    raise ArgumentError.new unless options.is_a?(Hash)
    raise ArgumentError.new('Option :bank_code is missing!') if options[:bank_code].empty?
    raise ArgumentError.new('Option :bank_account_number is missing!') if options[:bank_account_number].empty?

    uri = URI(URL)
    params = { 'b' => options[:bank_code],
               'a' => options[:bank_account_number]
             }
    uri.query = URI.encode_www_form(params)

    case response = Net::HTTP.get_response(uri)
      when Net::HTTPSuccess
        hash = JSON.parse(response.body)

        { :bank_name           => hash['Institutsname'],
          :bic                 => hash['BIC'],
          :iban                => hash['IBAN'],
          :bank_code           => hash['BLZ'],
          :bank_account_number => hash['KtoNr'],
          :return_code         => hash['RetCode']
        }
      when Net::HTTPNotFound
        raise ResourceNotFoundError
      else
        raise RuntimeError
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ibanomat-1.3.2 lib/ibanomat/client.rb
ibanomat-1.3.1 lib/ibanomat/client.rb
ibanomat-1.3.0 lib/ibanomat/client.rb