Class: Googletastic::Document

Inherits:
Base
  • Object
show all
Defined in:
lib/googletastic/document.rb

Overview

“Anonymous modules have no name to be referenced by”

If you have a class constant that's undefined

Constant Summary collapse

ID =
"https://docs.google.com/feeds/default/private/full/"
FEED_BASE =
'http://docs.google.com/feeds'
FEED =
'http://docs.google.com/feeds/documents/private/full'
BASE_URL =
"http://docs.google.com"
DOCS_DOWNLOAD =
'download/documents/Export'
CSV =
"text/csv"
TSV =
"text/tab-separated-values"
TAB =
"text/tab-separated-values"
HTML =
"text/html"
HTM =
"text/html"
DOC =
"application/msword"
DOCX =
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
ODS =
"application/x-vnd.oasis.opendocument.spreadsheet"
ODT =
"application/vnd.oasis.opendocument.text"
RTF =
"application/rtf"
SXW =
"application/vnd.sun.xml.writer"
TXT =
"text/plain"
XLS =
"application/vnd.ms-excel"
XLSX =
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
PDF =
"application/pdf"
PPT =
"application/vnd.ms-powerpoint"
PPS =
"application/vnd.ms-powerpoint"

Constants included from Mixins::Namespaces

Mixins::Namespaces::NAMESPACES

Instance Attribute Summary collapse

Attributes inherited from Base

#attachment_path, #created_at, #etag, #head, #id, #keep_raw, #raw, #response, #synced_with, #updated_at

Attributes included from Mixins::Attributes

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #to_xml

Methods included from Mixins::Pagination

included

Methods included from Mixins::Finders

included

Methods included from Mixins::Parsing

included

Methods included from Mixins::Requesting

included

Methods included from Mixins::Attributes

#attribute_names, #has_attribute?, #inspect

Methods included from Mixins::Namespaces

included

Constructor Details

This class inherits a constructor from Googletastic::Base

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



34
35
36
# File 'lib/googletastic/document.rb', line 34

def body
  @body
end

#categoriesObject

Returns the value of attribute categories.



28
29
30
# File 'lib/googletastic/document.rb', line 28

def categories
  @categories
end

#contentObject (readonly)

returns the content within the body tags for the document



42
43
44
# File 'lib/googletastic/document.rb', line 42

def content
  @content
end

#extObject

Returns the value of attribute ext.



28
29
30
# File 'lib/googletastic/document.rb', line 28

def ext
  @ext
end

#kindObject

Returns the value of attribute kind.



28
29
30
# File 'lib/googletastic/document.rb', line 28

def kind
  @kind
end

#plain_idObject

Returns the value of attribute plain_id.



28
29
30
# File 'lib/googletastic/document.rb', line 28

def plain_id
  @plain_id
end

#titleObject

Returns the value of attribute title.



28
29
30
# File 'lib/googletastic/document.rb', line 28

def title
  @title
end

Class Method Details

.build_url(options) ⇒ Object



163
164
165
166
167
168
# File 'lib/googletastic/document.rb', line 163

def build_url(options)
  base = options.has_key?(:url) ? options[:url] : self.index_url
  base.gsub!(/full$/, "expandAcl") if options.has_key?(:include) and options[:include].index(:acl)
  options[:url] = base
  super(options)
end

.client_classObject



79
80
81
# File 'lib/googletastic/document.rb', line 79

def client_class
  "DocList"
end

.download(*args) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/googletastic/document.rb', line 150

def download(*args)
  old_options = args.extract_options!
  options = {}
  options[:format] = old_options[:format] || "pdf"
  options[:url] = download_url(args.first.is_a?(String) ? args.first : old_options[:id])
  args << options
  find(*args)
end

.download_url(id) ⇒ Object

docId (camelcase)



136
137
138
# File 'lib/googletastic/document.rb', line 136

def download_url(id)
  "http://docs.google.com/feeds/download/documents/Export?docId=#{id}"
end

.edit_url(id) ⇒ Object



123
124
125
# File 'lib/googletastic/document.rb', line 123

def edit_url(id)
  "http://docs.google.com/Doc?docid=#{id}"
end

.get_clean_content(remote) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/googletastic/document.rb', line 223

def get_clean_content(remote)
  title   = remote.title
  ext     = remote.ext
  if ext == ".textile"
    # google is putting strange characters at beginning of downloaded files
    content = remote.download("txt").body.gsub(/\357\273\277/, "")
    content = RedCloth.new(content).to_html
  elsif ext == ".markdown"
    content = remote.download("txt").body.gsub(/\357\273\277/, "")
    content = BlueCloth.new(content).to_html
  elsif ext.nil? || ext.empty?
    # just use the html we have already
    content = remote.content
  else
    content = remote.download("txt").body.gsub(/\357\273\277/, "")
  end
  content
end

.get_url(id) ⇒ Object

docid (all lowercase)



115
116
117
# File 'lib/googletastic/document.rb', line 115

def get_url(id)
  "http://docs.google.com/View?docID=#{id}&revision=_latest"
end

.index_url(ids = nil) ⇒ Object



110
111
112
# File 'lib/googletastic/document.rb', line 110

def index_url(ids = nil)
  "http://docs.google.com/feeds/documents/private/full"
end

.marshall(record) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/googletastic/document.rb', line 202

def marshall(record)
  Nokogiri::XML::Builder.new { |xml| 
    xml.entry(ns_xml("atom", "exif", "openSearch")) {
      if record.id
        xml.id_ {
          xml.text get_url(record.id)
        }
      end
      record.categories.each do |category|
        xml.category(
          :scheme => "http://schemas.google.com/g/2005#kind",
          :term => "#{category}"
        )
      end
      xml.title {
        xml.text record.title
      }
    }
  }.to_xml
end

.show_url(id) ⇒ Object



119
120
121
# File 'lib/googletastic/document.rb', line 119

def show_url(id)
  "https://docs.google.com/feeds/documents/private/full/#{id}"
end

.unmarshall(xml) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/googletastic/document.rb', line 170

def unmarshall(xml)
  head    = unmarshall_head(xml)
  records = xml.xpath("//atom:entry", ns_tag("atom")).collect do |record|
    etag        = record["etag"].to_s
    kind = title = id = categories = value = nil # reset
    title       = record.xpath("atom:title", ns_tag("atom")).text
    categories  = record.xpath("atom:category", ns_tag("atom")).collect do |category|
      value = category["label"].to_s
      kind = value if !kind and valid_kind?(value)
      value
    end
    id          = record.xpath("gd:resourceId", ns_tag("gd")).text
    plain_id    = id.gsub(/#{kind}:/, "")
    created_at  = record.xpath("atom:published", ns_tag("atom")).text
    updated_at  = record.xpath("atom:updated", ns_tag("atom")).text

    Googletastic::Document.new(
      :id => id,
      :title => title,
      :categories => categories,
      :etag => etag,
      :kind => kind,
      :plain_id => plain_id,
      :ext => File.extname(title),
      :created_at => DateTime.parse(created_at),
      :updated_at => DateTime.parse(updated_at),
      :head => head
    )
  end
  records
end

.update_url(id) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/googletastic/document.rb', line 127

def update_url(id)
  if has_attachment?
    "http://docs.google.com/feeds/media/private/full/#{self.id}"
  else
    "http://docs.google.com/feeds/documents/private/full/#{self.id}"
  end
end

.upload(*args) ⇒ Object



159
160
161
# File 'lib/googletastic/document.rb', line 159

def upload(*args)
  client.post_file(upload_url, args.first, TXT)
end

.upload_url(id = nil) ⇒ Object



140
141
142
# File 'lib/googletastic/document.rb', line 140

def upload_url(id = nil)
  index_url(id)
end

.valid_kind?(value) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/googletastic/document.rb', line 106

def valid_kind?(value)
  %w(document spreadsheet folder presentation pdf form).include?(value)
end

.valid_queriesObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/googletastic/document.rb', line 83

def valid_queries
  {
    :title => "title",
    :exact_title => "title-exact",
    :opened_after => "opened-min",
    :opened_before => "opened-max",
    :edited_after => "edited-min",
    :edited_before => "edited-max",
    :owner => "owner",
    :writer => "writer",
    :reader => "reader",
    :show_folders => "showfolders",
    :show_deleted => "showdeleted",
    :optical_character_recognition => "ocr",
    :translate_to => "targetLanguage",
    :language => "sourceLanguage",
    :permanent_delete => "delete",
    :convert => "convert",
    :format => "exportFormat",
    :kind => "category"
  }.merge(super)
end

Instance Method Details

#aclObject



53
54
55
56
# File 'lib/googletastic/document.rb', line 53

def acl
  return @acl if @acl
  @acl = Googletastic::AccessRule.find(:all, :document_id => id)
end

#download(format = "pdf") ⇒ Object



67
68
69
# File 'lib/googletastic/document.rb', line 67

def download(format = "pdf")
  client.get(self.download_url + "&exportFormat=#{format}")
end

#has_access?(email) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/googletastic/document.rb', line 30

def has_access?(email)
  self.class.first(:url => update_url)
end

#new_record?Boolean

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/googletastic/document.rb', line 71

def new_record?
  return false if !self.id.nil?
  return self.class.first(:title => self.title, :exact_title => true).nil?
end

#stylesObject

returns the styles for the document, in case you need them



59
60
61
# File 'lib/googletastic/document.rb', line 59

def styles
  
end

#viewObject



63
64
65
# File 'lib/googletastic/document.rb', line 63

def view
  client.get(self.show_url)
end