Class: GData4Ruby::GDataObject

Inherits:
Object
  • Object
show all
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

ACL::AccessRule

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/gdata4ruby/gdata_object.rb', line 84

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|
    self.send("#{key}=", value)
  end
end

Instance Attribute Details

#acl_uriObject (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_emailObject (readonly)

The author/owner email



43
44
45
# File 'lib/gdata4ruby/gdata_object.rb', line 43

def author_email
  @author_email
end

#author_nameObject (readonly)

The author/owner name



40
41
42
# File 'lib/gdata4ruby/gdata_object.rb', line 40

def author_name
  @author_name
end

#categoriesObject (readonly)

A hash of categories



65
66
67
# File 'lib/gdata4ruby/gdata_object.rb', line 65

def categories
  @categories
end

#content_uriObject (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

#edit_uriObject (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

#etagObject (readonly)

The current instance etag for the entry



46
47
48
# File 'lib/gdata4ruby/gdata_object.rb', line 46

def etag
  @etag
end

A hash of additional feedLinks



55
56
57
# File 'lib/gdata4ruby/gdata_object.rb', line 55

def feed_links
  @feed_links
end

#feed_uriObject (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

#idObject (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

#kindObject (readonly)

The kind (type) of the object



74
75
76
# File 'lib/gdata4ruby/gdata_object.rb', line 74

def kind
  @kind
end

#parent_uriObject (readonly)

The parent URI, if any



49
50
51
# File 'lib/gdata4ruby/gdata_object.rb', line 49

def parent_uri
  @parent_uri
end

#publishedObject (readonly)

The raw date the document was published



34
35
36
# File 'lib/gdata4ruby/gdata_object.rb', line 34

def published
  @published
end

#serviceObject

A Service object



28
29
30
# File 'lib/gdata4ruby/gdata_object.rb', line 28

def service
  @service
end

#titleObject

The entry title.



31
32
33
# File 'lib/gdata4ruby/gdata_object.rb', line 31

def title
  @title
end

#updatedObject (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

#createObject

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.



166
167
168
# File 'lib/gdata4ruby/gdata_object.rb', line 166

def create
  return false
end

#deleteObject

Deletes the object.



171
172
173
174
175
176
177
# File 'lib/gdata4ruby/gdata_object.rb', line 171

def delete
  if @exists
    service.send_request(Request.new(:delete, @edit_uri, '', {"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.

Returns:

  • (Boolean)


77
78
79
# File 'lib/gdata4ruby/gdata_object.rb', line 77

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.



99
100
101
102
103
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
# File 'lib/gdata4ruby/gdata_object.rb', line 99

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"
        puts 'setting id' if service.debug
        @feed_uri = ele.text
      when 'content'
        @content_uri = ele.attributes['src'] if ele.attributes['src']
      when 'resourceId'
        @id = ele.text
      when 'title'
        @title = ele.text
      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'
        @published = Time.parse(ele.text) rescue nil
      when 'updated'
        @updated = Time.parse(ele.text) rescue nil
    end
  end
  return xml.root
end

#saveObject

Saves the object if it exsits, otherwise creates it.



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/gdata4ruby/gdata_object.rb', line 151

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)
    raise SaveFailed, 'Could not save object'
  end
  return true
end

#to_xmlObject

Creates a new string containing the XML representation of the object as a GData compliant <entry> element.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/gdata4ruby/gdata_object.rb', line 181

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