Skip to content

Instantly share code, notes, and snippets.

@elight
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elight/9977098 to your computer and use it in GitHub Desktop.
Save elight/9977098 to your computer and use it in GitHub Desktop.
module RequestRouter
ADMIN_REQUESTS = %w[
names
of_admin_methods
go_here
this_is_an_admin_method
]
def request(params)
if ADMIN_REQUESTS.include? calling_method
# create the admin service connection if necessary
@admin_service ||= admin_connection(:keystone, @options[:openstack_region].to_sym)
base_request(@admin_service, params)
else
base_request(@service, params)
end
end
private
# Here there be dragons...
def calling_method
caller(2, 1).first.split("`")[1].sub(/'/, "")
end
end
class PretendThisIsAService
include RequestRouter
def this_is_an_admin_method
request({})
end
def this_is_not_an_admin_method
request({})
end
# Methods below are stubs to support this example
def initialize
@options = { :openstack_region => :ord }
@service = :boring_normal_method
end
def admin_connection(*args)
:admin
end
def base_request(service_type, _)
service_type
end
end
## DEMONSTRATION
s = PretendThisIsAService.new
r1 = s.this_is_an_admin_method
r2 = s.this_is_not_an_admin_method
puts "admin method: #{r1}" # outputs "admin" because its the result of an admin method
puts "not admin method: #{r2}" # outputs "boring_normal_method" because not an admin method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment