lib/amee/profile.rb in amee-2.4.0 vs lib/amee/profile.rb in amee-2.5.0
- old
+ new
@@ -1,8 +1,9 @@
module AMEE
module Profile
class ProfileList < Array
+ include ParseHelper
def initialize(connection, options = {})
# Load data from path
response = connection.get('/profiles', options).body
# Parse data from response
@@ -22,19 +23,21 @@
# Store in array
self << profile
end
else
# Read XML
- doc = REXML::Document.new(response)
- @pager = AMEE::Pager.from_xml(REXML::XPath.first(doc, '/Resources/ProfilesResource/Pager'))
- REXML::XPath.each(doc, '/Resources/ProfilesResource/Profiles/Profile') do |p|
+ @doc = load_xml_doc(response)
+ @pager = AMEE::Pager.from_xml(@doc.xpath('/Resources/ProfilesResource/Pager').first)
+ @doc.xpath('/Resources/ProfilesResource/Profiles/Profile').each do |p|
data = {}
- data[:uid] = p.attributes['uid'].to_s
- data[:created] = DateTime.parse(p.attributes['created'].to_s)
- data[:modified] = DateTime.parse(p.attributes['modified'].to_s)
- data[:name] = p.elements['Name'].text || data[:uid]
- data[:path] = "/#{p.elements['Path'].text || data[:uid]}"
+ data[:uid] = x '@uid', :doc => p
+ data[:created] = DateTime.parse(x "@created", :doc => p)
+ data[:modified] = DateTime.parse(x "@modified", :doc => p)
+ data[:name] = x('Name', :doc => p)
+ data[:name] = data[:uid] if data[:name].blank?
+ data[:path] = x('Path', :doc => p)
+ data[:path] = "/#{data[:uid]}" if data[:path].blank?
# Create profile
profile = Profile.new(data)
# Store connection in profile object
profile.connection = connection
# Store in array
@@ -54,10 +57,14 @@
# backwards compatibility
def self.list(connection)
ProfileList.new(connection)
end
+ def self.xmlpathpreamble
+ '/Resources/ProfilesResource/Profile/'
+ end
+
def self.create(connection)
# Create new profile
response = connection.post('/profiles', :profile => true).body
# Parse data from response
if response.is_json?
@@ -67,24 +74,25 @@
data = {}
data[:uid] = p['uid']
data[:created] = DateTime.parse(p['created'])
data[:modified] = DateTime.parse(p['modified'])
data[:name] = p['name']
- data[:path] = p['path']
+ data[:path] = "/#{p['path']}"
# Create profile
profile = Profile.new(data)
# Done
return profile
else
# Read XML
- doc = REXML::Document.new(response)
- p = REXML::XPath.first(doc, '/Resources/ProfilesResource/Profile')
+ @doc = load_xml_doc(response)
data = {}
- data[:uid] = p.attributes['uid'].to_s
- data[:created] = DateTime.parse(p.attributes['created'].to_s)
- data[:modified] = DateTime.parse(p.attributes['modified'].to_s)
- data[:name] = p.elements['Name'].text || data[:uid]
- data[:path] = p.elements['Path'].text || data[:uid]
+ data[:uid] = x '@uid'
+ data[:created] = DateTime.parse(x '@created')
+ data[:modified] = DateTime.parse(x '@modified')
+ data[:name] = x 'Name'
+ data[:name] = data[:uid] if data[:name].blank?
+ data[:path] = x 'Path'
+ data[:path] = "/#{data[:uid]}" if data[:path].blank?
# Create profile
profile = Profile.new(data)
# Store connection in profile object
profile.connection = connection
# Done