This is an old revision of the document!


type:
integration
system:
Ruby on Rails
name:
Datafeed Decryption Example
description:
A basic example of how to decrypt a foxycart v060 datafeed
tags:
datafeed, encryption
date:
2010-06-25
version:
1.0
developer:
http://obledesign.com

Datafeed Decryption Example

Description

A simple example of how to implement a datafeed listener in Ruby on Rails. It especially focuses on decrypting the RC4 encryption.

Installation

  1. Create a new Datafeeds controller with the code below or paste the method into your own controller.
  2. Edit the API_KEY with the API key from your own store.

Requirements

  • You must have ruby-rc4 installed.
  • You must have Nokogiri installed.
  • This example is for a v060 datafeed. It may have to be adapted for any other version.

Gotchas

  • Be sure to use CGI::unescape instead of URI::unescape or URI::decode. The URI methods will screw up the decoding and you'll get weird artifacts in your XML.
  • If you get an InvalidAuthenticityToken error in Rails you'll have to disable protect_from_forgery for the action being called.

Code

class DatafeedsController < ApplicationController
 
  API_KEY = 'YOUR_API_KEY_GOES_HERE'
 
  # Parses Foxycart datafeed v060
  # see http://wiki.foxycart.com/docs/datafeed for details
  def create
 
    require 'ruby/rc4'
    require 'nokogiri'
 
    decryptor = RubyRc4.new(API_KEY) 
    xml = decryptor.encrypt(CGI::unescape(params['FoxyData']))
 
    feed = Nokogiri::XML(xml)
    feed.xpath('.//transaction').each do |transaction|
 
      # PROCESS THE FEED AS YOU LIKE HERE
 
    end
 
    render :text => 'foxy'
 
    rescue Exception=>e
      render :text => e.to_s
 
  end
 
end

Site Tools