Skip to content

Commit

Permalink
initial version extracted from the Savon library
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiii committed Jul 3, 2011
0 parents commit 00cde8c
Show file tree
Hide file tree
Showing 30 changed files with 1,237 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
doc
coverage
pkg
*~
*.gem
.bundle
Gemfile.lock
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
rvm:
- 1.8.7
- 1.9.2
- ruby-head
- ree
- rbx
- rbx-2.0
- jruby
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 1.0.0 (2011-07-03)

* Initial version extracted from the [Savon](http://rubygems.org/gems/savon) library.
Use it to build your own SOAP client and help to improve it!
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source "http://rubygems.org"
gemspec
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2011 Daniel Harrington

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Wasabi [![Build Status](http://travis-ci.org/rubiii/wasabi.png)](http://travis-ci.org/rubiii/wasabi)
=====

A simple WSDL parser.


Installation
------------

Wasabi is available through [Rubygems](http://rubygems.org/gems/wasabi) and can be installed via:

```
$ gem install wasabi
```


Getting started
---------------

``` ruby
document = Wasabi.document File.read("some.wsdl")
```

Get the SOAP endpoint:

``` ruby
document.endpoint
# => "http://soap.example.com"
```

Get the target namespace:

``` ruby
document.namespace
# => "http://v1.example.com"
```

Check whether elementFormDefault is set to `:qualified` or `:unqualified`:

``` ruby
document.element_form_default
# => :qualified
```

Get a list of available SOAP actions (snakecase for convenience):

``` ruby
document.soap_actions
# => [:create_user, :find_user]
```

Get a map of SOAP action Symbols, their input tag and original SOAP action name:

``` ruby
document.operations
# => { :create_user => { :input => "createUser", :action => "createUser" },
# => :find_user => { :input => "findUser", :action => "findUser" } }
```
11 changes: 11 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'bundler'
Bundler::GemHelper.install_tasks

require "rspec/core/rake_task"

RSpec::Core::RakeTask.new do |t|
t.rspec_opts = %w(-c)
end

task :default => :spec
task :test => :spec
11 changes: 11 additions & 0 deletions lib/wasabi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "wasabi/version"
require "wasabi/document"

module Wasabi

# Expects a WSDL document and returns a <tt>Wasabi::Document</tt>.
def self.document(document)
Document.new(document)
end

end
21 changes: 21 additions & 0 deletions lib/wasabi/core_ext/string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Wasabi
module CoreExt
module String

# Returns the String in snakecase.
def snakecase
str = dup
str.gsub! /::/, '/'
str.gsub! /([A-Z]+)([A-Z][a-z])/, '\1_\2'
str.gsub! /([a-z\d])([A-Z])/, '\1_\2'
str.tr! ".", "_"
str.tr! "-", "_"
str.downcase!
str
end unless method_defined?(:snakecase)

end
end
end

String.send :include, Wasabi::CoreExt::String
91 changes: 91 additions & 0 deletions lib/wasabi/document.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
require "nokogiri"
require "wasabi/parser"

module Wasabi

# = Wasabi::Document
#
# Represents a WSDL document.
class Document

# Accepts a WSDL +document+ to parse.
def initialize(document = nil)
self.document = document
end

attr_accessor :document

# Returns whether a +document+ was set.
def document?
!!document
end

# Returns the SOAP endpoint.
def endpoint
@endpoint ||= parser.endpoint
end

# Sets the SOAP endpoint.
attr_writer :endpoint

# Returns the target namespace.
def namespace
@namespace ||= parser.namespace
end

# Sets the target namespace.
attr_writer :namespace

# Returns the value of elementFormDefault.
def element_form_default
@element_form_default ||= parser.element_form_default
end

# Returns a list of available SOAP actions.
def soap_actions
@soap_actions ||= parser.operations.keys
end

# Returns the SOAP action for a given +key+.
def soap_action(key)
operations[key][:action] if operations[key]
end

# Returns the SOAP input for a given +key+.
def soap_input(key)
operations[key][:input] if operations[key]
end

# Returns a map of SOAP operations.
def operations
@operations ||= parser.operations
end

# Returns the raw WSDL document.
# Can be used as a hook to extend the library.
def xml
@xml ||= document
end

private

# Parses the WSDL document and returns the <tt>Wasabi::Parser</tt>.
def parser
@parser ||= guard_parse && parse
end

# Raises an error if the WSDL document is missing.
def guard_parse
return true if xml.kind_of?(String)
raise ArgumentError, "Wasabi needs a WSDL document"
end

# Parses the WSDL document and returns <tt>Wasabi::Parser</tt>.
def parse
parser = Parser.new Nokogiri::XML(xml)
parser.parse
parser
end

end
end
94 changes: 94 additions & 0 deletions lib/wasabi/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
require "uri"
require "wasabi/core_ext/string"

module Wasabi

# = Wasabi::Parser
#
# Parses WSDL documents and remembers their important parts.
class Parser

def initialize(document)
@document = document
@path = []
@operations = {}
@namespaces = {}
@element_form_default = :unqualified
end

# Returns the SOAP endpoint.
attr_reader :endpoint

# Returns the target namespace.
attr_reader :namespace

# Returns the SOAP operations.
attr_reader :operations

# Returns the value of elementFormDefault.
attr_reader :element_form_default

def parse
parse_namespaces
parse_endpoint
parse_operations
end

def parse_namespaces
element_form_default = @document.at_xpath(
"s0:definitions/s0:types/xs:schema/@elementFormDefault",
"s0" => "http://schemas.xmlsoap.org/wsdl/",
"xs" => "http://www.w3.org/2001/XMLSchema")
@element_form_default = element_form_default.to_s.to_sym if element_form_default

namespace = @document.at_xpath(
"s0:definitions/@targetNamespace",
"s0" => "http://schemas.xmlsoap.org/wsdl/")
@namespace = namespace.to_s if namespace
end

def parse_endpoint
endpoint = @document.at_xpath(
"s0:definitions/s0:service//soap11:address/@location",
"s0" => "http://schemas.xmlsoap.org/wsdl/",
"soap11" => "http://schemas.xmlsoap.org/wsdl/soap/")
endpoint ||= @document.at_xpath(
"s0:definitions/s0:service//soap12:address/@location",
"s0" => "http://schemas.xmlsoap.org/wsdl/",
"soap12" => "http://schemas.xmlsoap.org/wsdl/soap12/")

@endpoint = URI(URI.escape(endpoint.to_s)) if endpoint
end

def parse_operations
operations = @document.xpath(
"s0:definitions/s0:binding/s0:operation",
"s0" => "http://schemas.xmlsoap.org/wsdl/")

operations.each do |operation|
name = operation.attribute("name").to_s

soap_action = operation.at_xpath(".//soap11:operation/@soapAction",
"soap11" => "http://schemas.xmlsoap.org/wsdl/soap/"
)
soap_action ||= operation.at_xpath(".//soap12:operation/@soapAction",
"soap12" => "http://schemas.xmlsoap.org/wsdl/soap12/"
)

if soap_action
soap_action = soap_action.to_s

action = soap_action && !soap_action.empty? ? soap_action : name
input = (!name || name.empty?) ? action.split("/").last : name

@operations[input.snakecase.to_sym] =
{ :action => action, :input => input }
elsif !@operations[name.snakecase.to_sym]
@operations[name.snakecase.to_sym] =
{ :action => name, :input => name }
end
end
end

end
end
5 changes: 5 additions & 0 deletions lib/wasabi/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Wasabi

VERSION = "1.0.0"

end
Loading

0 comments on commit 00cde8c

Please sign in to comment.