-
Notifications
You must be signed in to change notification settings - Fork 15
Add some features #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 9 commits
5823cce
493b20a
76de493
f56bb63
dfa0821
8a2046a
3c3dfba
28244bb
22137dc
f9e0a1b
56daceb
7618cdb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,5 @@ | |
| .idea/ | ||
| .rvmrc | ||
| *.swp*.gem | ||
| Gemfile.lock | ||
| Gemfile.lock | ||
| .ruby-version | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| before_install: | ||
| - gem install bundler | ||
| rvm: | ||
| - 1.9.3 | ||
| - 2.0.0 | ||
| - 2.4.0 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,5 @@ source 'https://rubygems.org' | |
|
|
||
| # Specify your gem's dependencies in sinatra-soap.gemspec | ||
| gemspec | ||
|
|
||
| gem 'sinatra', '2.0.0.rc2' | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this should be moved into gemspec (well it's there). |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,15 +9,84 @@ def soap_views() | |
| File.join(File.dirname(__FILE__), "..", "views") | ||
| end | ||
|
|
||
| def hash_to_xml(xml, hash) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this should be rewritten. Too long method with huge complexity.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. But what you think about this idea in general? ( |
||
| hash.each do |key, value| | ||
| if value.is_a?(Hash) | ||
| attrs = {} | ||
| content = {} | ||
| content_str = nil | ||
| value.each do |key, value| | ||
| if key.to_s == "@@content" | ||
| content_str = value | ||
| elsif key.to_s.start_with?("@") | ||
| attrs[key.to_s[1..-1]] = value | ||
| else | ||
| content[key] = value | ||
| end | ||
| end | ||
| if content_str | ||
| xml.tag!(key, attrs, content_str) | ||
| else | ||
| xml.tag!(key, attrs) do | ||
| hash_to_xml(xml, content) | ||
| end | ||
| end | ||
| elsif value.is_a?(Array) | ||
| parent_tag = singularize(key) | ||
|
|
||
| if parent_tag == key.to_s | ||
| value.each do |value| | ||
| hash_to_xml(xml, parent_tag => value) | ||
| end | ||
| else | ||
| xml.tag!(key) do | ||
| value.each do |value| | ||
| hash_to_xml(xml, parent_tag => value) | ||
| end | ||
| end | ||
| end | ||
| else | ||
| xml.tag! key, value | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Try to use activesupport's singularize, failback to simplified implementation | ||
| def singularize(word) | ||
| word = word.to_s | ||
| if word.respond_to?(:singularize) | ||
| word.singularize | ||
| else | ||
| if word =~ /(c|s|x)es$/ | ||
| word.sub(/(c|s|x)es$/, '\1') | ||
| else | ||
| word.sub(/s$/, '') | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def call_action_block | ||
| request = Soap::Request.new(env, request, params) | ||
| if defined?(logger) && logger | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like Soap::Request should know about logging and log request while/before/after execution. |
||
| logger.info "SOAP Request: #{request.action}" | ||
| end | ||
| response = request.execute | ||
| builder :response, locals: {wsdl: response.wsdl, params: response.params}, :views => self.soap_views | ||
| builder :response, views: self.soap_views, locals: { | ||
| wsdl: response.wsdl, | ||
| params: response.params, | ||
| soap_headers: response.headers | ||
| } | ||
| rescue Soap::Error => e | ||
| builder :error, locals: {e: e}, :views => self.soap_views | ||
| if defined?(logger) && logger | ||
| logger.info "SOAP Request: #{env['HTTP_SOAPACTION']} - Undefined Soap Action" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's not correct. If we catch error that does not mean we catch Undefined Soap Action.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, thanks for point it |
||
| end | ||
| builder :error, locals: {e: e}, views: self.soap_views | ||
| end | ||
|
|
||
| def get_wsdl | ||
| if defined?(logger) && logger | ||
| logger.info "SOAP: wsdl request" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thats it? seriously? Just log that wsdl request?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it was useful in my case to verify if client working correctly or not. Probably just standard sinatra's logging will be enough |
||
| end | ||
| if defined?(settings.wsdl_path) | ||
| path = File.join(settings.public_folder, settings.wsdl_path) | ||
| if File.exist?(path) | ||
|
|
@@ -26,7 +95,7 @@ def get_wsdl | |
| raise "No wsdl file" | ||
| end | ||
| else | ||
| builder :wsdl, locals: {wsdl: Soap::Wsdl.actions}, :views => self.soap_views | ||
| builder :wsdl, locals: {wsdl: Soap::Wsdl.actions}, views: self.soap_views | ||
| end | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,20 +4,33 @@ module Sinatra | |
| module Soap | ||
| class Request | ||
|
|
||
| attr_reader :wsdl, :action, :env, :request, :params | ||
| attr_reader :wsdl, :action, :env, :request, :params, :response | ||
|
|
||
| alias_method :body, :params | ||
|
|
||
| def initialize(env, request, params) | ||
| @env = env | ||
| @request = request | ||
| @params = params | ||
| @header = {} | ||
| parse_request | ||
| end | ||
|
|
||
|
|
||
| def execute | ||
| request_block = wsdl.block | ||
| @response = Soap::Response.new(wsdl, nil) | ||
| response_hash = self.instance_eval(&request_block) | ||
| Soap::Response.new(wsdl, response_hash) | ||
|
|
||
| if @response.params == nil | ||
| if response_hash.is_a?(Array) | ||
| @response.params, @response.headers = response_hash | ||
| else | ||
| @response.params = response_hash | ||
| end | ||
| end | ||
|
|
||
| @response | ||
| end | ||
|
|
||
| alias_method :orig_params, :params | ||
|
|
@@ -27,14 +40,21 @@ def action | |
| orig_params[:action] = env['HTTP_SOAPACTION'].to_s.gsub(/^"(.*)"$/, '\1').to_sym | ||
| end | ||
|
|
||
|
|
||
| def params | ||
| def params | ||
| return orig_params[:soap] unless orig_params[:soap].nil? | ||
| rack_input = env["rack.input"].read | ||
| env["rack.input"].rewind | ||
| orig_params[:soap] = nori.parse(rack_input)[:Envelope][:Body][action] | ||
| end | ||
|
|
||
| def header | ||
| return orig_params[:soap_header] unless orig_params[:soap_header].nil? | ||
| rack_input = env["rack.input"].read | ||
| env["rack.input"].rewind | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer single quotes (rubocop)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. never understood the point of this rule, afaik now days compiler is smart enough to determine if have interpolations or not... |
||
| orig_params[:soap_header] = nori.parse(rack_input)[:Envelope][:Header] | ||
| end | ||
|
|
||
| alias_method :soap_header, :header | ||
|
|
||
| def wsdl | ||
| @wsdl = Soap::Wsdl.new(action) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| xml.instruct! | ||
| xml.tag! 'soap:Envelope', 'xmlns:soap' => 'http://schemas.xmlsoap.org/soap/envelope/', | ||
| 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance' do | ||
| if soap_headers | ||
| xml.tag! 'soap:Header' do | ||
| hash_to_xml(xml, soap_headers) | ||
| end | ||
| end | ||
| xml.tag! 'soap:Body' do | ||
| xml.tag! "soap:#{wsdl.action}Response" do | ||
| params.each do |key, value| | ||
| xml.tag! key, value | ||
| end | ||
| xml.tag! "soap:#{wsdl.reply_name || "#{wsdl.action}Response"}" do | ||
| hash_to_xml(xml, params) | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec| | |
| spec.add_development_dependency "rspec" | ||
| spec.add_development_dependency "rake" | ||
| spec.add_development_dependency "rack-test" | ||
| spec.add_development_dependency "debugger" | ||
| #spec.add_development_dependency "debugger" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would you remove debugger out of development dependencies?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's only for ruby 1.9.*, I think there was some installation issues with ruby 2.4 |
||
|
|
||
|
|
||
| spec.add_runtime_dependency "builder" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| require 'spec_helper' | ||
|
|
||
| describe "Request" do | ||
| def app | ||
| SoapApp | ||
| end | ||
|
|
||
| it "should render xml with params" do | ||
| headers = {"HTTP_SOAPACTION" => 'test_render'} | ||
| message = '<?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wsdl="any" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Body><wsdl:test></wsdl:test></env:Body></env:Envelope>' | ||
| post '/action', message, headers | ||
|
|
||
| response = %{ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
| <soap:Body> | ||
| <soap:TestRenderReply> | ||
| <top> | ||
| <child attr1="attr_value"> | ||
| <value>content</value> | ||
| </child> | ||
| </top> | ||
| </soap:TestRenderReply> | ||
| </soap:Body> | ||
| </soap:Envelope>}.strip | ||
|
|
||
| expect(last_response.body).to eq(response + "\n") | ||
| end | ||
|
|
||
| it "should render xml with params and single content" do | ||
| headers = {"HTTP_SOAPACTION" => 'test_render2'} | ||
| message = '<?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wsdl="any" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Body><wsdl:test></wsdl:test></env:Body></env:Envelope>' | ||
| post '/action', message, headers | ||
|
|
||
| response = %{ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
| <soap:Body> | ||
| <soap:test_render2Response> | ||
| <result status="success"> | ||
| <foo active="true">bar</foo> | ||
| </result> | ||
| </soap:test_render2Response> | ||
| </soap:Body> | ||
| </soap:Envelope>}.strip | ||
|
|
||
| expect(last_response.body).to eq(response + "\n") | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would you like to drop supporting for 1.9 branch without major release? Only major release should make breaking API changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably updating major version will be right. 1.9 support ended 2 years ago, do you know if anyone still using it? I haven't seen it for years
https://www.ruby-lang.org/en/news/2014/01/10/ruby-1-9-3-will-end-on-2015/