-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkontoapi-ruby.rb
More file actions
76 lines (63 loc) · 1.89 KB
/
kontoapi-ruby.rb
File metadata and controls
76 lines (63 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require 'addressable/uri'
require 'json'
require 'net/http'
require 'net/https'
module KontoAPI
extend self
RootCA = '/etc/ssl/certs'
VALIDITY_URL = Addressable::URI.parse 'https://ask.kontoapi.de/for/validity.json'
BANKNAME_URL = Addressable::URI.parse 'https://ask.kontoapi.de/for/bankname.json'
DEFAULT_TIMEOUT = 10
@@api_key = nil
def api_key=(key)
@@api_key = key
end
def api_key
@@api_key
end
@@timeout = nil
def timeout=(new_timeout)
@@timeout = new_timeout
end
def timeout
@@timeout || DEFAULT_TIMEOUT
end
def valid?(options={})
return false unless (!options[:ktn].to_s.strip.empty? && !options[:blz].to_s.strip.empty?) || !options[:iban].to_s.strip.empty? || !options[:bic].to_s.strip.empty?
response = ask_for(:validity, options)
response['answer'].eql?('yes')
end
def bank_name(bank_code)
return nil if bank_code.to_s.strip.empty?
response = ask_for(:bankname, { :blz => bank_code.to_s })
response['answer'].empty? ? nil : response['answer']
end
private
def ask_for(what, options={})
raise 'Please set your API Key first (KontoAPI::api_key = "<your_key>"). You can get one at https://www.kontoapi.de/' unless api_key
url = const_get("#{what}_URL".upcase).dup
options.merge!( :key => api_key )
url.query_values = options
body = get_url(url)
JSON.parse(body)
end
def get_url(url)
http = Net::HTTP.new(url.host, 443)
http.use_ssl = true
http.read_timeout = timeout
if File.directory? RootCA
http.ca_path = RootCA
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.verify_depth = 5
else
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
response = http.get url.request_uri, 'User-agent' => 'Konto API Ruby Client'
case response
when Net::HTTPSuccess, Net::HTTPOK
response.body
else
response.error!
end
end
end