Last active
August 29, 2015 13:58
-
-
Save elight/9977098 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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