happymapper 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.1.3 2008-12-31
2
+ * 1 major enhancement:
3
+ * Added parsing of attributes of elements that are also mapped, see current_weather.rb for example (jeremyf)
4
+
1
5
  == 0.1.2 2008-12-12
2
6
  * 1 major enhancement:
3
7
  * Fixed that :deep only worked for first item (dvrensk)
data/Manifest.txt CHANGED
@@ -8,6 +8,7 @@ TODO.txt
8
8
  config/hoe.rb
9
9
  config/requirements.rb
10
10
  examples/amazon.rb
11
+ examples/current_weather.rb
11
12
  examples/post.rb
12
13
  examples/twitter.rb
13
14
  happymapper.gemspec
@@ -22,6 +23,7 @@ script/destroy
22
23
  script/generate
23
24
  script/txt2html
24
25
  setup.rb
26
+ spec/fixtures/current_weather.xml
25
27
  spec/fixtures/pita.xml
26
28
  spec/fixtures/posts.xml
27
29
  spec/fixtures/statuses.xml
@@ -0,0 +1,19 @@
1
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require File.join(dir, 'happymapper')
3
+
4
+ file_contents = File.read(dir + '/../spec/fixtures/current_weather.xml')
5
+
6
+ class CurrentWeather
7
+ include HappyMapper
8
+ tag 'aws:ob'
9
+ element :temperature, Integer, :tag => 'aws:temp'
10
+ element :feels_like, Integer, :tag => 'aws:feels-like'
11
+ element :current_condition, String, :tag => 'aws:current-condition', :attributes => {:icon => String}
12
+ end
13
+
14
+ CurrentWeather.parse(file_contents).each do |current_weather|
15
+ puts "temperature: #{current_weather.temperature}"
16
+ puts "feels_like: #{current_weather.feels_like}"
17
+ puts "current_condition: #{current_weather.current_condition}"
18
+ puts "current_condition.icon: #{current_weather.current_condition.icon}"
19
+ end
data/happymapper.gemspec CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{happymapper}
5
- s.version = "0.1.2"
5
+ s.version = "0.1.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["John Nunemaker"]
9
- s.date = %q{2008-12-12}
9
+ s.date = %q{2009-01-04}
10
10
  s.description = %q{object to xml mapping library}
11
11
  s.email = ["nunemaker@gmail.com"]
12
12
  s.extra_rdoc_files = ["History.txt", "License.txt", "Manifest.txt", "PostInstall.txt", "README.txt", "TODO.txt"]
13
- s.files = ["History.txt", "License.txt", "Manifest.txt", "PostInstall.txt", "README.txt", "Rakefile", "TODO.txt", "config/hoe.rb", "config/requirements.rb", "examples/amazon.rb", "examples/post.rb", "examples/twitter.rb", "happymapper.gemspec", "lib/happymapper.rb", "lib/happymapper/attribute.rb", "lib/happymapper/element.rb", "lib/happymapper/item.rb", "lib/happymapper/version.rb", "lib/libxml_ext/libxml_helper.rb", "script/console", "script/destroy", "script/generate", "script/txt2html", "setup.rb", "spec/fixtures/pita.xml", "spec/fixtures/posts.xml", "spec/fixtures/statuses.xml", "spec/happymapper_attribute_spec.rb", "spec/happymapper_element_spec.rb", "spec/happymapper_item_spec.rb", "spec/happymapper_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/deployment.rake", "tasks/environment.rake", "tasks/rspec.rake", "tasks/website.rake", "website/css/common.css", "website/index.html"]
13
+ s.files = ["History.txt", "License.txt", "Manifest.txt", "PostInstall.txt", "README.txt", "Rakefile", "TODO.txt", "config/hoe.rb", "config/requirements.rb", "examples/amazon.rb", "examples/current_weather.rb", "examples/post.rb", "examples/twitter.rb", "happymapper.gemspec", "lib/happymapper.rb", "lib/happymapper/attribute.rb", "lib/happymapper/element.rb", "lib/happymapper/item.rb", "lib/happymapper/version.rb", "lib/libxml_ext/libxml_helper.rb", "script/console", "script/destroy", "script/generate", "script/txt2html", "setup.rb", "spec/fixtures/current_weather.xml", "spec/fixtures/pita.xml", "spec/fixtures/posts.xml", "spec/fixtures/statuses.xml", "spec/happymapper_attribute_spec.rb", "spec/happymapper_element_spec.rb", "spec/happymapper_item_spec.rb", "spec/happymapper_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/deployment.rake", "tasks/environment.rake", "tasks/rspec.rake", "tasks/website.rake", "website/css/common.css", "website/index.html"]
14
14
  s.has_rdoc = true
15
15
  s.homepage = %q{http://happymapper.rubyforge.org}
16
16
  s.post_install_message = %q{}
@@ -19,9 +19,11 @@ module HappyMapper
19
19
  @name = new_name.to_s
20
20
  end
21
21
 
22
- def from_xml_node(node, namespace=nil)
22
+ def from_xml_node(node, namespace = nil)
23
23
  if primitive?
24
- typecast(value_from_xml_node(node, namespace))
24
+ value_from_xml_node(node, namespace) do |value_before_type_cast|
25
+ typecast(value_before_type_cast)
26
+ end
25
27
  else
26
28
  use_default_namespace = !namespace.nil?
27
29
  type.parse(node, options.merge(:use_default_namespace => use_default_namespace))
@@ -77,9 +79,26 @@ module HappyMapper
77
79
  if element?
78
80
  depth = options[:deep] ? './/' : ''
79
81
  result = node.find_first("#{depth}#{namespace}#{tag}")
80
- result ? result.content : nil
82
+ if result
83
+ value = yield(result.content)
84
+ if options[:attributes].is_a?(Hash)
85
+ result.attributes.each do |xml_attribute|
86
+ if attribute_options = options[:attributes][xml_attribute.name.to_sym]
87
+ attribute_value = Attribute.new(xml_attribute.name.to_sym, *attribute_options).from_xml_node(result)
88
+ result.instance_eval <<-EOV
89
+ def value.#{xml_attribute.name}
90
+ #{attribute_value.inspect}
91
+ end
92
+ EOV
93
+ end
94
+ end
95
+ end
96
+ value
97
+ else
98
+ nil
99
+ end
81
100
  else
82
- node[tag]
101
+ yield(node[tag])
83
102
  end
84
103
  end
85
104
  end
@@ -2,7 +2,7 @@ module HappyMapper
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 2
5
+ TINY = 3
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -0,0 +1,89 @@
1
+ <aws:weather xmlns:aws="http://www.aws.com/aws">
2
+ <aws:api version="2.0"/>
3
+ <aws:WebURL>http://weather.weatherbug.com/IN/Carmel-weather.html?ZCode=Z5546&amp;Units=0&amp;stat=MOCAR</aws:WebURL>
4
+ <aws:ob>
5
+ <aws:ob-date>
6
+ <aws:year number="2008"/>
7
+ <aws:month number="12" text="December" abbrv="Dec"/>
8
+ <aws:day number="30" text="Tuesday" abbrv="Tue"/>
9
+ <aws:hour number="4" hour-24="16"/>
10
+ <aws:minute number="18"/>
11
+ <aws:second number="01"/>
12
+ <aws:am-pm abbrv="PM"/>
13
+ <aws:time-zone offset="-5" text="Eastern Standard Time" abbrv="EST"/>
14
+ </aws:ob-date>
15
+ <aws:requested-station-id>mocar</aws:requested-station-id>
16
+ <aws:station-id>MOCAR</aws:station-id>
17
+ <aws:station>Mohawk Trail ES</aws:station>
18
+ <aws:city-state zipcode="46033">Carmel, IN</aws:city-state>
19
+ <aws:country>USA</aws:country>
20
+ <aws:latitude>39.9711111111111</aws:latitude>
21
+ <aws:longitude>-86.0938888888889</aws:longitude>
22
+ <aws:site-url>http://www1.ccs.k12.in.us/mte/home</aws:site-url>
23
+ <aws:aux-temp units="&amp;deg;F">74</aws:aux-temp>
24
+ <aws:aux-temp-rate units="&amp;deg;F">+0.0</aws:aux-temp-rate>
25
+ <aws:current-condition icon="http://deskwx.weatherbug.com/images/Forecast/icons/cond007.gif">Sunny</aws:current-condition>
26
+ <aws:dew-point units="&amp;deg;F">35</aws:dew-point>
27
+ <aws:elevation units="ft">817</aws:elevation>
28
+ <aws:feels-like units="&amp;deg;F">51</aws:feels-like>
29
+ <aws:gust-time>
30
+ <aws:year number="0001"/>
31
+ <aws:month number="1" text="January" abbrv="Jan"/>
32
+ <aws:day number="1" text="Monday" abbrv="Mon"/>
33
+ <aws:hour number="12" hour-24="00"/>
34
+ <aws:minute number="00"/>
35
+ <aws:second number="00"/>
36
+ <aws:am-pm abbrv="AM"/>
37
+ <aws:time-zone offset="-5" text="Eastern Standard Time" abbrv="EST"/>
38
+ </aws:gust-time>
39
+ <aws:gust-direction>W</aws:gust-direction>
40
+ <aws:gust-speed units="mph">25</aws:gust-speed>
41
+ <aws:humidity units="%">53</aws:humidity>
42
+ <aws:humidity-high units="%">100.0</aws:humidity-high>
43
+ <aws:humidity-low units="%">42.5</aws:humidity-low>
44
+ <aws:humidity-rate>-5.0</aws:humidity-rate>
45
+ <aws:indoor-temp units="&amp;deg;F">75</aws:indoor-temp>
46
+ <aws:indoor-temp-rate units="&amp;deg;F">+0.0</aws:indoor-temp-rate>
47
+ <aws:light>28</aws:light>
48
+ <aws:light-rate>-1.5</aws:light-rate>
49
+ <aws:moon-phase moon-phase-img="http://api.wxbug.net/images/moonphase/mphase02.gif">-10</aws:moon-phase>
50
+ <aws:pressure units="&quot;">29.71</aws:pressure>
51
+ <aws:pressure-high units="&quot;">30.18</aws:pressure-high>
52
+ <aws:pressure-low units="&quot;">29.71</aws:pressure-low>
53
+ <aws:pressure-rate units="&quot;/h">-0.04</aws:pressure-rate>
54
+ <aws:rain-month units="&quot;">6.64</aws:rain-month>
55
+ <aws:rain-rate units="&quot;/h">0.00</aws:rain-rate>
56
+ <aws:rain-rate-max units="&quot;/h">0.00</aws:rain-rate-max>
57
+ <aws:rain-today units="&quot;">0.00</aws:rain-today>
58
+ <aws:rain-year units="&quot;">53.83</aws:rain-year>
59
+ <aws:temp units="&amp;deg;F">51.8</aws:temp>
60
+ <aws:temp-high units="&amp;deg;F">52</aws:temp-high>
61
+ <aws:temp-low units="&amp;deg;F">29</aws:temp-low>
62
+ <aws:temp-rate units="&amp;deg;F/h">+2.5</aws:temp-rate>
63
+ <aws:sunrise>
64
+ <aws:year number="2008"/>
65
+ <aws:month number="12" text="December" abbrv="Dec"/>
66
+ <aws:day number="30" text="Tuesday" abbrv="Tue"/>
67
+ <aws:hour number="8" hour-24="08"/>
68
+ <aws:minute number="06"/>
69
+ <aws:second number="02"/>
70
+ <aws:am-pm abbrv="AM"/>
71
+ <aws:time-zone offset="-5" text="Eastern Standard Time" abbrv="EST"/>
72
+ </aws:sunrise>
73
+ <aws:sunset>
74
+ <aws:year number="2008"/>
75
+ <aws:month number="12" text="December" abbrv="Dec"/>
76
+ <aws:day number="30" text="Tuesday" abbrv="Tue"/>
77
+ <aws:hour number="5" hour-24="17"/>
78
+ <aws:minute number="28"/>
79
+ <aws:second number="53"/>
80
+ <aws:am-pm abbrv="PM"/>
81
+ <aws:time-zone offset="-5" text="Eastern Standard Time" abbrv="EST"/>
82
+ </aws:sunset>
83
+ <aws:wet-bulb units="&amp;deg;F">44.24</aws:wet-bulb>
84
+ <aws:wind-speed units="mph">4</aws:wind-speed>
85
+ <aws:wind-speed-avg units="mph">7</aws:wind-speed-avg>
86
+ <aws:wind-direction>SSW</aws:wind-direction>
87
+ <aws:wind-direction-avg>SW</aws:wind-direction-avg>
88
+ </aws:ob>
89
+ </aws:weather>
@@ -40,6 +40,15 @@ class Status
40
40
  has_one :user, User
41
41
  end
42
42
 
43
+ class CurrentWeather
44
+ include HappyMapper
45
+ tag 'aws:ob'
46
+ element :temperature, Integer, :tag => 'aws:temp'
47
+ element :feels_like, Integer, :tag => 'aws:feels-like'
48
+ element :current_condition, String, :tag => 'aws:current-condition', :attributes => {:icon => String}
49
+ end
50
+
51
+
43
52
  module PITA
44
53
  class Item
45
54
  include HappyMapper
@@ -213,4 +222,19 @@ describe HappyMapper do
213
222
  second.manufacturer.should == 'Wrox'
214
223
  end
215
224
  end
225
+
226
+ describe "#parse (with xml that has attributes of elements)" do
227
+ before do
228
+ file_contents = File.read(File.dirname(__FILE__) + '/fixtures/current_weather.xml')
229
+ @items = CurrentWeather.parse(file_contents)
230
+ end
231
+
232
+ it "should properly create objects" do
233
+ @first = @items[0]
234
+ @first.temperature.should == 51
235
+ @first.feels_like.should == 51
236
+ @first.current_condition.should == 'Sunny'
237
+ @first.current_condition.icon.should == 'http://deskwx.weatherbug.com/images/Forecast/icons/cond007.gif'
238
+ end
239
+ end
216
240
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: happymapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-12-12 00:00:00 -05:00
12
+ date: 2009-01-04 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -47,6 +47,7 @@ files:
47
47
  - config/hoe.rb
48
48
  - config/requirements.rb
49
49
  - examples/amazon.rb
50
+ - examples/current_weather.rb
50
51
  - examples/post.rb
51
52
  - examples/twitter.rb
52
53
  - happymapper.gemspec
@@ -61,6 +62,7 @@ files:
61
62
  - script/generate
62
63
  - script/txt2html
63
64
  - setup.rb
65
+ - spec/fixtures/current_weather.xml
64
66
  - spec/fixtures/pita.xml
65
67
  - spec/fixtures/posts.xml
66
68
  - spec/fixtures/statuses.xml