Class: GDocs4Ruby::Folder

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

Overview

The Document class represents a Google Documents Folder.

Usage

Assumes a valid and authenticated @service object.

  1. Retrieving a list of folders @service.folders

  2. Getting a list of files in a folder @folder = @service.folders.first @folder.files

  3. Getting a list of sub folders in a folder @folder = @service.folders.first @folder.sub_folders

Constant Summary collapse

FOLDER_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#folder" label="folder"/>
  <atom:title></atom:title>
</atom:entry>'

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, #put_content, #remove_access_rule, #remove_from_folder, #save, #to_iframe, #update_access_rule

Constructor Details

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

Creates a new Folder instance. Requires a valid GData4Ruby#Service object.



42
43
44
45
# File 'lib/gdocs4ruby/folder.rb', line 42

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

Class Method Details

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

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

Raises:

  • (ArgumentError)


101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/gdocs4ruby/folder.rb', line 101

def self.find(service, query, args = {})      
  raise ArgumentError if not query.is_a? Hash and not query.is_a? String
  ret = query.is_a?(String) ? [] : nil
  service.folders.each do |f|
    if (query.is_a? Hash and ((query[:id] and f.id == query[:id]) or (query[:query] and f.title.include? query[:query])))
      return f
    end
    if (query.is_a? String and f.title.include? query)
      ret << f
    end
  end
  return ret
end

Instance Method Details

#filesObject

Returns a list of files in the folder



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gdocs4ruby/folder.rb', line 77

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

#load(string) ⇒ Object

Loads the Calendar with returned data from Google Calendar feed. Returns true if successful.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gdocs4ruby/folder.rb', line 49

def load(string)
  super(string)
  xml = REXML::Document.new(string)
  xml.root.elements.each(){}.map do |ele|
#        case ele.name
#          
#        end
  end
  
  @folder_feed = @id
  return true
end

#parentObject

A reference to the parent folder. If there is no parent folder, nil is returned.



116
117
118
119
120
121
122
123
124
# File 'lib/gdocs4ruby/folder.rb', line 116

def parent
  return nil if @parent_uri == nil
  ret = @service.send_request(GData4Ruby::Request.new(:get, @parent_uri))
  folder = nil
  puts ret.body if @service.debug
  folder = Folder.new(@service)
  folder.load(ret.body)
  return folder
end

#sub_foldersObject

Returns a list of sub folders that this folder contains.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/gdocs4ruby/folder.rb', line 63

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