GithubHelp home page GithubHelp logo

gyoku_v1's Introduction

GyokuV1

GyokuV1 translates Ruby Hashes to XML.

GyokuV1.xml(:find_user => { :id => 123, "v1:Key" => "api" })
# => "<findUser><id>123</id><v1:Key>api</v1:Key></findUser>"

Build Status Gem Version Code Climate Coverage Status

Installation

GyokuV1 is available through Rubygems and can be installed via:

$ gem install gyoku_v1

or add it to your Gemfile like this:

gem 'gyoku_v1', '~> 1.0'

Hash keys

Hash key Symbols are converted to lowerCamelCase Strings.

GyokuV1.xml(:lower_camel_case => "key")
# => "<lowerCamelCase>key</lowerCamelCase>"

You can change the default conversion formula to :camelcase, :upcase or :none.
Note that options are passed as a second Hash to the .xml method.

GyokuV1.xml({ :camel_case => "key" }, { :key_converter => :camelcase })
# => "<CamelCase>key</CamelCase>"

Hash key Strings are not converted and may contain namespaces.

GyokuV1.xml("XML" => "key")
# => "<XML>key</XML>"

Hash values

  • DateTime objects 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
  • These conventions are also applied to the return value of objects responding to :call
  • All other objects are converted to Strings using :to_s

Special characters

GyokuV1 escapes special characters unless the Hash key ends with an exclamation mark.

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

Self-closing tags

Hash Keys ending with a forward slash create self-closing tags.

GyokuV1.xml(:"self_closing/" => "", "selfClosing/" => nil)
# => "<selfClosing/><selfClosing/>"

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 the :order! key.

GyokuV1.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 the:attributes! key.

GyokuV1.xml(:person => "Eve", :attributes! => { :person => { :id => 1 } })
# => "<person id=\"1\">Eve</person>"

Explicit XML Attributes

In addition to using the :attributes! key, you may also specify attributes through keys beginning with an "@" sign. Since you'll need to set the attribute within the hash containing the node's contents, a :content! key can be used to explicity set the content of the node. The :content! value may be a String, Hash, or Array.

This is particularly useful for self-closing tags.

Using :attributes!

GyokuV1.xml(
  "foo/" => "", 
  :attributes! => {
    "foo/" => {
      "bar" => "1", 
      "biz" => "2", 
      "baz" => "3"
    }
  }
)
# => "<foo baz=\"3\" bar=\"1\" biz=\"2\"/>"

Using "@" keys and ":content!"

GyokuV1.xml(
  "foo/" => {
    :@bar => "1",
    :@biz => "2",
    :@baz => "3",
    :content! => ""
  })
# => "<foo baz=\"3\" bar=\"1\" biz=\"2\"/>"

Example using "@" to get Array of parent tags each with @attributes & :content!

GyokuV1.xml(
  "foo" => [
    {:@name => "bar", :content! => 'gyoku_v1'}
    {:@name => "baz", :@some => "attr", :content! => 'rocks!'}
  ])
# => "<foo name=\"bar\">gyoku_v1</foo><foo name=\"baz\" some=\"attr\">rocks!</foo>"

Naturally, it would ignore :content! if tag is self-closing:

GyokuV1.xml(
  "foo/" => [
    {:@name => "bar", :content! => 'gyoku_v1'}
    {:@name => "baz", :@some => "attr", :content! => 'rocks!'}
  ])
# => "<foo name=\"bar\"/><foo name=\"baz\" some=\"attr\"/>"

This seems a bit more explicit with the attributes rather than having to maintain a hash of attributes.

For backward compatibility, :attributes! will still work. However, "@" keys will override :attributes! keys if there is a conflict.

GyokuV1.xml(:person => {:content! => "Adam", :@id! => 0})
# => "<person id=\"0\">Adam</person>"

Example with ":content!", :attributes! and "@" keys

GyokuV1.xml({ 
  :subtitle => { 
    :@lang => "en", 
    :content! => "It's Godzilla!" 
  }, 
  :attributes! => { :subtitle => { "lang" => "jp" } } 
}
# => "<subtitle lang=\"en\">It's Godzilla!</subtitle>"

The example above shows an example of how you can use all three at the same time.

Notice that we have the attribute "lang" defined twice. The @lang value takes precedence over the :attribute![:subtitle]["lang"] value.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.