Commit b11efc2

mo <mo.khan@gmail.com>
2017-12-30 19:17:34
deprecate decrypt in favour of decrypt_xml or decrypt_hash.
1 parent f0dc827
lib/xml/kit/decryption.rb
@@ -13,6 +13,7 @@ module Xml
       #
       # @param data [Hash] the XML document converted to a [Hash] using Hash.from_xml.
       def decrypt(data)
+        ::Xml::Kit.deprecate("decrypt is deprecated. Use decrypt_xml or decrypt_hash instead.")
         decrypt_hash(data)
       end
 
@@ -20,7 +21,7 @@ module Xml
       #
       # @param raw_xml [String] the XML document as a string.
       def decrypt_xml(raw_xml)
-        decrypt(Hash.from_xml(raw_xml))
+        decrypt_hash(Hash.from_xml(raw_xml))
       end
 
       # Decrypts an EncryptedData section of an XML document.
lib/xml/kit/signature.rb
@@ -27,6 +27,10 @@ module Xml
         @reference_id = reference_id
         @signature_method = SIGNATURE_METHODS[signature_method]
       end
+
+      def to_xml(xml: ::Builder::XmlMarkup.new)
+        ::Xml::Kit::Template.new(self).to_xml(xml: xml)
+      end
     end
   end
 end
lib/xml/kit/templatable.rb
@@ -39,7 +39,7 @@ module Xml
 
       def signature_for(reference_id:, xml:)
         return unless sign?
-        render(signatures.build(reference_id), xml: xml)
+        signatures.build(reference_id).to_xml(xml: xml)
       end
 
       # Allows you to specify which key pair to use for generating an XML digital signature.
lib/xml/kit.rb
@@ -1,5 +1,6 @@
 require "active_model"
 require "active_support/core_ext/numeric/time"
+require "active_support/deprecation"
 require "base64"
 require "builder"
 require "logger"
@@ -35,6 +36,11 @@ module Xml
       def logger=(logger)
         @logger = logger
       end
+
+      def deprecate(message)
+        @deprecation ||= ActiveSupport::Deprecation.new('1.0.0', 'xml-kit')
+        @deprecation.deprecation_warning(message)
+      end
     end
   end
 end
spec/xml/decryption_spec.rb
@@ -1,5 +1,5 @@
 RSpec.describe Xml::Kit::Decryption do
-  describe "#decrypt" do
+  describe "#decrypt_hash" do
     let(:secret) { FFaker::Movie.title }
     let(:password) { FFaker::Movie.title }
 
@@ -40,11 +40,11 @@ RSpec.describe Xml::Kit::Decryption do
         }
       }
       subject = described_class.new(private_keys: [private_key])
-      decrypted = subject.decrypt(data)
+      decrypted = subject.decrypt_hash(data)
       expect(decrypted.strip).to eql(secret)
     end
 
-    it 'attemps to decrypt with each encryption keypair' do
+    it 'attempts to decrypt with each encryption keypair' do
       certificate_pem, private_key_pem = generate_key_pair(password)
       public_key = OpenSSL::X509::Certificate.new(certificate_pem).public_key
       private_key = OpenSSL::PKey::RSA.new(private_key_pem, password)
@@ -84,7 +84,7 @@ RSpec.describe Xml::Kit::Decryption do
       other_private_key = OpenSSL::PKey::RSA.new(other_private_key_pem, password)
 
       subject = described_class.new(private_keys: [other_private_key, private_key])
-      decrypted = subject.decrypt(data)
+      decrypted = subject.decrypt_hash(data)
       expect(decrypted.strip).to eql(secret)
     end
 
@@ -127,7 +127,7 @@ RSpec.describe Xml::Kit::Decryption do
       new_private_key = OpenSSL::PKey::RSA.new(new_private_key_pem, password)
       subject = described_class.new(private_keys: [new_private_key])
       expect do
-        subject.decrypt(data)
+        subject.decrypt_hash(data)
       end.to raise_error(OpenSSL::PKey::RSAError)
     end
   end