3

我对 Rails 很陌生,所以如果我的问题没有最有意义,我深表歉意。

我有一个名为的类PaymentGatewayCipher,如下所示:

require 'openssl'

# Encapsulates payment gateway encryption / decryption utility functions
class PaymentGatewayCipher
  class << self
    def encrypt(file, options = {})
      cipher = create_cipher
      cipher.encrypt(cipher_key)
      data = cipher.update(File.read(file))
      data << cipher.final

      if to_file = options[:to]
        # Write it out to a different file
        File.open(to_file, 'wb') do |f|
          f << data
        end
      end

      data
    end

    # Decrypts the given file
    def decrypt(file)
      cipher = create_cipher
      cipher.decrypt(cipher_key)
      encrypted_data = File.open(file, 'rb') {|io| io.read}
      data = cipher.update(encrypted_data)
      data << cipher.final
    end

    # Generates the cipher to be used for encryption/decryption
    def create_cipher
      OpenSSL::Cipher::Cipher.new('aes-256-cbc')
    end

    # Loads the cipher key used for the symmetric algorithm
    def cipher_key
      File.open(File.join(Rails.root, 'config/mystuff/live/cipher.key'), 'rb') {|io| io.read}
    end
  end
end

我想写一个rake task来运行它来解密一个文件。我试过把一个文件放进去tasks/Rakefile,看起来像:

directory "tasks"

task :decrypt_test do
  puts "Decypting"
  pay_pal_config = PaymentGatewayCipher.decrypt('hpa1')
end

但是,当我运行它时,它说找不到Class::Rails

帮助?

4

2 回答 2

7

使用lib/tasks文件夹,不要忘记在您的任务中包含 rails 环境:

directory "tasks"

task :decrypt_test => :environment do
  puts "Decypting"
  pay_pal_config = PaymentGatewayCipher.decrypt('hpa1')
end
于 2012-12-11T16:04:07.343 回答
0

您不必为此编辑Rakefile。例如,将您自己的任务添加到以.rakelib/tasks结尾的文件中,它们将自动可供 Rake 使用。lib/tasks/bootstrap.rake

于 2012-12-11T16:01:23.867 回答