Install
# add github to your sources if you haven't
$ gem sources -a http://gems.github.com
$ sudo gem install jnunemaker-happymapper
# (when rubyforge approves and I release there, you can do this)
$ sudo gem install happymapper
Examples
Given the following xml:
<statuses type="array">
<status>
<created_at>Sat Aug 09 05:38:12 +0000 2008</created_at>
<id>882281424</id>
<text>I so just thought the guy lighting the Olympic torch was falling when he began to run on the wall. Wow that would have been catastrophic.</text>
<source>web</source>
<truncated>false</truncated>
<in_reply_to_status_id>1234</in_reply_to_status_id>
<in_reply_to_user_id>12345</in_reply_to_user_id>
<favorited></favorited>
<user>
<id>4243</id>
<name>John Nunemaker</name>
<screen_name>jnunemaker</screen_name>
<location>Mishawaka, IN, US</location>
<description>Loves his wife, ruby, notre dame football and iu basketball</description>
<profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/53781608/Photo_75_normal.jpg</profile_image_url>
<url>http://addictedtonew.com</url>
<protected>false</protected>
<followers_count>486</followers_count>
</user>
</status>
</statuses>
You could have the following objects:
class User
include HappyMapper
element :id, Integer
element :name, String
element :screen_name, String
element :location, String
element :description, String
element :profile_image_url, String
element :url, String
element :protected, Boolean
element :followers_count, Integer
end
class Status
include HappyMapper
element :id, Integer
element :text, String
element :created_at, Time
element :source, String
element :truncated, Boolean
element :in_reply_to_status_id, Integer
element :in_reply_to_user_id, Integer
element :favorited, Boolean
has_one :user, User
end
statuses = Status.parse(file_contents)
statuses.each do |status|
puts status.user.name, status.user.screen_name, status.text, status.source, ''
end
Support
Conversations welcome in the google group and bugs/features over at Lightouse.