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:
<pre>user = Scribd::User.login 'login', 'pass'</pre>
You can now access information about this user through direct method calls:
<pre>user.name #=> 'Real Name'</pre>
If, at any time, you would like to retrieve the User instance for the currently logged-in user, simply call:
<pre>user = Scribd::API.instance.user</pre>
For information on a user’s attributes, please consult the online API documentation.
You can create a new account with the User.signup (a.k.a. Resource.create) method:
<pre>user = Scribd::User.signup :username => 'testuser', :password => 'testpassword', :email => [email protected]</pre>
Class Method Summary collapse
-
.login(username, password) ⇒ Scribd::User
Logs into Scribd using the given username and password.
Instance Method Summary collapse
-
#auto_sign_in_url(next_url = "") ⇒ String
Returns a URL that, when visited, will automatically sign in this user and then redirect to the provided URL.
-
#collections(options = {}) ⇒ Array<Scribd::Collection>
Returns the collections this user has created.
-
#documents(options = {}) ⇒ Array<Scribd::Document>
Returns a list of documents owned by this user.
-
#find_document(document_id) ⇒ Scribd::Document?
Loads a Document by ID.
-
#find_documents(options = {}) ⇒ Object
Finds documents owned by this user matching a given query.
-
#id ⇒ String
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
-
#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?, #scribd_id, #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.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/scribd/user.rb', line 34 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) ⇒ Scribd::User
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.
174 175 176 177 178 179 180 |
# File 'lib/scribd/user.rb', line 174 def self.login(username, password) response = API.instance.send_request('user.login', { :username => username, :password => password }) xml = response.get_elements('/rsp').first user = User.new(:xml => xml) API.instance.user = user return user end |
Instance Method Details
#auto_sign_in_url(next_url = "") ⇒ String
Returns a URL that, when visited, will automatically sign in this user and then redirect to the provided URL.
default the user is redirected to the home page.
156 157 158 159 160 |
# File 'lib/scribd/user.rb', line 156 def auto_sign_in_url(next_url="") raise NotReadyError, "User hasn't been created yet" unless created? response = API.instance.send_request('user.getAutoSignInUrl', :session_key => @attributes[:session_key], :next_url => next_url) return response.get_elements('/rsp/url').first.cdatas.first.to_s end |
#collections(options = {}) ⇒ Array<Scribd::Collection>
Returns the collections this user has created. For information about search options, see the online API documentation. The list of collections is not memoized or cached locally.
method.
138 139 140 141 142 143 144 145 146 |
# File 'lib/scribd/user.rb', line 138 def collections(={}) raise NotReadyError, "User hasn't been created yet" unless created? response = API.instance.send_request('docs.getCollections', .merge(:session_key => @attributes[:session_key])) collections = Array.new response.elements['/rsp/resultset'].elements.each do |coll| collections << Collection.new(:xml => coll, :owner => self) end return collections end |
#documents(options = {}) ⇒ Array<Scribd::Document>
Returns a list of documents owned by this user. By default, the size of the returned list is capped at 1,000. Use the @:limit@ and @:offset@ parameters to page through this user’s documents; however, @:limit@ cannot be greater than 1,000. 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:
<pre> docs = user.documents docs.each(&:destroy) docs #=> Still populated, because it hasn't been updated docs = user.documents #=> Now it's empty </pre>
Document instances returned through this method have more attributes than those returned by the Document.find method. The additional attributes are documented online.
85 86 87 88 89 90 91 92 |
# File 'lib/scribd/user.rb', line 85 def documents( = {}) response = API.instance.send_request('docs.getList', .merge(: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) ⇒ Scribd::Document?
Loads a Document by ID. You can only load such documents if they belong to this user.
112 113 114 115 116 |
# File 'lib/scribd/user.rb', line 112 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 Resource.find.
100 101 102 103 |
# File 'lib/scribd/user.rb', line 100 def find_documents(={}) return nil unless @attributes[:session_key] Document.find .merge(:scope => 'user', :session_key => @attributes[:session_key]) end |
#id ⇒ String
184 185 186 |
# File 'lib/scribd/user.rb', line 184 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.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/scribd/user.rb', line 51 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
189 190 191 |
# File 'lib/scribd/user.rb', line 189 def to_s @attributes[:username] end |
#upload(options) ⇒ Object
Uploads a document to a user’s document list. See the Document#save method for more information on the options hash.
124 125 126 127 |
# File 'lib/scribd/user.rb', line 124 def upload() raise NotReadyError, "User hasn't been created yet" unless created? Document.create .merge(:owner => self) end |