Class: Scribd::User
Overview
A user of the Scribd website. API programs can use this class to log in as a Scribd user, create new user accounts, and get information about the current user.
An API program begins by logging into Scribd:
user = Scribd::User.login 'login', 'pass'
You can now access information about this user through direct method calls:
user.name #=> 'Real Name'
If, at any time, you would like to retrieve the Scribd::User instance for the currently logged-in user, simply call:
user = Scribd::API.instance.user
For information on a user’s attributes, please consult the online API documentation at www.scribd.com/publisher/api?method_name=user.login
You can create a new account with the signup (a.k.a. create) method:
user = Scribd::User.signup :username => 'testuser', :password => 'testpassword', :email => [email protected]
Class Method Summary collapse
-
.login(username, password) ⇒ Object
Logs into Scribd using the given username and password.
Instance Method Summary collapse
-
#documents ⇒ Object
Returns a list of all documents owned by this user.
-
#find_document(document_id) ⇒ Object
Loads a Scribd::Document by ID.
-
#find_documents(options) ⇒ Object
Finds documents owned by this user matching a given query.
-
#id ⇒ Object
Returns the
user_id
attribute. -
#initialize(options = {}) ⇒ User
constructor
Creates a new, unsaved user with the given attributes.
-
#save ⇒ Object
For new, unsaved records, creates a new Scribd user with the provided attributes, then logs in as that user.
-
#to_s ⇒ Object
:nodoc:.
-
#upload(options) ⇒ Object
Uploads a document to a user’s document list.
Methods inherited from Resource
create, #created?, #destroy, find, #inspect, #method_missing, #read_attribute, #read_attributes, #saved?, #write_attributes
Constructor Details
#initialize(options = {}) ⇒ User
Creates a new, unsaved user with the given attributes. You can eventually use this record to create a new Scribd account.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/scribduser.rb', line 32 def initialize(={}) super if [:xml] then load_attributes([:xml]) @saved = true @created = true else @attributes = end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Scribd::Resource
Class Method Details
.login(username, password) ⇒ Object
Logs into Scribd using the given username and password. This user will be used for all subsequent Scribd API calls. You must log in before you can use protected API functions. Returns the Scribd::User instance for the logged in user.
125 126 127 128 129 130 131 |
# File 'lib/scribduser.rb', line 125 def self.login(username, password) response = API.instance.send_request('user.login', { :username => username, :password => password }) xml = response.get_elements('/rsp')[0] user = User.new(:xml => xml) API.instance.user = user return user end |
Instance Method Details
#documents ⇒ Object
Returns a list of all documents owned by this user. This list is not backed by the server, so if you add or remove items from it, it will not make those changes server-side. This also has some tricky consequences when modifying a list of documents while iterating over it:
docs = user.documents
docs.each(&:destroy)
docs #=> Still populated, because it hasn't been updated
docs = user.documents #=> Now it's empty
Scribd::Document instances returned through this method have more attributes than those returned by the Scribd::Document.find method. The additional attributes are documented online at www.scribd.com/publisher/api?method_name=docs.getSettings
74 75 76 77 78 79 80 81 |
# File 'lib/scribduser.rb', line 74 def documents response = API.instance.send_request('docs.getList', { :session_key => @attributes[:session_key] }) documents = Array.new response.elements['/rsp/resultset'].elements.each do |doc| documents << Document.new(:xml => doc, :owner => self) end return documents end |
#find_document(document_id) ⇒ Object
Loads a Scribd::Document by ID. You can only load such documents if they belong to this user.
95 96 97 98 99 |
# File 'lib/scribduser.rb', line 95 def find_document(document_id) return nil unless @attributes[:session_key] response = API.instance.send_request('docs.getSettings', { :doc_id => document_id, :session_key => @attributes[:session_key] }) Document.new :xml => response.elements['/rsp'], :owner => self end |
#find_documents(options) ⇒ Object
Finds documents owned by this user matching a given query. The parameters provided to this method are identical to those provided to Scribd::Document.find.
87 88 89 90 |
# File 'lib/scribduser.rb', line 87 def find_documents() return nil unless @attributes[:session_key] Document.find .merge(:scope => 'user', :session_key => @attributes[:session_key]) end |
#id ⇒ Object
Returns the user_id
attribute.
135 136 137 |
# File 'lib/scribduser.rb', line 135 def id self.user_id end |
#save ⇒ Object
For new, unsaved records, creates a new Scribd user with the provided attributes, then logs in as that user. Currently modification of existing Scribd users is not supported. Throws a ResponseError if a remote error occurs.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/scribduser.rb', line 48 def save if not created? then response = API.instance.send_request('user.signup', @attributes) xml = response.get_elements('/rsp')[0] load_attributes(xml) API.instance.user = self else raise NotImplementedError, "Cannot update a user once that user's been saved" end end |
#to_s ⇒ Object
:nodoc:
139 140 141 |
# File 'lib/scribduser.rb', line 139 def to_s #:nodoc: @attributes[:username] end |
#upload(options) ⇒ Object
Uploads a document to a user’s document list. This method takes the following options:
file
-
The location of a file on disk or the URL to a file on the Web
type
-
The file’s type (e.g., “txt” or “ppt”). Optional if the file has an extension (like “file.txt”).
There are other options you can specify. For more information, see the Scribd::Document.save method.
111 112 113 114 |
# File 'lib/scribduser.rb', line 111 def upload() raise NotReadyError, "User hasn't been created yet" unless created? Document.create .merge(:owner => self) end |