Skip to content

Commit

Permalink
Added some output to generated proxies (to show service methods), re-…
Browse files Browse the repository at this point in the history
…enabled debugging flag with specs, and small parser change for SOAPAction
  • Loading branch information
jeremydurham committed Oct 26, 2009
1 parent 78b94a4 commit 573a632
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
3 changes: 2 additions & 1 deletion bin/wsdl2proxy
Expand Up @@ -31,11 +31,12 @@ proxy.service_methods.each do |service_method|
def parse_#{underscore(service_method)}(response)
Hpricot.XML(response.body)
end
end
EOS
end
template = File.read(template_filename)
template.gsub!(/%name%/, output_klass_name)
template.gsub!(/%body%/, output)
template.gsub!(/%uri%/, ARGV[0])
File.open(output_filename, 'w') { |f| f.puts template }
puts "Generated proxy #{output_filename} - You can use the proxy by executing #{output_klass_name}.new('#{ARGV[0]}')"
21 changes: 13 additions & 8 deletions lib/service_proxy/base.rb
Expand Up @@ -23,15 +23,20 @@ def initialize(endpoint)
self.endpoint = endpoint
self.setup
end

def call_service(options)
method = options[:method]
headers = { 'content-type' => 'text/xml; charset=utf-8', 'SOAPAction' => self.soap_actions[method] || '' }
headers = { 'content-type' => 'text/xml; charset=utf-8', 'SOAPAction' => self.soap_actions[method] }
body = build_request(method, options)
response = self.http.request_post(self.uri.path, body, headers)
parse_response(method, response)
end
end

def debug=(value)
@debug = value
self.http.set_debug_output(STDOUT) if value
end

protected

def setup
Expand All @@ -41,10 +46,12 @@ def setup
get_wsdl
parse_wsdl
end


private

def setup_http
raise ArgumentError, "Endpoint URI must be valid" unless self.uri.scheme
self.http = Net::HTTP.new(self.uri.host, self.uri.port)
raise ArgumentError, "Endpoint URI must be valid" unless self.uri.scheme
self.http ||= Net::HTTP.new(self.uri.host, self.uri.port)
setup_https if self.uri.scheme == 'https'
self.http.set_debug_output(STDOUT) if self.debug
self.http.read_timeout = 5
Expand All @@ -55,8 +62,6 @@ def setup_https
self.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

private

def setup_uri
self.uri = URI.parse(self.endpoint)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/service_proxy/parser.rb
Expand Up @@ -4,7 +4,7 @@ class Parser < Nokogiri::XML::SAX::Document

def initialize(*args)
self.service_methods = []
self.soap_actions = {}
self.soap_actions = Hash.new('')
super
end

Expand Down
5 changes: 4 additions & 1 deletion lib/templates/proxy.rbt
Expand Up @@ -4,4 +4,7 @@ require 'hpricot'

class %name% < ServiceProxy::Base
%body%
end
end

proxy = %name%.new('%uri%')
p proxy.service_methods
12 changes: 11 additions & 1 deletion spec/service_proxy_spec.rb
Expand Up @@ -58,7 +58,7 @@
@proxy = ZipcodeService.new('http://ws.fraudlabs.com/zipcodeworldUS_webservice.asmx?wsdl')
end

it "should not fail" do
it "should be successful" do
@proxy.ZIPCodeWorld_US.should_not be_nil
end
end
Expand All @@ -77,4 +77,14 @@
end
end

describe "debugging a service call" do
before do
@proxy = ZipcodeService.new('http://ws.fraudlabs.com/zipcodeworldUS_webservice.asmx?wsdl')
end

it "should set_debug_output on the HTTP connection" do
@proxy.http.should_receive(:set_debug_output)
@proxy.debug = true
end
end
end

0 comments on commit 573a632

Please sign in to comment.