3

I'm pretty new to Rails, so I apologize if my question doesn't make the most sense.

I have a class called PaymentGatewayCipher that looks like:

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

I want to write a rake task to run it to decrypt a file. I've tried putting a file in tasks/Rakefile that looks like:

directory "tasks"

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

When I run it, however, it says cannot find Class::Rails

Help?

weberc2
  • 7,423
  • 4
  • 41
  • 57
Shamoon
  • 41,293
  • 91
  • 306
  • 570

2 Answers2

7

Use lib/tasks folder and dont forget to include rails environment on your tasks:

directory "tasks"

task :decrypt_test => :environment do
  puts "Decypting"
  pay_pal_config = PaymentGatewayCipher.decrypt('hpa1')
end
daniloisr
  • 1,367
  • 11
  • 20
0

You don't have to edit the Rakefile for that. Add your own tasks in files placed in lib/tasks ending in .rake, for example lib/tasks/bootstrap.rake, and they will automatically be available to Rake.

Syed Aslam
  • 8,707
  • 5
  • 40
  • 54