Skip to content

Instantly share code, notes, and snippets.

@aakashd
Created July 22, 2011 18:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aakashd/1100092 to your computer and use it in GitHub Desktop.
Save aakashd/1100092 to your computer and use it in GitHub Desktop.
Custom auth header for ActiveResource requests
class AppAuthHeaders
cattr_accessor :remember_token
def self.headers
remember_token.blank? ? {"Authorization" => ""} : {"Authorization" => "RememberToken #{self.remember_token}"}
end
end
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(:default, Rails.env) if defined?(Bundler)
module MyApp
class Application < Rails::Application
# some configuration
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(#{config.root}/app/extensions)
# some other configuration
end
end
module CustomAuthHeader
def self.included(klass)
class << klass
def headers_with_authorization
default_headers = headers_without_authorization
AppAuthHeaders.headers.keys.each {|key| default_headers.delete(key) }
default_headers.reverse_merge!(EngineAuthHeaders.headers)
end
alias_method_chain :headers, :authorization
end
end
end
ActiveResource::Base.send(:include, CustomAuthHeader)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment