lib/google_apps/atom/nickname.rb in google_apps-0.4.8.1 vs lib/google_apps/atom/nickname.rb in google_apps-0.4.9
- old
+ new
@@ -8,22 +8,23 @@
ELEMENTS = { nick: ['apps:nickname', 'name'], user: ['apps:login', 'userName'] }
def initialize
@document = Atom::XML::Document.new
- @document.root = header
- @document.root << category
+ @document.root = build_root
end
# nickname= sets the nickname value on the object and in the
# underlying XML document. It takes a string as an argument.
#
# nickname = 'Timmy'
#
# nickname= returns the new nickname value
def nickname=(nick)
- @nickname.nil? ? set_nickname(nick) : change_nickname(nick)
+ @nickname ? find_and_update(@document, '//apps:nickname', name: [@nickname, nick]) : create('nickname', nick)
+
+ @nickname = nick
end
# user= sets the username value on the object and in the
# underlying XML document. It takes a string (default/current username)
@@ -31,11 +32,13 @@
#
# user = 'tom'
#
# user= returns the new username value
def user=(username)
- @user.nil? ? set_user(username) : change_user(username)
+ @user ? find_and_update(@document, '//apps:login', userName: [@user, username]) : create('login', username)
+
+ @user = username
end
# to_s returns the underlying XML document as a string.
def to_s
@@ -45,97 +48,24 @@
private
- # header returns an atom:entry node with the appropriate
- # namespaces for a GoogleApps nickname document
- def header
- add_namespaces create_node(type: 'atom:entry'), atom: 'http://www.w3.org/2005/Atom', apps: 'http://schemas.google.com/apps/2006'
- end
-
-
- # category constructs an atom:category node with the
- # appropriate attributes for a GoogleApps nickname
- # document.
- def category
- create_node type: 'atom:category', attrs: Atom::CATEGORY[:nickname]
- end
-
-
- # set_nickname adds an apps:nickname node to the
- # underlying XML document and sets @nickname.
- # It takes a nickname in string form for its
- # argument.
+ # create adds the specified node to @document. It takes
+ # a type and a value as arguments.
#
- # set_nickname 'Timmy'
+ # create 'nickname', 'Bob'
#
- # set_nickname returns the new nickname value.
- def set_nickname(nick)
- @document.root << create_node(type: 'apps:nickname', attrs: [['name', nick]])
-
- @nickname = nick
- end
-
-
- # set_user adds an apps:login node to the underlying
- # XML document and sets @user. It takes a username
- # (current/default username) in string form for its
- # argument.
- #
- # set_user 'bob'
- #
- # set_user returns the new user value.
- def set_user(username)
- @document.root << create_node(type: 'apps:login', attrs: [['userName', username]])
-
- @user = username
- end
-
-
- # change_nickname changes the name attribute for the
- # apps:nickname node in the underlying XML document.
- # It takes a nickname in string form.
- #
- # change_nickname 'Timmy'
- #
- # change_nickname returns the new nickname.
- def change_nickname(nick)
- @document.root.each do |node|
- node.attributes['name'] = nick if node.attributes['name'] == @nickname
+ # create returns a parsed copy of the document.
+ def create(type, value)
+ case type
+ when 'nickname'
+ @document.root << create_node(type: 'apps:nickname', attrs: [['name', value]])
+ when 'login'
+ @document.root << create_node(type: 'apps:login', attrs: [['userName', value]])
end
- @nickname = nick
- end
-
-
- # change_user changes the userName attribute for the
- # apps:login node in the underlying XML document. It
- # takes a username (Email format) in string form.
- #
- # change_user 'bob@work.com'
- #
- # change_user returns the new user value.
- def change_user(username)
- @document.root.each do |node|
- node.attributes['userName'] = username if node.attributes['userName'] == @user
- end
-
- @user = username
- end
-
- # :nodoc:
- def change_element(type, value)
- @document.root.each do |node|
- node.attributes[ELEMENTS[type][1]] = value
- end
- end
-
-
- # parse_document takes an XML document and returns
- # a parsed copy of that document.
- def parse_document(document = @document)
- Atom::XML::Parser.document(document).parse
+ @document = parse @document
end
end
end
end
\ No newline at end of file