Project

mapricot

0.0
No commit activity in last 3 years
No release in over 3 years
XML to object mapper with an interface similar to ActiveRecord associations.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 0.7
>= 1.1.3
>= 1.4.1
 Project Readme

Mapricot¶ ↑

XML to object mapper with an interface similar to ActiveRecord associations.

Install¶ ↑

sudo gem install mapricot

Example Usage¶ ↑

Super Simple¶ ↑

require 'mapricot'

simple_xml = %(
  <user>
    <id>1</name>
    <name>Bob</name>
    <pet>cat</pet>
    <pet>dog</pet>
  </user>
)

class User < Mapricot::Base
  has_one   :id,    :integer
  has_one   :name,  :string
  has_many  :pets,  :string
end

user = User.new(simple_xml)
puts user.id                # => 1
puts user.name              # => "Bob"
puts user.pets              # => ["cat", "dog"]

A little more realistic¶ ↑

require 'mapricot'

xml = %(
  <user>
    <id>2</name>
    <name>Sally</name>
    <location code="ny123">
      <city>New York</city>
      <state>NY</state>
    </location>
    <hobby>
      <description>Skiing</description>
      <number_of_years>2</number_of_years>
    </hobby>
    <hobby>
      <description>Hiking</description>
      <number_of_years>3</number_of_years>
    </hobby>
  </user>
)

class User < Mapricot::Base
  has_one   :id,        :integer
  has_one   :name       # Tag type will default to :string
  has_many  :pets
  has_one   :location,  :xml
  has_many  :hobbies,   :xml
end

class Location < Mapricot::Base
  has_attribute :code
  has_one       :city
  has_one       :state
end

class Hobby < Mapricot::Base
  has_one :description,     :string
  has_one :number_of_years, :integer
end

user = User.new(xml)
puts user.name            # => "Sally"
puts user.pets.inspect    # => []
puts user.location.class  # => Location
puts user.location.city   # => "New York"
puts user.location.state  # => "NY"
puts user.location.code   # => "ny123"
puts user.hobbies.class         # => Array
puts user.hobbies.first.class   # => Hobby

user.hobbies.each do |hobby|
  puts "#{hobby.description} for #{hobby.number_of_years} years"
end
# => Skiing for 2 years
# => Hiking for 3 years

Parsing from a URL¶ ↑

If you want to parse xml from a url, do something like this:

require 'open-uri'

Foo < Mapricot::Base
...
end

url = 'http://www.example.com'
Foo.new(open(url))

Changing the xml library¶ ↑

Mapricot.parser = :hpricot
Mapricot.parser = :nokogiri
Mapricot.parser = :libxml

Other stuff¶ ↑

Your classes should inherit from Mapricot::Base, which provides the class methods

  • has_one(name, type, opts = {})

  • has_many(name, type, opts = {})

  • has_attribute(name)

Copyright © 2010 Lou Zell, released under the MIT license