Class: GData4Ruby::GDataObject
- Inherits:
-
Object
- Object
- GData4Ruby::GDataObject
- Defined in:
- lib/gdata4ruby/gdata_object.rb
Overview
The GDataObject class represents any <entry> object returned by a Google Service. Includes attributes for accessing the common elements/parameters of the object, and methods for CRUD operations.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#acl_uri ⇒ Object
readonly
The feedLink that represents the entry’s ACL feed.
-
#author_email ⇒ Object
readonly
The author/owner email.
-
#author_name ⇒ Object
readonly
The author/owner name.
-
#categories ⇒ Object
readonly
A hash of categories.
-
#content_uri ⇒ Object
readonly
The content uri for exporting the object content.
-
#debug ⇒ Object
Used for debugging.
-
#edit_uri ⇒ Object
readonly
The edit URI, for making changes to the entry.
-
#etag ⇒ Object
readonly
The current instance etag for the entry.
-
#feed_links ⇒ Object
readonly
A hash of additional feedLinks.
-
#feed_uri ⇒ Object
readonly
The entry’s feed uri, otherwise known as the Atom <id> element value.
-
#id ⇒ Object
readonly
The unique entry id, as represented by the <gd:resourceId> tag.
-
#kind ⇒ Object
readonly
The kind (type) of the object.
-
#parent_uri ⇒ Object
readonly
The parent URI, if any.
-
#published ⇒ Object
readonly
The raw date the document was published.
-
#service ⇒ Object
A Service object.
-
#title ⇒ Object
The entry title.
-
#updated ⇒ Object
readonly
The raw date the document was last updated.
Instance Method Summary collapse
-
#create ⇒ Object
Creates the object.
-
#delete ⇒ Object
Deletes the object.
-
#exists? ⇒ Boolean
Indicates whether the object exists on the Google servers, i.e.
-
#initialize(service, attributes = {}) ⇒ GDataObject
constructor
Initializes a new GDataObject.
-
#load(string) ⇒ Object
Loads data into the object.
- #log(string) ⇒ Object
-
#save ⇒ Object
Saves the object if it exsits, otherwise creates it.
-
#to_xml ⇒ Object
Creates a new string containing the XML representation of the object as a GData compliant <entry> element.
Constructor Details
#initialize(service, attributes = {}) ⇒ GDataObject
Initializes a new GDataObject. You must pass a valid Service object, and can pass an optional array of attributes to initialize values. To load data into an object, use the load method.
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/gdata4ruby/gdata_object.rb', line 87 def initialize(service, attributes = {}) @xml ||= '' @service ||= service @exists = false @title = @content_uri = @etag = @acl_uri = @edit_uri = @parent_uri = @feed_uri = @kind = nil @categories = @feed_links = [] @include_etag = true attributes.each do |key, value| if self.respond_to?("#{key}=") self.send("#{key}=", value) end end end |
Instance Attribute Details
#acl_uri ⇒ Object (readonly)
The feedLink that represents the entry’s ACL feed.
68 69 70 |
# File 'lib/gdata4ruby/gdata_object.rb', line 68 def acl_uri @acl_uri end |
#author_email ⇒ Object (readonly)
The author/owner email
43 44 45 |
# File 'lib/gdata4ruby/gdata_object.rb', line 43 def @author_email end |
#author_name ⇒ Object (readonly)
The author/owner name
40 41 42 |
# File 'lib/gdata4ruby/gdata_object.rb', line 40 def @author_name end |
#categories ⇒ Object (readonly)
A hash of categories
65 66 67 |
# File 'lib/gdata4ruby/gdata_object.rb', line 65 def categories @categories end |
#content_uri ⇒ Object (readonly)
The content uri for exporting the object content
71 72 73 |
# File 'lib/gdata4ruby/gdata_object.rb', line 71 def content_uri @content_uri end |
#debug ⇒ Object
Used for debugging
77 78 79 |
# File 'lib/gdata4ruby/gdata_object.rb', line 77 def debug @debug end |
#edit_uri ⇒ Object (readonly)
The edit URI, for making changes to the entry
52 53 54 |
# File 'lib/gdata4ruby/gdata_object.rb', line 52 def edit_uri @edit_uri end |
#etag ⇒ Object (readonly)
The current instance etag for the entry
46 47 48 |
# File 'lib/gdata4ruby/gdata_object.rb', line 46 def etag @etag end |
#feed_links ⇒ Object (readonly)
A hash of additional feedLinks
55 56 57 |
# File 'lib/gdata4ruby/gdata_object.rb', line 55 def feed_links @feed_links end |
#feed_uri ⇒ Object (readonly)
The entry’s feed uri, otherwise known as the Atom <id> element value.
62 63 64 |
# File 'lib/gdata4ruby/gdata_object.rb', line 62 def feed_uri @feed_uri end |
#id ⇒ Object (readonly)
The unique entry id, as represented by the <gd:resourceId> tag. Not to be confused with the Atom <id> tag, which is accessible as the feed_uri attribute.
59 60 61 |
# File 'lib/gdata4ruby/gdata_object.rb', line 59 def id @id end |
#kind ⇒ Object (readonly)
The kind (type) of the object
74 75 76 |
# File 'lib/gdata4ruby/gdata_object.rb', line 74 def kind @kind end |
#parent_uri ⇒ Object (readonly)
The parent URI, if any
49 50 51 |
# File 'lib/gdata4ruby/gdata_object.rb', line 49 def parent_uri @parent_uri end |
#published ⇒ Object (readonly)
The raw date the document was published
34 35 36 |
# File 'lib/gdata4ruby/gdata_object.rb', line 34 def published @published end |
#service ⇒ Object
A Service object
28 29 30 |
# File 'lib/gdata4ruby/gdata_object.rb', line 28 def service @service end |
#title ⇒ Object
The entry title.
31 32 33 |
# File 'lib/gdata4ruby/gdata_object.rb', line 31 def title @title end |
#updated ⇒ Object (readonly)
The raw date the document was last updated
37 38 39 |
# File 'lib/gdata4ruby/gdata_object.rb', line 37 def updated @updated end |
Instance Method Details
#create ⇒ Object
Creates the object. This must be overridden in a subclass, as the feed url for creating new objects/entries is service dependent. In other words, each google service uses a different URI for saving new objects.
185 186 187 |
# File 'lib/gdata4ruby/gdata_object.rb', line 185 def create return false end |
#delete ⇒ Object
Deletes the object.
190 191 192 193 194 195 196 |
# File 'lib/gdata4ruby/gdata_object.rb', line 190 def delete if @exists service.send_request(Request.new(:delete, @edit_uri, nil, {"If-Match" => "*"})) end @exists = false return true end |
#exists? ⇒ Boolean
Indicates whether the object exists on the Google servers, i.e. has been created/saved.
80 81 82 |
# File 'lib/gdata4ruby/gdata_object.rb', line 80 def exists? return @exists end |
#load(string) ⇒ Object
Loads data into the object. Accepts a string containing an XML <entry> from a GData compliant feed.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/gdata4ruby/gdata_object.rb', line 104 def load(string) @exists = @include_etag = true @xml = string xml = REXML::Document.new(string) xml.root.elements.each(){}.map do |ele| @etag = xml.root.attributes['etag'] if xml.root.attributes['etag'] case ele.name when "id" @feed_uri = ele.text log("ID: #{@feed_uri}") when 'content' @content_uri = ele.attributes['src'] if ele.attributes['src'] when 'resourceId' @id = ele.text when 'title' @title = ele.text log("Title: #{@title}") when 'category' @categories << {:label => ele.attributes['label'], :scheme => ele.attributes['scheme'], :term => ele.attributes['term']} if ele.attributes['scheme'] and ele.attributes['scheme'] == 'http://schemas.google.com/g/2005#kind' @kind = if ele.attributes['label'] ele.attributes['label'] else ele.attributes['term'] end end when 'link' case ele.attributes['rel'] when 'http://schemas.google.com/docs/2007#parent' @parent_uri = ele.attributes['href'] when 'edit' @edit_uri = ele.attributes['href'] when 'http://schemas.google.com/acl/2007#accessControlList' @acl_uri = ele.attributes['href'] if not @acl_uri end when 'feedLink' @feed_links << {:rel => ele.attributes['rel'], :href => ele.attributes['href']} @acl_uri = ele.attributes['href'] if ele.attributes['rel'].include? 'accessControlList' and not @acl_uri when 'author' ele.elements.each('name'){}.map {|e| @author_name = e.text} ele.elements.each('email'){}.map {|e| @author_email = e.text} when 'published' begin @published = Time.parse(ele.text) rescue @published = nil end when 'updated' begin @updated = Time.parse(ele.text) rescue @updated = nil end end end return xml.root end |
#log(string) ⇒ Object
178 179 180 |
# File 'lib/gdata4ruby/gdata_object.rb', line 178 def log(string) puts string if debug end |
#save ⇒ Object
Saves the object if it exsits, otherwise creates it.
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/gdata4ruby/gdata_object.rb', line 165 def save if @exists ret = service.send_request(Request.new(:put, @edit_uri, to_xml)) else ret = create end if not ret or not load(ret.read_body) raise SaveFailed, 'Could not save object' end return true end |
#to_xml ⇒ Object
Creates a new string containing the XML representation of the object as a GData compliant <entry> element.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/gdata4ruby/gdata_object.rb', line 200 def to_xml xml = REXML::Document.new(@xml) xml.root.elements.each(){}.map do |ele| if @include_etag xml.root.attributes['gd:etag'] = @etag if @etag and @etag != '' else xml.root.delete_attribute('gd:etag') end case ele.name when "title" ele.text = @title end end xml.to_s end |