Skip to content

Commit

Permalink
Working on updating happy mapper to include options
Browse files Browse the repository at this point in the history
Signed-off-by: John Nunemaker <nunemaker@gmail.com>
  • Loading branch information
Jeremy Friesen authored and jnunemaker committed Jan 5, 2009
1 parent f4018f0 commit c0aa03a
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 3 deletions.
19 changes: 19 additions & 0 deletions examples/current_weather.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'happymapper')

file_contents = File.read(dir + '/../spec/fixtures/current_weather.xml')

class CurrentWeather
include HappyMapper
tag 'aws:ob'
element :temperature, Integer, :tag => 'aws:temp'
element :feels_like, Integer, :tag => 'aws:feels-like'
element :current_condition, String, :tag => 'aws:current-condition', :attributes => {:icon => String}
end

CurrentWeather.parse(file_contents).each do |current_weather|
puts "temperature: #{current_weather.temperature}"
puts "feels_like: #{current_weather.feels_like}"
puts "current_condition: #{current_weather.current_condition}"
puts "current_condition.icon: #{current_weather.current_condition.icon}"
end
11 changes: 8 additions & 3 deletions lib/happymapper/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def name=(new_name)
@name = new_name.to_s
end

def from_xml_node(node, namespace=nil)
def from_xml_node(node, namespace)
if primitive?
typecast(value_from_xml_node(node, namespace))
else
Expand All @@ -40,7 +40,12 @@ def attribute?
!element?
end

def typecast(value)
def typecast(*args)
args.flatten!
value = args.shift
if options[:attributes]
options[:attributes].each {|attr| }
end
return value if value.kind_of?(type) || value.nil?
begin
if type == String then value.to_s
Expand Down Expand Up @@ -77,7 +82,7 @@ def value_from_xml_node(node, namespace=nil)
if element?
depth = options[:deep] ? './/' : ''
result = node.find_first("#{depth}#{namespace}#{tag}")
result ? result.content : nil
return result ? [result.content, result.attributes] : nil
else
node[tag]
end
Expand Down
89 changes: 89 additions & 0 deletions spec/fixtures/current_weather.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<aws:weather xmlns:aws="http://www.aws.com/aws">
<aws:api version="2.0"/>
<aws:WebURL>http://weather.weatherbug.com/IN/Carmel-weather.html?ZCode=Z5546&amp;Units=0&amp;stat=MOCAR</aws:WebURL>
<aws:ob>
<aws:ob-date>
<aws:year number="2008"/>
<aws:month number="12" text="December" abbrv="Dec"/>
<aws:day number="30" text="Tuesday" abbrv="Tue"/>
<aws:hour number="4" hour-24="16"/>
<aws:minute number="18"/>
<aws:second number="01"/>
<aws:am-pm abbrv="PM"/>
<aws:time-zone offset="-5" text="Eastern Standard Time" abbrv="EST"/>
</aws:ob-date>
<aws:requested-station-id>mocar</aws:requested-station-id>
<aws:station-id>MOCAR</aws:station-id>
<aws:station>Mohawk Trail ES</aws:station>
<aws:city-state zipcode="46033">Carmel, IN</aws:city-state>
<aws:country>USA</aws:country>
<aws:latitude>39.9711111111111</aws:latitude>
<aws:longitude>-86.0938888888889</aws:longitude>
<aws:site-url>http://www1.ccs.k12.in.us/mte/home</aws:site-url>
<aws:aux-temp units="&amp;deg;F">74</aws:aux-temp>
<aws:aux-temp-rate units="&amp;deg;F">+0.0</aws:aux-temp-rate>
<aws:current-condition icon="http://deskwx.weatherbug.com/images/Forecast/icons/cond007.gif">Sunny</aws:current-condition>
<aws:dew-point units="&amp;deg;F">35</aws:dew-point>
<aws:elevation units="ft">817</aws:elevation>
<aws:feels-like units="&amp;deg;F">51</aws:feels-like>
<aws:gust-time>
<aws:year number="0001"/>
<aws:month number="1" text="January" abbrv="Jan"/>
<aws:day number="1" text="Monday" abbrv="Mon"/>
<aws:hour number="12" hour-24="00"/>
<aws:minute number="00"/>
<aws:second number="00"/>
<aws:am-pm abbrv="AM"/>
<aws:time-zone offset="-5" text="Eastern Standard Time" abbrv="EST"/>
</aws:gust-time>
<aws:gust-direction>W</aws:gust-direction>
<aws:gust-speed units="mph">25</aws:gust-speed>
<aws:humidity units="%">53</aws:humidity>
<aws:humidity-high units="%">100.0</aws:humidity-high>
<aws:humidity-low units="%">42.5</aws:humidity-low>
<aws:humidity-rate>-5.0</aws:humidity-rate>
<aws:indoor-temp units="&amp;deg;F">75</aws:indoor-temp>
<aws:indoor-temp-rate units="&amp;deg;F">+0.0</aws:indoor-temp-rate>
<aws:light>28</aws:light>
<aws:light-rate>-1.5</aws:light-rate>
<aws:moon-phase moon-phase-img="http://api.wxbug.net/images/moonphase/mphase02.gif">-10</aws:moon-phase>
<aws:pressure units="&quot;">29.71</aws:pressure>
<aws:pressure-high units="&quot;">30.18</aws:pressure-high>
<aws:pressure-low units="&quot;">29.71</aws:pressure-low>
<aws:pressure-rate units="&quot;/h">-0.04</aws:pressure-rate>
<aws:rain-month units="&quot;">6.64</aws:rain-month>
<aws:rain-rate units="&quot;/h">0.00</aws:rain-rate>
<aws:rain-rate-max units="&quot;/h">0.00</aws:rain-rate-max>
<aws:rain-today units="&quot;">0.00</aws:rain-today>
<aws:rain-year units="&quot;">53.83</aws:rain-year>
<aws:temp units="&amp;deg;F">51.8</aws:temp>
<aws:temp-high units="&amp;deg;F">52</aws:temp-high>
<aws:temp-low units="&amp;deg;F">29</aws:temp-low>
<aws:temp-rate units="&amp;deg;F/h">+2.5</aws:temp-rate>
<aws:sunrise>
<aws:year number="2008"/>
<aws:month number="12" text="December" abbrv="Dec"/>
<aws:day number="30" text="Tuesday" abbrv="Tue"/>
<aws:hour number="8" hour-24="08"/>
<aws:minute number="06"/>
<aws:second number="02"/>
<aws:am-pm abbrv="AM"/>
<aws:time-zone offset="-5" text="Eastern Standard Time" abbrv="EST"/>
</aws:sunrise>
<aws:sunset>
<aws:year number="2008"/>
<aws:month number="12" text="December" abbrv="Dec"/>
<aws:day number="30" text="Tuesday" abbrv="Tue"/>
<aws:hour number="5" hour-24="17"/>
<aws:minute number="28"/>
<aws:second number="53"/>
<aws:am-pm abbrv="PM"/>
<aws:time-zone offset="-5" text="Eastern Standard Time" abbrv="EST"/>
</aws:sunset>
<aws:wet-bulb units="&amp;deg;F">44.24</aws:wet-bulb>
<aws:wind-speed units="mph">4</aws:wind-speed>
<aws:wind-speed-avg units="mph">7</aws:wind-speed-avg>
<aws:wind-direction>SSW</aws:wind-direction>
<aws:wind-direction-avg>SW</aws:wind-direction-avg>
</aws:ob>
</aws:weather>

0 comments on commit c0aa03a

Please sign in to comment.