Skip to content

Instantly share code, notes, and snippets.

@frnz
Created January 29, 2010 19:53
Show Gist options
  • Save frnz/290047 to your computer and use it in GitHub Desktop.
Save frnz/290047 to your computer and use it in GitHub Desktop.
# los requires no son necesarios dentro de rails, obviamente :p
require 'rubygems'
require 'active_support'
# format_event retorna un hash formateado a como querés, por lo cual al resultado le podés hacer .to_json
def format_event(event_hash)
# Si no hay imagenes retorna un hash con info "vacía"
return {:totalImages => '0', :images => {}} if (event_hash['images'].nil? || event_hash['images']['image'].nil?)
# por legibilidad está en varias líneas :p
{
# el totalImages lo querés como cadena, cierto?
'totalImages' => event_hash['images']['image'].count.to_s,
'images' =>
event_hash['images']['image'].map {|image| {
:id => image['id'].to_s,
:sizes => %w[small medium thumb].map {|size|
{ size => {
:url => image[size]['url'].to_s, :caption => image[size]['caption'].to_s,
:width => image[size]['width'].to_s, :height => image[size]['height'].to_s
}
}
}
}
}
}
end
# El xml que me enviaste:
# más abajo uso format_event para pasar éste xml a json
xml = <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<event id="E0-001-027213439-8">
<url>http://eventful.com/newyorkcity/events/lukas-rossi-highline-ballroom-/E0-001-027213439-8</url>
<title>Lukas Rossi at The Highline Ballroom</title>
<description>Singer-Songwriter Lukas Rossi Takes His Unique Rock Style Acoustic for this ALL AGES show at Highline Ballroom.
Make the night a truly special experience! Have dinner with Lukas and be his guest for the show!
For details please visit http://lukasrossi.com/news
For further details on other upcoming shows in this area, go to http://lukasrossi.com</description>
<start_time>2010-01-31 20:00:00</start_time>
<stop_time/>
<all_day>0</all_day>
<tz_id/>
<tz_olson_path/>
<tz_country/>
<tz_city/>
<venue_name>Highline Ballroom</venue_name>
<venue_id>V0-001-001140718-3</venue_id>
<venue_type>Bar/Night Club</venue_type>
<venue_display>1</venue_display>
<address>431 West Sixteenth Street</address>
<city>New York</city>
<region>New York</region>
<region_abbr>NY</region_abbr>
<postal_code>10011</postal_code>
<country>United States</country>
<country_abbr2>US</country_abbr2>
<country_abbr>USA</country_abbr>
<latitude>40.74307</latitude>
<longitude>-74.005716</longitude>
<geocode_type>EVDB Geocoder</geocode_type>
<created>2009-12-26 20:03:03</created>
<modified>2009-12-26 20:03:03</modified>
<owner>monkeyhq</owner>
<privacy>1</privacy>
<free/>
<price/>
<withdrawn>0</withdrawn>
<withdrawn_note/>
<recurrence/>
<parents/>
<children/>
<links/>
<comments>
<comment id="">
<text/>
<username/>
<time/>
</comment>
</comments>
<trackbacks/>
<performers>
<performer>
<id>P0-001-000080359-4</id>
<url>http://eventful.com/performers/lukas-rossi-/P0-001-000080359-4?utm_source=apis&amp;utm_medium=apim&amp;utm_campaign=apic</url>
<name>Lukas Rossi</name>
<short_bio>Singer/Songwriter</short_bio>
<creator>monkeyhq</creator>
<linker>monkeyhq</linker>
</performer>
</performers>
<images>
<image>
<id>I0-001-002643459-8</id>
<creator>monkeyhq</creator>
<url>http://static.eventful.com/images/small/I0-001/002/643/459-8.jpeg</url>
<width>48</width>
<height>48</height>
<thumb>
<url>http://static.eventful.com/images/thumb/I0-001/002/643/459-8.jpeg</url>
<width>48</width>
<height>48</height>
</thumb>
<small>
<url>http://static.eventful.com/images/small/I0-001/002/643/459-8.jpeg</url>
<width>48</width>
<height>48</height>
</small>
<medium>
<url>http://static.eventful.com/images/medium/I0-001/002/643/459-8.jpeg</url>
<width>128</width>
<height>128</height>
</medium>
</image>
<image>
<id>I0-001-002643373-9</id>
<creator>monkeyhq</creator>
<url>http://static.eventful.com/images/small/I0-001/002/643/373-9.jpeg</url>
<width>48</width>
<height>48</height>
<thumb>
<url>http://static.eventful.com/images/thumb/I0-001/002/643/373-9.jpeg</url>
<width>48</width>
<height>48</height>
</thumb>
<small>
<url>http://static.eventful.com/images/small/I0-001/002/643/373-9.jpeg</url>
<width>48</width>
<height>48</height>
</small>
<medium>
<url>http://static.eventful.com/images/medium/I0-001/002/643/373-9.jpeg</url>
<width>128</width>
<height>128</height>
</medium>
</image>
<image>
<id>I0-001-002296751-1</id>
<creator>monkeyhq</creator>
<url>http://static.eventful.com/images/small/I0-001/002/296/751-1.jpeg</url>
<width>48</width>
<height>48</height>
<thumb>
<url>http://static.eventful.com/images/thumb/I0-001/002/296/751-1.jpeg</url>
<width>48</width>
<height>48</height>
</thumb>
<small>
<url>http://static.eventful.com/images/small/I0-001/002/296/751-1.jpeg</url>
<width>48</width>
<height>48</height>
</small>
<medium>
<url>http://static.eventful.com/images/medium/I0-001/002/296/751-1.jpeg</url>
<width>128</width>
<height>128</height>
</medium>
</image>
<image>
<id>I0-001-002617058-4</id>
<creator>monkeyhq</creator>
<url>http://static.eventful.com/images/small/I0-001/002/617/058-4.jpeg</url>
<width>48</width>
<height>48</height>
<thumb>
<url>http://static.eventful.com/images/thumb/I0-001/002/617/058-4.jpeg</url>
<width>48</width>
<height>48</height>
</thumb>
<small>
<url>http://static.eventful.com/images/small/I0-001/002/617/058-4.jpeg</url>
<width>48</width>
<height>48</height>
</small>
<medium>
<url>http://static.eventful.com/images/medium/I0-001/002/617/058-4.jpeg</url>
<width>128</width>
<height>128</height>
</medium>
</image>
<image>
<id>I0-001-002511751-5</id>
<creator>monkeyhq</creator>
<url>http://static.eventful.com/images/small/I0-001/002/511/751-5.jpeg</url>
<width>48</width>
<height>48</height>
<thumb>
<url>http://static.eventful.com/images/thumb/I0-001/002/511/751-5.jpeg</url>
<width>48</width>
<height>48</height>
</thumb>
<small>
<url>http://static.eventful.com/images/small/I0-001/002/511/751-5.jpeg</url>
<width>48</width>
<height>48</height>
</small>
<medium>
<url>http://static.eventful.com/images/medium/I0-001/002/511/751-5.jpeg</url>
<width>128</width>
<height>128</height>
</medium>
</image>
<image>
<id>I0-001-002511750-6</id>
<creator>monkeyhq</creator>
<url>http://static.eventful.com/images/small/I0-001/002/511/750-6.jpeg</url>
<width>48</width>
<height>48</height>
<thumb>
<url>http://static.eventful.com/images/thumb/I0-001/002/511/750-6.jpeg</url>
<width>48</width>
<height>48</height>
</thumb>
<small>
<url>http://static.eventful.com/images/small/I0-001/002/511/750-6.jpeg</url>
<width>48</width>
<height>48</height>
</small>
<medium>
<url>http://static.eventful.com/images/medium/I0-001/002/511/750-6.jpeg</url>
<width>128</width>
<height>128</height>
</medium>
</image>
<image>
<id>I0-001-001833535-3</id>
<creator>monkeyhq</creator>
<url>http://static.eventful.com/images/small/I0-001/001/833/535-3.jpeg</url>
<width>48</width>
<height>48</height>
<thumb>
<url>http://static.eventful.com/images/thumb/I0-001/001/833/535-3.jpeg</url>
<width>48</width>
<height>48</height>
</thumb>
<small>
<url>http://static.eventful.com/images/small/I0-001/001/833/535-3.jpeg</url>
<width>48</width>
<height>48</height>
</small>
<medium>
<url>http://static.eventful.com/images/medium/I0-001/001/833/535-3.jpeg</url>
<width>128</width>
<height>128</height>
</medium>
</image>
<image>
<id>I0-001-001833518-6</id>
<creator>monkeyhq</creator>
<url>http://static.eventful.com/images/small/I0-001/001/833/518-6.jpeg</url>
<width>48</width>
<height>48</height>
<thumb>
<url>http://static.eventful.com/images/thumb/I0-001/001/833/518-6.jpeg</url>
<width>48</width>
<height>48</height>
</thumb>
<small>
<url>http://static.eventful.com/images/small/I0-001/001/833/518-6.jpeg</url>
<width>48</width>
<height>48</height>
</small>
<medium>
<url>http://static.eventful.com/images/medium/I0-001/001/833/518-6.jpeg</url>
<width>128</width>
<height>128</height>
</medium>
</image>
<image>
<id>I0-001-001480020-2</id>
<creator>monkeyhq</creator>
<url>http://static.eventful.com/images/small/I0-001/001/480/020-2.jpeg</url>
<width>48</width>
<height>48</height>
<thumb>
<url>http://static.eventful.com/images/thumb/I0-001/001/480/020-2.jpeg</url>
<width>48</width>
<height>48</height>
</thumb>
<small>
<url>http://static.eventful.com/images/small/I0-001/001/480/020-2.jpeg</url>
<width>48</width>
<height>48</height>
</small>
<medium>
<url>http://static.eventful.com/images/medium/I0-001/001/480/020-2.jpeg</url>
<width>128</width>
<height>128</height>
</medium>
</image>
<image>
<id>I0-001-000161587-1</id>
<creator>KimW</creator>
<url>http://static.eventful.com/images/small/I0-001/000/161/587-1.jpg</url>
<width>48</width>
<height>48</height>
<thumb>
<url>http://static.eventful.com/images/thumb/I0-001/000/161/587-1.jpg</url>
<width>48</width>
<height>48</height>
</thumb>
<small>
<url>http://static.eventful.com/images/small/I0-001/000/161/587-1.jpg</url>
<width>48</width>
<height>48</height>
</small>
<medium>
<url>http://static.eventful.com/images/medium/I0-001/000/161/587-1.jpg</url>
<width>128</width>
<height>128</height>
</medium>
</image>
</images>
<tags>
<tag id="acoustic">
<title>acoustic</title>
<owner>monkeyhq</owner>
</tag>
<tag id="alternative">
<title>alternative</title>
<owner>monkeyhq</owner>
</tag>
<tag id="music">
<title>music</title>
<owner>monkeyhq</owner>
</tag>
<tag id="pop">
<title>Pop</title>
<owner>evdb</owner>
</tag>
<tag id="rock">
<title>rock</title>
<owner>monkeyhq</owner>
</tag>
<tag id="rockstar">
<title>rockstar</title>
<owner>monkeyhq</owner>
</tag>
<tag id="singersongwriter">
<title>singer-songwriter</title>
<owner>monkeyhq</owner>
</tag>
<tag id="singles_social">
<title>singles_social</title>
<owner>monkeyhq</owner>
</tag>
<tag id="ticketmaster">
<title>ticketmaster</title>
<owner>evdb</owner>
</tag>
<tag id="ticketmastercom">
<title>ticketmaster.com</title>
<owner>evdb</owner>
</tag>
<tag id="canada">
<title>canada</title>
<owner>evdb</owner>
</tag>
<tag id="cleavage">
<title>Cleavage</title>
<owner>evdb</owner>
</tag>
<tag id="concerts">
<title>concerts</title>
<owner>evdb</owner>
</tag>
<tag id="headspin">
<title>Headspin</title>
<owner>evdb</owner>
</tag>
<tag id="indie">
<title>Indie</title>
<owner>evdb</owner>
</tag>
<tag id="losangeles">
<title>Los Angeles</title>
<owner>evdb</owner>
</tag>
<tag id="lukasrossi">
<title>Lukas Rossi</title>
<owner>evdb</owner>
</tag>
<tag id="riseelectric">
<title>Rise Electric</title>
<owner>evdb</owner>
</tag>
<tag id="rock_pop">
<title>rock_pop</title>
<owner>evdb</owner>
</tag>
<tag id="rockstarsupernova">
<title>Rock Star Supernova</title>
<owner>evdb</owner>
</tag>
<tag id="songwriter">
<title>songwriter</title>
<owner>evdb</owner>
</tag>
<tag id="starsdown">
<title>Stars Down</title>
<owner>evdb</owner>
</tag>
<tag id="supernova">
<title>Supernova</title>
<owner>evdb</owner>
</tag>
<tag id="toronto">
<title>Toronto</title>
<owner>evdb</owner>
</tag>
</tags>
<flags/>
<calendars/>
<groups/>
<going>
<user>
<username>gaylejennifer</username>
<status>going</status>
<thumb_url/>
</user>
<user>
<username>monkeyhq</username>
<status>watching</status>
<thumb_url>http://static.eventful.com/images/thumb/I0-001/002/139/590-2.jpeg</thumb_url>
</user>
<user>
<username>lindsaysevents</username>
<status>watching</status>
<thumb_url/>
</user>
<user>
<username>tigereventful2</username>
<status>watching</status>
<thumb_url/>
</user>
<user>
<username>DeForbidden</username>
<status>watching</status>
<thumb_url/>
</user>
</going>
<properties/>
<categories>
<category>
<id>music</id>
<name>Concerts | Live Music</name>
</category>
<category>
<id>singles_social</id>
<name>Nightlife | Singles</name>
</category>
</categories>
</event>
EOF
# probando el código
event_hash = Hash.from_xml(xml)['event']
y format_event( event_hash )
puts "\n\nJson formateado:\n\n #{format_event( event_hash ).to_json}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment