Class: GDocsAPIWrapper::Client::GDocsClient

Inherits:
Object
  • Object
show all
Defined in:
lib/gdocsapi-wrapper/client/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GDocsClient

Returns a new instance of GDocsClient.



32
33
34
35
36
37
38
39
# File 'lib/gdocsapi-wrapper/client/client.rb', line 32

def initialize(options = {})
  options.each do |key, value|
    self.send("#{key}=", value)
  end

  @source ||= 'UndefinedApplication'
  @login_handler = GDocsAPIWrapper::Auth::Login.new
end

Instance Attribute Details

#login_handlerObject

Returns the value of attribute login_handler.



30
31
32
# File 'lib/gdocsapi-wrapper/client/client.rb', line 30

def 
  @login_handler
end

#sourceObject

Identifying the application which uses the GDocsAPIWrapper



29
30
31
# File 'lib/gdocsapi-wrapper/client/client.rb', line 29

def source
  @source
end

Instance Method Details

#base_headersObject



89
90
91
92
93
94
95
# File 'lib/gdocsapi-wrapper/client/client.rb', line 89

def base_headers
  headers = Hash.new
  if loggedin?
    headers['Authorization'] = 'GoogleLogin auth=' + @login_handler.token
  end
  return headers
end

#list_documents(options = {}) ⇒ Object

Get a list of documents, possible options: :type => :all, :word, :spreadsheet, :presentation and :filter => :none, :starred, :trashed

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gdocsapi-wrapper/client/client.rb', line 49

def list_documents(options = {})
  options[:type] ||= :all
  options[:filter] ||= :none
  
  # login required to call this function
  raise LoginRequiredError.new unless loggedin?
  
  request = GDocsAPIWrapper::HTTP::Request.new(GDocsAPIWrapper::Client::Service.get(:documents, :type => options[:type], :filter => options[:filter]), :method => :get, :headers => base_headers)
  response = request.send_request
  
  documents = Array.new
  doc = REXML::Document.new(response.body)
  doc.elements.each("/feed/entry") do |entry|
    documents << GDocsAPIWrapper::Models::Document.parse(entry)
  end
  
  return documents
end

#list_foldersObject

Get a list of folders

Raises:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gdocsapi-wrapper/client/client.rb', line 69

def list_folders
  # login required to call this function
  raise LoginRequiredError.new unless loggedin?
  
  request = GDocsAPIWrapper::HTTP::Request.new(GDocsAPIWrapper::Client::Service.get(:folders), :method => :get, :headers => base_headers)
  response = request.send_request
  
  folders = Array.new
  doc = REXML::Document.new(response.body)
  doc.elements.each("/feed/entry") do |entry|
    folders << GDocsAPIWrapper::Models::Folder.parse(entry)
  end
  
  return folders
end

#loggedin?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/gdocsapi-wrapper/client/client.rb', line 85

def loggedin?
  return !(@login_handler.token.nil?)
end

#login(username, password) ⇒ Object

Performs login on GDocsAPIWrapper:Auth



42
43
44
45
46
# File 'lib/gdocsapi-wrapper/client/client.rb', line 42

def (username, password)
  source = GDocsAPIWrapper::Auth::SOURCE_PREFIX + @source

  @login_handler.(username, password, source)
end