Permalink
Browse files

Added a patch that allows even crazy namespaces to work.

  • Loading branch information...
1 parent cce41ea commit 4ada717b2e6927ee0ca57e25fc716d89397155b5 @jnunemaker committed May 18, 2009
Showing with 115 additions and 7 deletions.
  1. +8 −7 lib/happymapper.rb
  2. +61 −0 spec/fixtures/analytics.xml
  3. +46 −0 spec/happymapper_spec.rb
View
@@ -1,5 +1,4 @@
dir = File.dirname(__FILE__)
-$:.unshift(dir) unless $:.include?(dir) || $:.include?(File.expand_path(dir))
require 'date'
require 'time'
@@ -86,14 +85,16 @@ def parse(xml, options = {})
# namespace prefix registered here will propagate down
namespaces = node.namespaces
if namespaces && namespaces.default
- namespaces.default_prefix = DEFAULT_NS
+ already_assigned = namespaces.definitions.detect do |defn|
+ namespaces.default && namespaces.default.href == defn.href && defn.prefix
+ end
+ namespaces.default_prefix = DEFAULT_NS unless already_assigned
namespace ||= DEFAULT_NS
end
xpath = root ? '/' : './/'
xpath += "#{namespace}:" if namespace
xpath += tag_name
- # puts "parse: #{xpath}"
nodes = node.find(xpath)
collection = nodes.collect do |n|
@@ -108,7 +109,7 @@ def parse(xml, options = {})
obj.send("#{elem.method_name}=",
elem.from_xml_node(n, namespace))
end
-
+
obj
end
@@ -124,6 +125,6 @@ def parse(xml, options = {})
end
end
-require 'happymapper/item'
-require 'happymapper/attribute'
-require 'happymapper/element'
+require File.join(dir, 'happymapper/item')
+require File.join(dir, 'happymapper/attribute')
+require File.join(dir, 'happymapper/element')
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom"
+ xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/"
+ xmlns:dxp="http://schemas.google.com/analytics/2009">
+ <id>http://www.google.com/analytics/feeds/accounts/nunemaker@gmail.com</id>
+ <updated>2009-04-22T23:21:23.000-07:00</updated>
+ <title type="text">Profile list for nunemaker@gmail.com</title>
+ <link rel="self" type="application/atom+xml"
+ href="http://www.google.com/analytics/feeds/accounts/default"/>
+ <author>
+ <name>Google Analytics</name>
+ </author>
+ <generator version="1.0">Google Analytics</generator>
+ <openSearch:totalResults>4</openSearch:totalResults>
+ <openSearch:startIndex>1</openSearch:startIndex>
+ <openSearch:itemsPerPage>4</openSearch:itemsPerPage>
+ <entry>
+ <id>http://www.google.com/analytics/feeds/accounts/ga:47912</id>
+ <updated>2008-11-24T12:11:32.000-08:00</updated>
+ <title type="text">addictedtonew.com</title>
+ <link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
+ <dxp:tableId>ga:47912</dxp:tableId>
+ <dxp:property name="ga:accountId" value="85301"/>
+ <dxp:property name="ga:accountName" value="addictedtonew.com"/>
+ <dxp:property name="ga:profileId" value="47912"/>
+ <dxp:property name="ga:webPropertyId" value="UA-85301-1"/>
+ </entry>
+ <entry>
+ <id>http://www.google.com/analytics/feeds/accounts/ga:1897579</id>
+ <updated>2008-11-24T12:11:32.000-08:00</updated>
+ <title type="text">railstips.org</title>
+ <link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
+ <dxp:tableId>ga:1897579</dxp:tableId>
+ <dxp:property name="ga:accountId" value="85301"/>
+ <dxp:property name="ga:accountName" value="addictedtonew.com"/>
+ <dxp:property name="ga:profileId" value="1897579"/>
+ <dxp:property name="ga:webPropertyId" value="UA-85301-7"/>
+ </entry>
+ <entry>
+ <id>http://www.google.com/analytics/feeds/accounts/ga:17132454</id>
+ <updated>2009-04-22T23:21:23.000-07:00</updated>
+ <title type="text">johnnunemaker.com</title>
+ <link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
+ <dxp:tableId>ga:17132454</dxp:tableId>
+ <dxp:property name="ga:accountId" value="85301"/>
+ <dxp:property name="ga:accountName" value="addictedtonew.com"/>
+ <dxp:property name="ga:profileId" value="17132454"/>
+ <dxp:property name="ga:webPropertyId" value="UA-85301-25"/>
+ </entry>
+ <entry>
+ <id>http://www.google.com/analytics/feeds/accounts/ga:17132478</id>
+ <updated>2009-04-22T23:21:23.000-07:00</updated>
+ <title type="text">harmonyapp.com</title>
+ <link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
+ <dxp:tableId>ga:17132478</dxp:tableId>
+ <dxp:property name="ga:accountId" value="85301"/>
+ <dxp:property name="ga:accountName" value="addictedtonew.com"/>
+ <dxp:property name="ga:profileId" value="17132478"/>
+ <dxp:property name="ga:webPropertyId" value="UA-85301-26"/>
+ </entry>
+</feed>
View
@@ -2,6 +2,38 @@
require 'pp'
require 'uri'
+module Analytics
+ class Property
+ include HappyMapper
+
+ tag 'property'
+ namespace 'dxp'
+ attribute :name, String
+ attribute :value, String
+ end
+
+ class Entry
+ include HappyMapper
+
+ tag 'entry'
+ element :id, String
+ element :updated, DateTime
+ element :title, String
+ element :table_id, String, :namespace => 'dxp', :tag => 'tableId'
+ has_many :properties, Property
+ end
+
+ class Feed
+ include HappyMapper
+
+ tag 'feed'
+ element :id, String
+ element :updated, DateTime
+ element :title, String
+ has_many :entries, Entry
+ end
+end
+
class Feature
include HappyMapper
element :name, String, :tag => '.|.//text()'
@@ -505,6 +537,20 @@ module Bar; class Baz; include HappyMapper; end; end
track.tran_detail.cust_tran_id.should == '20090102-111321'
end
+ it "should be able to parse google analytics api xml" do
+ data = Analytics::Feed.parse(fixture_file('analytics.xml'))
+ data.id.should == 'http://www.google.com/analytics/feeds/accounts/nunemaker@gmail.com'
+ data.entries.size.should == 4
+
+ entry = data.entries[0]
+ entry.title.should == 'addictedtonew.com'
+ entry.properties.size.should == 4
+
+ property = entry.properties[0]
+ property.name.should == 'ga:accountId'
+ property.value.should == '85301'
+ end
+
xit "should parse family search xml" do
tree = FamilySearch::FamilyTree.parse(fixture_file('family_tree.xml'))
tree.version.should == '1.0.20071213.942'

0 comments on commit 4ada717

Please sign in to comment.