Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Converts Ruby Hashes to XML
Ruby
tag: v0.3.0

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.gitignore
.rspec
Gemfile
LICENSE
README.md
Rakefile
gyoku.gemspec

README.md

Gyoku

Gyoku translates Ruby Hashes to XML.

Bugs | Docs

Installation

The gem is available through Rubygems and can be installed via:

$ gem install gyoku

An example

Gyoku.xml :find_user => { :id => 123, "wsdl:Key" => "api" }
# => "<findUser><id>123</id><wsdl:Key>api</wsdl:Key></findUser>"

As you might notice, Gyoku follows a couple of conventions for translating Hashes into XML.

Conventions

  • Hash key Symbols are converted to lowerCamelCase Strings
  • Hash key Strings are not converted and may contain namespaces
  • Hash keys ending with a forward slash create self-closing tags
  • DateTime values are converted to xs:dateTime Strings
  • Objects responding to :to_datetime (except Strings) are converted to xs:dateTime Strings
  • TrueClass and FalseClass objects are converted to "true" and "false" Strings
  • NilClass objects are converted to xsi:nil tags
  • All other objects are converted to Strings using :to_s

Special characters

Gyoku escapes special characters unless the Hash key ends with an exclamation mark:

Gyoku.xml :escaped => "<tag />", :not_escaped! => "<tag />"
# => "<escaped>&lt;tag /&gt;</escaped><notEscaped><tag /></notEscaped>"

Sort XML tags

In case you need the XML tags to be in a specific order, you can specify the order through an additional Array stored under an :order! key:

Gyoku.xml :name => "Eve", :id => 1, :order! => [:id, :name]
# => "<id>1</id><name>Eve</name>"

XML attributes

Adding XML attributes is rather ugly, but it can be done by specifying an additional Hash stored under an :attributes! key:

Gyoku.xml :person => "Eve", :attributes! => { :person => { :id => 1 } }
# => "<person id=\"1\">Eve</person>"
Something went wrong with that request. Please try again.