Skip to content

Instantly share code, notes, and snippets.

@ka8725
Created December 14, 2011 17:30
Show Gist options
  • Save ka8725/1477559 to your computer and use it in GitHub Desktop.
Save ka8725/1477559 to your computer and use it in GitHub Desktop.
# encoding: utf-8
require 'yaml'
module Proxy
OVERRIDEN_METHODS = %w(get post put delete).freeze
APP_CONFIG = YAML.load(File.open File.join(File.dirname(__FILE__), '../config/application.yml'))
def self.included(base)
base.class_eval do
OVERRIDEN_METHODS.each do |method|
alias_method "#{method}_without_api_key", method
alias_method method, "#{method}_with_api_key"
end
end
end
OVERRIDEN_METHODS.each do |m|
define_method("#{m}_with_api_key") do |*args, &block|
args[0] = set_api_key(args[0])
send("#{m}_without_api_key", args, &block)
end
end
private
def set_api_key(path)
"#{path}#{url_separator(path)}api_key=#{APP_CONFIG['api_key']}"
end
def url_separator(path)
path.include?('?') ? '&' : '?'
end
end
Net::HTTP.send :include, Proxy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment