Class: GDocs4Ruby::Document

Inherits:
BaseObject
  • Object
show all
Defined in:
lib/gdocs4ruby/document.rb

Overview

The Document class represents a Google Word Document. Also check out BaseObject, the superclass of Document.

Usage

All usages assume you’ve already authenticated with the service, and have a service object called

  1. Create a new Document doc = Document.new(@service) doc.title = ‘Test Document’ doc.content = ‘<h1>Test Content HTML</h1>’ doc.content_type = ‘html’ doc.save

  2. Deleting a Document doc = Document.find(@service, => @doc_id) doc.delete

  3. Finding an existing Document by id doc = Document.find(@service, => @doc_id)

  4. Full Text Query doc = Document.find(@service, ‘content text’)

or

 doc = Document.find(@service, {:query => 'content text'})
  1. Finding an Existing Document by Title doc = Document.find(@service, nil, => ‘Test Document’)

  2. Updating a Document with Content from a Local File doc = Document.find(@service, => @doc_id) doc.title = ‘New Title’ doc.local_file = ‘/path/to/some/file’ doc.save

  3. Retrieving an Export doc = Document.find(@service, => @doc_id) doc.download_to_file(‘pdf’, ‘/path/to/save/location.pdf’)

Direct Known Subclasses

Presentation, Spreadsheet

Constant Summary collapse

DOCUMENT_XML =
'<?xml version="1.0" encoding="UTF-8"?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom">
  <atom:category scheme="http://schemas.google.com/g/2005#kind"
      term="http://schemas.google.com/docs/2007#document" label="document"/>
  <atom:title>example document</atom:title>
</atom:entry>'
DOWNLOAD_TYPES =
['doc', 'html', 'odt', 'pdf', 'png', 'rtf', 'txt', 'zip']
EXPORT_URI =
'https://docs.google.com/feeds/download/documents/Export'

Constants inherited from BaseObject

BaseObject::BOUNDARY, BaseObject::ENTRY_XML, BaseObject::FEEDS, BaseObject::QUERY_FEEDS, BaseObject::TYPES, BaseObject::UPLOAD_TYPES

Instance Attribute Summary

Attributes inherited from BaseObject

#bytes_used, #folders, #html_uri, #local_file, #type, #viewed

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseObject

#access_rules, #add_access_rule, #add_to_folder, #content=, #content_type=, #create, #load, #put_content, #remove_access_rule, #remove_from_folder, #save, #to_iframe, #update_access_rule

Constructor Details

#initialize(service, attributes = {}) ⇒ Document

Creates a new Document instance. Requires a valid Service object.



68
69
70
71
# File 'lib/gdocs4ruby/document.rb', line 68

def initialize(service, attributes = {})
  super(service, attributes)
  @xml = DOCUMENT_XML
end

Class Method Details

.find(service, query, args = {}) ⇒ Object

Helper function limit queries to Documents. See BaseObject#find for syntax. Type is not required and assumed to be ‘document’.



86
87
88
# File 'lib/gdocs4ruby/document.rb', line 86

def self.find(service, query, args = {})
  super(service, query, 'document', args)
end

Instance Method Details

#download_to_file(type, location) ⇒ Object

Downloads the export retrieved through get_content to a specified local file. Parameters are:

type

must be a valid type enumerated in DOWNLOAD_TYPES

location

a valid file location for the local system



93
94
95
# File 'lib/gdocs4ruby/document.rb', line 93

def download_to_file(type, location)
  File.open(location, 'wb+') {|f| f.write(get_content(type)) }
end

#get_content(type) ⇒ Object

Retrieves an export of the Document. The parameter type must be one of the DOWNLOAD_TYPES.



74
75
76
77
78
79
80
81
82
83
# File 'lib/gdocs4ruby/document.rb', line 74

def get_content(type)
  if !@exists
    raise DocumentDoesntExist
  end
  if not DOWNLOAD_TYPES.include? type
    raise ArgumentError
  end
  ret = service.send_request(GData4Ruby::Request.new(:get, EXPORT_URI, nil, nil, {"docId" => @id,"exportFormat" => type}))
  ret.body
end