Class: GDocs4Ruby::Service

Inherits:
GData4Ruby::Service
  • Object
show all
Defined in:
lib/gdocs4ruby/service.rb

Overview

The service class is the main handler for all direct interactions with the Google Documents API. A service represents a single user account. Each user account can have multiple documents and folders.

Usage

  1. Authenticate service = Service.new service.authenticate(“[email protected]”, “password”)

  2. Get Document List documents = service.files

  3. Get Folder List folders = serivce.folders

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Service

Accepts an optional attributes hash for initialization values



47
48
49
# File 'lib/gdocs4ruby/service.rb', line 47

def initialize(attributes = {})
  super(attributes)
end

Instance Method Details

#authenticate(username, password, service = 'writely') ⇒ Object

The authenticate method passes the username and password to google servers.

If authentication succeeds, returns true, otherwise raises the AuthenticationFailed error.



53
54
55
# File 'lib/gdocs4ruby/service.rb', line 53

def authenticate(username, password, service='writely')
  super(username, password, service)
end

#filesObject

Returns an array of objects for each document in the account. Note that this method will return all documents for the account, including documents contained in subfolders.



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

def files
  contents = []
  ret = send_request(GData4Ruby::Request.new(:get, DOCUMENT_LIST_FEED))
  xml = REXML::Document.new(ret.body)
  xml.root.elements.each('entry'){}.map do |ele|
    ele = GData4Ruby::Utils::add_namespaces(ele)
    obj = BaseObject.new(self)
    obj.load(ele.to_s)
    case obj.type
      when 'document'
        doc = Document.new(self)
      when 'spreadsheet'
        doc = Spreadsheet.new(self)
      when 'presentation'
        doc = Presentation.new(self)
      else
        doc = BaseObject.new(self)
    end
    if doc
      doc.load(ele.to_s)
      contents << doc
    end
  end
  return contents
end

#foldersObject

Returns an array of Folder objects for each folder associated with the authenticated account.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gdocs4ruby/service.rb', line 64

def folders
  if not @auth_token
     raise NotAuthenticated
  end
  ret = send_request(GData4Ruby::Request.new(:get, FOLDER_LIST_FEED))
  folders = []
  REXML::Document.new(ret.body).root.elements.each("entry"){}.map do |entry|
    entry = GData4Ruby::Utils::add_namespaces(entry)
    folder = Folder.new(self)
    puts entry.to_s if debug
    folder.load("<?xml version='1.0' encoding='UTF-8'?>#{entry.to_s}")
    folders << folder
  end
  return folders
end

#reauthenticate(service = 'writely') ⇒ Object

Helper function to reauthenticate to a new Google service without having to re-set credentials.



58
59
60
# File 'lib/gdocs4ruby/service.rb', line 58

def reauthenticate(service='writely')
  authenticate(@account, @password, service)
end