Last active
August 29, 2015 13:58
-
-
Save mru2/9975136 to your computer and use it in GitHub Desktop.
Webservice proxy
This file contains 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
# ============= | |
# Without proxy | |
# ============= | |
require 'soap_client' | |
class Webservice | |
def initialize() | |
@client = SoapClient.new('http://my.api.com/endpoint') | |
end | |
def call_remote(method_name, params = {}) | |
response = @client.call(method_name, params) | |
return parse_response(response) | |
end | |
def parse_response | |
# ... | |
end | |
end | |
service = Webservice.new | |
service.call_remote('get_user', {:id => 'foo'}) | |
# => {:id => 'foo', :name => 'Frank Underwood'} | |
# =========== | |
# With proxy | |
# =========== | |
require 'soap_client' | |
class Webservice | |
class Proxy | |
def initialize(webservice) | |
@webservice = webservice | |
end | |
def method_missing(method_name, *args) | |
@webservice.remote_call(method_name, args) | |
end | |
end | |
attr_reader :proxy | |
def initialize() | |
@proxy = Proxy.new(self) | |
@client = SoapClient.new('http://my.api.com/endpoint') | |
end | |
def call_remote(method_name, params = {}) | |
response = @client.call(method_name, params) | |
if response.status == 404 | |
raise NoMethodError, "undefined endpoint `#{method_name}' for client #{@client}" | |
end | |
return parse_response(response) | |
end | |
def parse_response | |
# ... | |
end | |
end | |
service = Webservice.new | |
service.proxy.get_user(:id => 'foo') | |
# => {:id => 'foo', :name => 'Frank Underwood'} | |
service.proxy.foobar('baz') | |
# => NoMethodError: undefined method `foobar' for #<SoapClient:http://my.api.com/endpoint> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment