<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -21,6 +21,6 @@ if defined? Rails
   require 'facebooker/rails/facebook_url_helper'
   require 'facebooker/rails/extensions/rack_setup' if Rails.version &gt; '2.3'
   require 'facebooker/rails/extensions/action_controller'
-  require 'facebooker/rails/extensions/action_view'
+  #require 'facebooker/rails/extensions/action_view'
   require 'facebooker/rails/extensions/routing'
 end</diff>
      <filename>init.rb</filename>
    </modified>
    <modified>
      <diff>@@ -27,6 +27,7 @@ require 'digest/md5'
 module Facebooker
 
     @facebooker_configuration = {}
+    @raw_facebooker_configuration = {}
     @current_adapter = nil
     @set_asset_host_to_callback_url = true
     @path_prefix = nil
@@ -37,11 +38,12 @@ module Facebooker
     def load_configuration(facebooker_yaml_file)
       if File.exist?(facebooker_yaml_file)
         if defined? RAILS_ENV
-          config = YAML.load_file(facebooker_yaml_file)[RAILS_ENV] 
+          @raw_facebooker_configuration = YAML.load_file(facebooker_yaml_file)[RAILS_ENV]
         else
-          config = YAML.load_file(facebooker_yaml_file)           
+          @raw_facebooker_configuration = YAML.load_file(facebooker_yaml_file)
         end
-        apply_configuration(config)
+        Thread.current[:fb_api_config] = @raw_facebooker_configuration unless Thread.current[:fb_api_config]
+        apply_configuration(@raw_facebooker_configuration)
       end
     end
 
@@ -67,6 +69,41 @@ module Facebooker
       @facebooker_configuration
     end
 
+    def with_application(api_key, &amp;block)
+      config = fetch_config_for( api_key )
+
+      unless config
+        self.logger.info &quot;Can't find facebooker config: '#{api_key}'&quot; if self.logger
+        yield if block_given?
+        return
+      end
+
+      # Save the old config to handle nested activation
+      old = Thread.current[:fb_api_config].dup rescue false
+
+      if block_given?
+        begin
+          self.logger.info &quot;Swapping facebooker config: '#{api_key}'&quot; if self.logger
+          Thread.current[:fb_api_config] = apply_configuration(config)
+          yield
+        ensure
+          Thread.current[:fb_api_config] = old if old
+          apply_configuration(Thread.current[:fb_api_config])
+        end
+      end
+    end
+
+    def fetch_config_for(api_key)
+      if @raw_facebooker_configuration['api_key'] == api_key
+        return @raw_facebooker_configuration
+      elsif @raw_facebooker_configuration['alternative_keys'] and
+            @raw_facebooker_configuration['alternative_keys'].keys.include?(api_key)
+        return @raw_facebooker_configuration['alternative_keys'][api_key].merge(
+                'api_key' =&gt; api_key )
+      end
+      return false
+    end
+
     # TODO: This should be converted to attr_accessor, but we need to
     # get all the require statements at the top of the file to work.
 </diff>
      <filename>lib/facebooker.rb</filename>
    </modified>
    <modified>
      <diff>@@ -34,14 +34,20 @@ module Rack
         request = Rack::Request.new(env)
         fb_params = extract_fb_sig_params(request.POST)
         unless fb_params.empty?
-          unless signature_is_valid?(fb_params, request.POST['fb_sig'])
-            return Rack::Response.new([&quot;Invalid Facebook signature&quot;], 400).finish
+          Facebooker.with_application(fb_params['api_key']) do
+            unless signature_is_valid?(fb_params, request.POST['fb_sig'])
+              return Rack::Response.new([&quot;Invalid Facebook signature&quot;], 400).finish
+            end
+            env['REQUEST_METHOD'] = fb_params[&quot;request_method&quot;] if fb_params[&quot;request_method&quot;]
+            convert_parameters!(request.POST)
+            @app.call(env)
           end
-          env['REQUEST_METHOD'] = fb_params[&quot;request_method&quot;] if fb_params[&quot;request_method&quot;]
-          convert_parameters!(request.POST)
+        else
+          @app.call(env)
         end
+      else
+        @app.call(env)
       end
-      @app.call(env)
     end
     
     private
@@ -73,4 +79,4 @@ module Rack
       end
     end
   end
-end
\ No newline at end of file
+end</diff>
      <filename>lib/rack/facebook.rb</filename>
    </modified>
    <modified>
      <diff>@@ -93,4 +93,82 @@ class Facebooker::AdaptersTest &lt; Test::Unit::TestCase
   def test_bebo_process_data
 
   end
+
+  def test_fetch_config_for_can_find_top_level_api_key
+    old = Facebooker.instance_variable_get('@raw_facebooker_configuration')
+    # Now that we've backed up the old value...
+    raw_fb_config = { 'api_key' =&gt; 'a key' }
+    Facebooker.instance_variable_set('@raw_facebooker_configuration', raw_fb_config)
+    assert_equal Facebooker.fetch_config_for( 'a key' ), raw_fb_config
+  ensure
+    # Put the old value back
+    Facebooker.instance_variable_set('@raw_facebooker_configuration', old)
+  end
+
+  def test_fetch_config_for_can_find_deep_api_key
+    old = Facebooker.instance_variable_get('@raw_facebooker_configuration')
+    # Now that we've backed up the old value...
+    raw_fb_config = { 'api_key' =&gt; 'a key',
+                      'alternative_keys' =&gt; {
+                        'another key'     =&gt; { 'secret_key' =&gt; 'sdfsd' },
+                        'yet another key' =&gt; { 'secret_key' =&gt; '9ho2h' } } }
+    Facebooker.instance_variable_set('@raw_facebooker_configuration', raw_fb_config)
+    assert_equal raw_fb_config['alternative_keys']['another key'].merge( 'api_key' =&gt; 'another key' ),
+                 Facebooker.fetch_config_for( 'another key' )
+  ensure
+    # Put the old value back
+    Facebooker.instance_variable_set('@raw_facebooker_configuration', old)
+  end
+
+  def test_fetch_config_for_returns_false_if_no_apikey_found
+    old = Facebooker.instance_variable_get('@raw_facebooker_configuration')
+    # Now that we've backed up the old value...
+    raw_fb_config = { 'api_key' =&gt; 'a key' }
+    Facebooker.instance_variable_set('@raw_facebooker_configuration', raw_fb_config)
+    assert ! Facebooker.fetch_config_for( 'another key' )
+  ensure
+    # Put the old value back
+    Facebooker.instance_variable_set('@raw_facebooker_configuration', old)
+  end
+
+  def test_with_application_yields_if_no_config_is_found
+    flexmock( Facebooker ).
+      should_receive( :fetch_config_for ).
+      and_return( false )
+    # Is there a better way to assert the block is yielded?
+    @changes_to_true = false
+    Facebooker.with_application('somekey not found') do
+      @changes_to_true = true
+    end
+    assert @changes_to_true
+  end
+
+  def test_with_application_changes_config_inside_block
+    flexmock( Facebooker ).
+      should_receive( :fetch_config_for ).
+      and_return({ 'api_key'    =&gt; 'a key',
+                   'secret_key' =&gt; 'my secret key' })
+    Facebooker.with_application('a key') do
+      @secret_in_block = Facebooker.secret_key
+    end
+    # Check outside the block, assures the assertion gets run
+    assert_equal 'my secret key', @secret_in_block
+  end
+
+  def test_with_application_restores_last_config_outside_block
+    flexmock( Facebooker ).
+      should_receive( :fetch_config_for ).
+      and_return( { 'api_key'    =&gt; 'a key',
+                    'secret_key' =&gt; 'my secret key' },
+                  { 'api_key'    =&gt; 'another key',
+                    'secret_key' =&gt; 'my other secret key' } )
+    Facebooker.with_application('a key') do
+      Facebooker.with_application('another key') do
+      end
+      @secret_in_outer_block = Facebooker.secret_key
+    end
+    # Check outside the block, assures the assertion gets run
+    assert_equal 'my secret key', @secret_in_outer_block
+  end
+
 end</diff>
      <filename>test/facebooker/adapters_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -29,12 +29,14 @@ class Facebooker::SessionTest &lt; Test::Unit::TestCase
 
   def test_if_keys_are_not_available_via_environment_then_they_are_gotten_from_a_file
     ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'] = nil
+    Facebooker.instance_variable_set('@facebooker_configuration', nil)
     flexmock(File).should_receive(:read).with(File.expand_path(&quot;~/.facebookerrc&quot;)).once.and_return('{:api =&gt; &quot;foo&quot;}')
     assert_equal('foo', Facebooker::Session.api_key)
   end
 
   def test_if_environment_and_file_fail_to_match_then_an_exception_is_raised
     ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'] = nil
+    Facebooker.instance_variable_set('@facebooker_configuration', nil)
     flexmock(File).should_receive(:read).with(File.expand_path(&quot;~/.facebookerrc&quot;)).once.and_return {raise Errno::ENOENT, &quot;No such file&quot;}
     assert_raises(Facebooker::Session::ConfigurationMissing) {
       Facebooker::Session.api_key</diff>
      <filename>test/facebooker/session_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -6,12 +6,12 @@ require 'rack/mock'
 class Rack::FacebookTest &lt; Test::Unit::TestCase
   
   def setup
-    @secret_key = 'secret'
+    flexmock(Facebooker).should_receive(:secret_key).and_return('secret')
     @app = lambda do |env|
       @env = env
       Rack::Response.new().to_a
     end
-    @facebook = Rack::Facebook.new(@app, @secret_key)
+    @facebook = Rack::Facebook.new(@app)
   end
   
   def params(p)
@@ -47,13 +47,13 @@ class Rack::FacebookTest &lt; Test::Unit::TestCase
   end
   
   def test_skips_with_false_condition
-    @facebook = Rack::Facebook.new(@app, @secret_key) { false }
+    @facebook = Rack::Facebook.new(@app) { false }
     response = app.post(&quot;/&quot;, :input =&gt; params(:fb_sig_user =&gt; 'ignored'))
     assert_equal 200, response.status
   end
   
   def test_verifies_with_true_condition
-    @facebook = Rack::Facebook.new(@app, @secret_key) { true }
+    @facebook = Rack::Facebook.new(@app) { true }
     response = app.post(&quot;/&quot;, :input =&gt; params(:fb_sig_user =&gt; 'ignored'))
     assert_equal 400, response.status
   end</diff>
      <filename>test/rack/facebook_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>39841db21aae7bbe71f10b912a26789af4f54f45</id>
    </parent>
  </parents>
  <author>
    <name>Matthew Beale</name>
    <email>mixonic@synitech.com</email>
  </author>
  <url>http://github.com/mmangino/facebooker/commit/b61851e0b09c9e35c661586a16359dd3e6259ffa</url>
  <id>b61851e0b09c9e35c661586a16359dd3e6259ffa</id>
  <committed-date>2009-06-17T12:46:27-07:00</committed-date>
  <authored-date>2009-06-16T14:04:13-07:00</authored-date>
  <message>A working and tested version of facebooker handling multiple apps at once.</message>
  <tree>9018df51bd07d26b27eab1cadc1e75bb71ad6296</tree>
  <committer>
    <name>Mike Mangino</name>
    <email>mmangino@elevatedrails.com</email>
  </committer>
</commit>
