Permalink
Browse files

discovered many namespace discrepencies between tests and production,…

… so for now ignore all namespaces and simplify all xpath expressions
  • Loading branch information...
1 parent fbae273 commit 36b77e09f6324efe50ea3b4afd77897093d1ba83 @djrice djrice committed Aug 21, 2011
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
@@ -18,11 +18,18 @@ class Author
def initialize(author_element)
@name = element_text(author_element, 'name')
@email = element_text(author_element, 'email', true)
- @uri = element_text(author_element, 'uri', true)
- @icon_uri = element_text(author_element, 'mingle:icon', true)
+ @uri = element_text(author_element, 'uri', true)
+ @icon_uri = element_text(author_element, 'icon', true)
+ end
+
+ def self.from_xml_snippet(entry_xml)
+ self.new(Nokogiri::XML(entry_xml).remove_namespaces!)
end
end
-
+
end
-end
+end
+
+
+
@@ -8,8 +8,8 @@ def build(element)
{
:category => Category::CARD_TYPE_CHANGE,
:type => Category::CARD_TYPE_CHANGE,
- :old_value => build_card_type(element.at_xpath("mingle:old_value")),
- :new_value => build_card_type(element.at_xpath("mingle:new_value"))
+ :old_value => build_card_type(element.at("old_value")),
+ :new_value => build_card_type(element.at("new_value"))
}
end
@@ -19,10 +19,17 @@ def build_card_type(element)
if element["nil"] == "true"
nil
else
- {
- :url => element.at_xpath("mingle:card_type")["url"],
- :name => element.at_xpath("mingle:card_type/mingle:name").inner_text
- }
+ if element.at("card_type")
+ {
+ :url => element.at("card_type")["url"],
+ :name => element.at("card_type/name").inner_text
+ }
+ else
+ {
+ :deleted? => true,
+ :name => element.at("deleted_card_type/name").inner_text
+ }
+ end
end
end
@@ -8,8 +8,8 @@ def build(element)
{
:category => Category::NAME_CHANGE,
:type => Category::NAME_CHANGE,
- :old_value => value(element.at_xpath("mingle:old_value")),
- :new_value => value(element.at_xpath("mingle:new_value"))
+ :old_value => value(element.at("old_value")),
+ :new_value => value(element.at("new_value"))
}
end
@@ -27,7 +27,7 @@ def each
def parse_changes
changes = []
- @changes_element.xpath("mingle:change").map do |change_element|
+ @changes_element.search("change").map do |change_element|
category = Category.for_mingle_term(change_element["type"])
builder = CUSTOM_BUILDERS[category] || ChangeBuilders::NoDetailChange.new(category)
changes << builder.build(change_element)
@@ -5,7 +5,7 @@ module Feed
module ElementSupport
def element_text(parent_element, element_name, optional = false)
- element = parent_element.at(".//#{element_name}")
+ element = parent_element.at(element_name)
if optional && element.nil?
nil
else
@@ -9,6 +9,10 @@ class Entry
def initialize(entry_element)
@entry_element = entry_element
end
+
+ def self.from_xml_snippet(entry_xml)
+ self.new(Nokogiri::XML(entry_xml).remove_namespaces!)
+ end
# The raw entry XML from the Atom feed
def raw_xml
@@ -18,7 +22,7 @@ def raw_xml
# The Atom entry's id value. This is the one true identifier for the entry,
# and therefore the event.
def entry_id
- @entry_id ||= @entry_element.at('id').inner_text
+ @entry_id ||= @entry_element.at("id").inner_text
end
alias :event_id :entry_id
@@ -29,18 +33,18 @@ def title
# The time at which entry was created, i.e., the event was triggered
def updated
- @updated ||= Time.parse(@entry_element.at('updated').inner_text)
+ @updated ||= Time.parse(@entry_element.at("updated").inner_text)
end
# The user who created the entry (triggered the event), i.e., changed project data in Mingle
def author
- @author ||= Author.new(@entry_element.at('author'))
+ @author ||= Author.new(@entry_element.at("author"))
end
# The set of Atom categoies describing the entry
def categories
- @categories ||= @entry_element.search('category').map do |category_element|
- Category.new(category_element.attribute('term').text, category_element.attribute('scheme').text)
+ @categories ||= @entry_element.search("category").map do |category_element|
+ Category.new(category_element["term"], category_element["scheme"])
end
end
@@ -51,7 +55,7 @@ def categories
# Change detail is contained in nested hashes, again with keys specified by element
# names in the the above sepcified Mingle help documetion
def changes
- @changes ||= Changes.new(@entry_element.at('content').at('changes', 'xmlns' => 'http://www.thoughtworks-studios.com/ns/mingle'))
+ @changes ||= Changes.new(@entry_element.at("content/changes"))
end
# Whether the entry/event was sourced by a Mingle card
@@ -70,7 +74,7 @@ def card_number
# The version number of the card or page that was created by this event. (For now, only
# working with cards.)
def version
- @version ||= CGI.parse(URI.parse(card_version_resource_uri).query)['version'].first.to_i
+ @version ||= CGI.parse(URI.parse(card_version_resource_uri).query)["version"].first.to_i
end
# The resource URI for the card version that was created by this event. Throws error if not card event.
@@ -104,14 +108,14 @@ def parse_card_number
"link[@rel='http://www.thoughtworks-studios.com/ns/mingle#event-source'][@type='application/vnd.mingle+xml']"
)
# TODO: improve this bit of parsing :)
- card_number_element.attribute('href').text.split('/').last.split('.')[0..-2].join.to_i
+ card_number_element["href"].split('/').last.split('.')[0..-2].join.to_i
end
def parse_card_version_resource_uri
card_number_element = @entry_element.at(
"link[@rel='http://www.thoughtworks-studios.com/ns/mingle#version'][@type='application/vnd.mingle+xml']"
)
- card_number_element.attribute('href').text
+ card_number_element["href"]
end
end
@@ -14,45 +14,24 @@ def initialize(url, mingle_access)
end
def entries
- @entries ||= page_as_document.search('feed/entry').map do |entry_element|
+ @entries ||= page_as_document.search('entry').map do |entry_element|
Entry.new(entry_element)
end
end
def next
- next_url_element = page_as_document.at("feed/link[@rel='next']")
+ next_url_element = page_as_document.at("link[@rel='next']")
if next_url_element.nil?
nil
else
- Page.new(next_url_element.attribute('href').text, @mingle_access)
- end
- end
-
- def previous
- previous_url_element = page_as_document.at("feed/link[@rel='previous']")
- if previous_url_element.nil?
- nil
- else
- Page.new(previous_url_element.attribute('href').text, @mingle_access)
- end
- end
-
- def archived?
- URI.parse(url).query && url == page_as_document.at("feed/link[@rel='self']").attribute('href').text
- end
-
- def closest_archived_page
- if archived?
- self
- else
- self.next
+ Page.new(next_url_element["href"], @mingle_access)
end
end
private
def page_as_document
- @page_as_document ||= Nokogiri::XML(@mingle_access.fetch_page(@url))
+ @page_as_document ||= Nokogiri::XML(@mingle_access.fetch_page(@url)).remove_namespaces!
end
end
@@ -81,7 +81,7 @@ def write_entry_to_disk(entry, next_entry)
def current_state_entry(info_file_key)
if info_file = load_current_state[info_file_key]
- entry_for_xml(YAML.load(File.new(info_file))[:entry_xml])
+ Feed::Entry.from_xml_snippet(YAML.load(File.new(info_file))[:entry_xml])
else
nil
end
@@ -106,7 +106,7 @@ def load_last_fetched_entry
current_state = load_current_state
last_fetched_entry = if current_state[:last_fetched_entry_info_file]
last_fetched_entry_info = YAML.load(File.new(current_state[:last_fetched_entry_info_file]))
- Feed::Entry.new(Nokogiri::XML(last_fetched_entry_info[:entry_xml]).at('/entry'))
+ Feed::Entry.from_xml_snippet(last_fetched_entry_info[:entry_xml])
else
nil
end
@@ -120,10 +120,6 @@ def load_current_state
end
end
- def entry_for_xml(entry_xml)
- Feed::Entry.new(Nokogiri::XML(entry_xml).at('/entry'))
- end
-
class Entries
include Enumerable
@@ -132,16 +128,12 @@ def initialize(first_info_file, last_info_file)
@first_info_file = first_info_file
@last_info_file = last_info_file
end
-
- def entry_for_xml(entry_xml)
- Feed::Entry.new(Nokogiri::XML(entry_xml).at('/entry'))
- end
-
+
def each(&block)
current_file = @first_info_file
while current_file
current_entry_info = YAML.load(File.new(current_file))
- yield(entry_for_xml(current_entry_info[:entry_xml]))
+ yield(Feed::Entry.from_xml_snippet(current_entry_info[:entry_xml]))
break if File.expand_path(current_file) == File.expand_path(@last_info_file)
current_file = current_entry_info[:next_entry_file_path]
end
@@ -6,27 +6,28 @@ module Feed
class AuthorTest < Test::Unit::TestCase
def test_parse_attributes
- element = Nokogiri.XML(%{
- <author xmlns:mingle="http://www.thoughtworks-studios.com/ns/mingle">
+ author_xml = %{
+ <author >
<name>Sammy Soso</name>
<email>sammy@example.com</email>
<uri>https://mingle.example.com/api/v2/users/233.xml</uri>
<mingle:icon>https://mingle.example.com/user/icon/233/profile.jpg</mingle:icon>
- </author>})
-
- author = Author.new(element)
+ </author>
+ }
+ author = Author.from_xml_snippet(author_xml)
assert_equal("Sammy Soso", author.name)
assert_equal("sammy@example.com", author.email)
assert_equal("https://mingle.example.com/api/v2/users/233.xml", author.uri)
assert_equal("https://mingle.example.com/user/icon/233/profile.jpg", author.icon_uri)
end
def test_parse_attributes_when_no_optional_fields
- element = Nokogiri.XML(%{
- <author xmlns:mingle="http://www.thoughtworks-studios.com/ns/mingle">
+ author_xml = %{
+ <author>
<name>Sammy Soso</name>
- </author>})
- author = Author.new(element)
+ </author>
+ }
+ author = Author.from_xml_snippet(author_xml)
assert_equal("Sammy Soso", author.name)
assert_nil(author.email)
assert_nil(author.uri)
Oops, something went wrong.

0 comments on commit 36b77e0

Please sign in to comment.