Skip to content

Commit fc71f30

Browse files
authored
Merge pull request #4 from pennylane-hq/fix-response-parser-for-h003
Refactor XPath queries to use client URN schema based on client version
2 parents 847a298 + 34fa1b8 commit fc71f30

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

lib/epics/response.rb

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,97 @@
11
class Epics::Response
2-
attr_accessor :doc
3-
attr_accessor :client
2+
attr_accessor :doc, :client
43

54
def initialize(client, xml)
65
self.doc = Nokogiri::XML.parse(xml)
76
self.client = client
87
end
98

109
def technical_error?
11-
!["011000", "000000"].include?(technical_code)
10+
!%w[011000 000000].include?(technical_code)
1211
end
1312

1413
def technical_code
1514
mutable_return_code.empty? ? system_return_code : mutable_return_code
1615
end
1716

1817
def mutable_return_code
19-
doc.xpath("//xmlns:header/xmlns:mutable/xmlns:ReturnCode", xmlns: "urn:org:ebics:H004").text
18+
doc.xpath('//xmlns:header/xmlns:mutable/xmlns:ReturnCode', xmlns: client.urn_schema).text
2019
end
2120

2221
def system_return_code
23-
doc.xpath("//xmlns:SystemReturnCode/xmlns:ReturnCode", xmlns: 'http://www.ebics.org/H000').text
22+
doc.xpath('//xmlns:SystemReturnCode/xmlns:ReturnCode', xmlns: client.urn_schema).text
2423
end
2524

2625
def business_error?
27-
!["", "000000"].include?(business_code)
26+
!['', '000000'].include?(business_code)
2827
end
2928

3029
def business_code
31-
doc.xpath("//xmlns:body/xmlns:ReturnCode", xmlns: "urn:org:ebics:H004").text
30+
doc.xpath('//xmlns:body/xmlns:ReturnCode', xmlns: client.urn_schema).text
3231
end
3332

3433
def ok?
3534
!technical_error? & !business_error?
3635
end
3736

3837
def last_segment?
39-
!!doc.at_xpath("//xmlns:header/xmlns:mutable/*[@lastSegment='true']", xmlns: "urn:org:ebics:H004")
38+
!!doc.at_xpath("//xmlns:header/xmlns:mutable/*[@lastSegment='true']", xmlns: client.urn_schema)
4039
end
4140

4241
def segmented?
43-
!!doc.at_xpath("//xmlns:header/xmlns:mutable/xmlns:SegmentNumber", xmlns: "urn:org:ebics:H004")
42+
!!doc.at_xpath('//xmlns:header/xmlns:mutable/xmlns:SegmentNumber', xmlns: client.urn_schema)
4443
end
4544

4645
def return_code
47-
doc.xpath("//xmlns:ReturnCode", xmlns: "urn:org:ebics:H004").last.content
46+
doc.xpath('//xmlns:ReturnCode', xmlns: client.urn_schema).last.content
4847
rescue NoMethodError
4948
nil
5049
end
5150

5251
def report_text
53-
doc.xpath("//xmlns:ReportText", xmlns: "urn:org:ebics:H004").first.content
52+
doc.xpath('//xmlns:ReportText', xmlns: client.urn_schema).first.content
5453
end
5554

5655
def transaction_id
57-
doc.xpath("//xmlns:header/xmlns:static/xmlns:TransactionID", xmlns: 'urn:org:ebics:H004').text
56+
doc.xpath('//xmlns:header/xmlns:static/xmlns:TransactionID', xmlns: client.urn_schema).text
5857
end
5958

6059
def order_id
61-
doc.xpath("//xmlns:header/xmlns:mutable/xmlns:OrderID", xmlns: "urn:org:ebics:H004").text
60+
doc.xpath('//xmlns:header/xmlns:mutable/xmlns:OrderID', xmlns: client.urn_schema).text
6261
end
6362

6463
def digest_valid?
6564
authenticated = doc.xpath("//*[@authenticate='true']").map(&:canonicalize).join
66-
digest_value = doc.xpath("//ds:DigestValue", ds: "http://www.w3.org/2000/09/xmldsig#").first
65+
digest_value = doc.xpath('//ds:DigestValue', ds: 'http://www.w3.org/2000/09/xmldsig#').first
6766

6867
digest = Base64.encode64(digester.digest(authenticated)).strip
6968

7069
digest == digest_value.content
7170
end
7271

7372
def signature_valid?
74-
signature = doc.xpath("//ds:SignedInfo", ds: "http://www.w3.org/2000/09/xmldsig#").first.canonicalize
75-
signature_value = doc.xpath("//ds:SignatureValue", ds: "http://www.w3.org/2000/09/xmldsig#").first
73+
signature = doc.xpath('//ds:SignedInfo', ds: 'http://www.w3.org/2000/09/xmldsig#').first.canonicalize
74+
signature_value = doc.xpath('//ds:SignatureValue', ds: 'http://www.w3.org/2000/09/xmldsig#').first
7675

7776
client.bank_x.key.verify(digester, Base64.decode64(signature_value.content), signature)
7877
end
7978

8079
def public_digest_valid?
81-
encryption_pub_key_digest = doc.xpath("//xmlns:EncryptionPubKeyDigest", xmlns: 'urn:org:ebics:H004').first
80+
encryption_pub_key_digest = doc.xpath('//xmlns:EncryptionPubKeyDigest', xmlns: client.urn_schema).first
8281

8382
client.e.public_digest == encryption_pub_key_digest.content
8483
end
8584

8685
def order_data
87-
order_data_encrypted = Base64.decode64(doc.xpath("//xmlns:OrderData", xmlns: 'urn:org:ebics:H004').first.content)
86+
order_data_encrypted = Base64.decode64(doc.xpath('//xmlns:OrderData', xmlns: client.urn_schema).first.content)
8887

8988
data = (cipher.update(order_data_encrypted) + cipher.final)
9089

9190
Zlib::Inflate.new.inflate(data)
9291
end
9392

9493
def cipher
95-
cipher = OpenSSL::Cipher.new("aes-128-cbc")
94+
cipher = OpenSSL::Cipher.new('aes-128-cbc')
9695

9796
cipher.decrypt
9897
cipher.padding = 0
@@ -101,13 +100,13 @@ def cipher
101100
end
102101

103102
def transaction_key
104-
transaction_key_encrypted = Base64.decode64(doc.xpath("//xmlns:TransactionKey", xmlns: 'urn:org:ebics:H004').first.content)
103+
transaction_key_encrypted = Base64.decode64(doc.xpath('//xmlns:TransactionKey',
104+
xmlns: client.urn_schema).first.content)
105105

106106
@transaction_key ||= client.e.key.private_decrypt(transaction_key_encrypted)
107107
end
108108

109109
def digester
110-
@digester ||= OpenSSL::Digest::SHA256.new
110+
@digester ||= OpenSSL::Digest.new('SHA256')
111111
end
112-
113112
end

0 commit comments

Comments
 (0)