rscribd 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/resource.rb ADDED
@@ -0,0 +1,158 @@
1
+ module Scribd
2
+
3
+ # Describes a remote object that the Scribd API lets you interact with. All
4
+ # such objects are modeled after the <tt>ActiveRecord</tt> ORM approach.
5
+ #
6
+ # The Resource superclass is never directly used; you will interact with
7
+ # actual Scribd entities like Document and User, which inherit functionality
8
+ # from this superclass.
9
+ #
10
+ # Objects have one or more attributes (also called fields) that can be
11
+ # accessed directly through synonymous methods. For instance, if your resource
12
+ # has an attribute +title+, you can get and set the title like so:
13
+ #
14
+ # obj.title #=> "Title"
15
+ # obj.title = "New Title"
16
+ #
17
+ # The specific attributes that a Document or a User or any other resource has
18
+ # are not stored locally. They are downloaded remotely whenever a resource is
19
+ # loaded from the remote server. Thus, you can modify any attribute you want,
20
+ # though it may or may not have any effect:
21
+ #
22
+ # doc = Scribd::Document.find(:text => 'foo').first
23
+ # doc.self_destruct_in = 5.seconds #=> Does not produce error
24
+ # doc.save #=> Has no effect, since that attribute doesn't exist. Your document does not explode.
25
+ #
26
+ # As shown above, when you make changes to an attribute, these changes are not
27
+ # immediately reflected remotely. They are only stored locally until such time
28
+ # as save is called. When you call save, the remote object is updated to
29
+ # reflect the changes you made in its API instance.
30
+
31
+ class Resource
32
+
33
+ # Initializes instance variables.
34
+
35
+ def initialize(options={})
36
+ @saved = false
37
+ @created = false
38
+ @attributes = Hash.new
39
+ end
40
+
41
+ # Creates a new instance with the given attributes, saves it immediately,
42
+ # and returns it. You should call its created? method if you need to verify
43
+ # that the object was saved successfully.
44
+
45
+ def self.create(options={})
46
+ obj = new(options)
47
+ obj.save
48
+ obj
49
+ end
50
+
51
+ # Throws NotImplementedError by default.
52
+
53
+ def save
54
+ raise NotImplementedError, "Cannot save #{self.class.to_s} objects"
55
+ end
56
+
57
+ # Throws NotImplementedError by default.
58
+
59
+ def self.find(options)
60
+ raise NotImplementedError, "Cannot find #{self.class.to_s} objects"
61
+ end
62
+
63
+ # Throws NotImplementedError by default.
64
+
65
+ def destroy
66
+ raise NotImplementedError, "Cannot destroy #{self.class.to_s} objects"
67
+ end
68
+
69
+ # Returns true if this document's attributes have been updated remotely, and
70
+ # thus their local values reflect the remote values.
71
+
72
+ def saved?
73
+ @saved
74
+ end
75
+
76
+ # Returns true if this document has been created remotely, and corresponds
77
+ # to a document on the Scribd website.
78
+
79
+ def created?
80
+ @created
81
+ end
82
+
83
+ # Returns the value of an attribute, referenced by string or symbol, or nil
84
+ # if the attribute cannot be read.
85
+
86
+ def read_attribute(attribute)
87
+ raise ArgumentError, "Attribute must respond to to_sym" unless attribute.respond_to? :to_sym
88
+ @attributes[attribute.to_sym]
89
+ end
90
+
91
+ # Returns a map of attributes to their values, given an array of attributes,
92
+ # referenced by string or symbol. Attributes that cannot be read are
93
+ # ignored.
94
+
95
+ def read_attributes(attributes)
96
+ raise ArgumentError, "Attributes must be listed in an Enumeration" unless attributes.kind_of? Enumeration
97
+ raise ArgumentError, "All attributes must respond to to_sym" unless attributes.all? { |a| a.respond_to? :to_sym }
98
+ keys = attributes.map(&:to_sym)
99
+ values = @attributes.values_at keys
100
+ keys.zip(values).to_hsh
101
+ end
102
+
103
+ # Assigns values to attributes. Takes a hash that specifies the
104
+ # attribute-value pairs to update. Does not perform a save. Non-writeable
105
+ # attributes are ignored.
106
+
107
+ def write_attributes(values)
108
+ raise ArgumentError, "Values must be specified through a hash of attributes" unless values.kind_of? Hash
109
+ raise ArgumentError, "All attributes must respond to to_sym" unless values.keys.all? { |a| a.respond_to? :to_sym }
110
+ @attributes.update values.map { |k,v| [ k.to_sym, v ] }.to_hsh
111
+ end
112
+
113
+ # Gets or sets attributes for the resource. Any named attribute can be
114
+ # retrieved for changed through a method call, even if it doesn't exist.
115
+ # Such attributes will be ignored and purged when the document is saved:
116
+ #
117
+ # doc = Scribd::Document.new
118
+ # doc.foobar #=> Returns nil
119
+ # doc.foobar = 12
120
+ # doc.foobar #=> Returns 12
121
+ # doc.save
122
+ # doc.foobar #=> Returns nil
123
+ #
124
+ # Because of this, no Scribd resource will ever raise +NoMethodError+.
125
+
126
+ def method_missing(meth, *args)
127
+ if meth.to_s =~ /(\w+)=/ then
128
+ raise ArgumentError, "Only one parameter can be passed to attribute=" unless args.size == 1
129
+ @attributes[$1.to_sym] = args[0]
130
+ else
131
+ @attributes[meth]
132
+ end
133
+ end
134
+
135
+ # Pretty-print for debugging output of Scribd resources.
136
+
137
+ def inspect #:nodoc:
138
+ "#<#{self.class.to_s} #{@attributes.select { |k, v| not v.nil? }.collect { |k,v| k.to_s + '=' + v.to_s }.join(', ')}>"
139
+ end
140
+
141
+ private
142
+
143
+ def load_attributes(xml)
144
+ @attributes.clear
145
+ xml.each_element do |element|
146
+ @attributes[element.name.to_sym] = if element.attributes['type'] == 'integer' then
147
+ element.text.to_i
148
+ elsif element.attributes['type'] == 'float' then
149
+ element.text.to_f
150
+ elsif element.attributes['type'] == 'symbol' then
151
+ element.text.to_sym
152
+ else
153
+ element.text
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
data/lib/rscribd.rb ADDED
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ class Hash #:nodoc:
4
+ # Taken from Rails, with appreciation to DHH
5
+ def stringify_keys
6
+ inject({}) do |options, (key, value)|
7
+ options[key.to_s] = value
8
+ options
9
+ end
10
+ end unless method_defined?(:stringify_keys)
11
+ end
12
+
13
+ class Array #:nodoc:
14
+ def to_hsh
15
+ h = Hash.new
16
+ each { |k, v| h[k] = v }
17
+ h
18
+ end
19
+ end
20
+
21
+ require 'multipart'
22
+ require 'exceptions'
23
+ require 'api'
24
+ require 'resource'
25
+ require 'doc'
26
+ require 'user'
data/lib/user.rb ADDED
@@ -0,0 +1,142 @@
1
+ module Scribd
2
+
3
+ # A user of the Scribd website. API programs can use this class to log in as a
4
+ # Scribd user, create new user accounts, and get information about the current
5
+ # user.
6
+ #
7
+ # An API program begins by logging into Scribd:
8
+ #
9
+ # user = Scribd::User.login 'login', 'pass'
10
+ #
11
+ # You can now access information about this user through direct method calls:
12
+ #
13
+ # user.name #=> 'Real Name'
14
+ #
15
+ # If, at any time, you would like to retrieve the Scribd::User instance for
16
+ # the currently logged-in user, simply call:
17
+ #
18
+ # user = Scribd::API.instance.user
19
+ #
20
+ # For information on a user's attributes, please consult the online API
21
+ # documentation at http://www.scribd.com/platform/documentation?method_name=user.login&subtab=api
22
+ #
23
+ # You can create a new account with the signup (a.k.a. create) method:
24
+ #
25
+ # user = Scribd::User.signup :username => 'testuser', :password => 'testpassword', :email => your@email.com
26
+
27
+ class User < Resource
28
+
29
+ # Creates a new, unsaved user with the given attributes. You can eventually
30
+ # use this record to create a new Scribd account.
31
+
32
+ def initialize(options={})
33
+ super
34
+ if options[:xml] then
35
+ load_attributes(options[:xml])
36
+ @saved = true
37
+ @created = true
38
+ else
39
+ @attributes = options
40
+ end
41
+ end
42
+
43
+ # For new, unsaved records, creates a new Scribd user with the provided
44
+ # attributes, then logs in as that user. Currently modification of existing
45
+ # Scribd users is not supported. Throws a ResponseError if a remote error
46
+ # occurs.
47
+
48
+ def save
49
+ if not created? then
50
+ response = API.instance.send_request('user.signup', @attributes)
51
+ xml = response.get_elements('/rsp')[0]
52
+ load_attributes(xml)
53
+ API.instance.user = self
54
+ else
55
+ raise NotImplementedError, "Cannot update a user once that user's been saved"
56
+ end
57
+ end
58
+
59
+ # Returns a list of all documents owned by this user. This list is _not_
60
+ # backed by the server, so if you add or remove items from it, it will not
61
+ # make those changes server-side. This also has some tricky consequences
62
+ # when modifying a list of documents while iterating over it:
63
+ #
64
+ # docs = user.documents
65
+ # docs.each(&:destroy)
66
+ # docs #=> Still populated, because it hasn't been updated
67
+ # docs = user.documents #=> Now it's empty
68
+ #
69
+ # Scribd::Document instances returned through this method have more
70
+ # attributes than those returned by the Scribd::Document.find method. The
71
+ # additional attributes are documented online at
72
+ # http://www.scribd.com/platform/documentation?method_name=docs.getSettings&subtab=api
73
+
74
+ def documents
75
+ response = API.instance.send_request('docs.getList', { :session_key => @attributes[:session_key] })
76
+ documents = Array.new
77
+ response.elements['/rsp/resultset'].elements.each do |doc|
78
+ documents << Document.new(:xml => doc, :owner => self)
79
+ end
80
+ return documents
81
+ end
82
+
83
+ # Finds documents owned by this user matching a given query. The parameters
84
+ # provided to this method are identical to those provided to
85
+ # Scribd::Document.find.
86
+
87
+ def find_documents(options)
88
+ Document.find options.merge(:scope => 'user', :session_key => @attributes[:session_key])
89
+ end
90
+
91
+ # Loads a Scribd::Document by ID. You can only load such documents if they
92
+ # belong to this user.
93
+
94
+ def find_document(document_id)
95
+ response = API.instance.send_request('docs.getSettings', { :doc_id => document_id, :session_key => @attributes[:session_key] })
96
+ Document.new :xml => response.elements['/rsp'], :owner => self
97
+ end
98
+
99
+ # Uploads a document to a user's document list. This method takes the
100
+ # following options:
101
+ #
102
+ # +file+:: The location of a file on disk or the URL to a file on the Web
103
+ # +type+:: The file's type (e.g., "txt" or "ppt"). Optional if the file has
104
+ # an extension (like "file.txt").
105
+ #
106
+ # There are other options you can specify. For more information, see the
107
+ # Scribd::Document.save method.
108
+
109
+ def upload(options)
110
+ Document.create options.merge(:owner => self)
111
+ end
112
+
113
+ # Synonym for create.
114
+
115
+ def self.signup(options={})
116
+ create options
117
+ end
118
+
119
+ # Logs into Scribd using the given username and password. This user will be
120
+ # used for all subsequent Scribd API calls. You must log in before you can
121
+ # use protected API functions. Returns the Scribd::User instance for the
122
+ # logged in user.
123
+
124
+ def self.login(username, password)
125
+ response = API.instance.send_request('user.login', { :username => username, :password => password })
126
+ xml = response.get_elements('/rsp')[0]
127
+ user = User.new(:xml => xml)
128
+ API.instance.user = user
129
+ return user
130
+ end
131
+
132
+ # Returns the +user_id+ attribute.
133
+
134
+ def id
135
+ self.user_id
136
+ end
137
+
138
+ def to_s #:nodoc:
139
+ @attributes[:username]
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,47 @@
1
+ # Example 1 - Uploading a test text file and removing it afterwards.
2
+
3
+ require 'rubygems'
4
+ require 'rscribd'
5
+
6
+ # Use your API key / secret here
7
+ api_key = ''
8
+ api_secret = ''
9
+
10
+ # Create a scribd object
11
+ Scribd::API.instance.key = api_key
12
+ Scribd::API.instance.secret = api_secret
13
+ #Scribd::API.instance.debug = true
14
+
15
+ begin
16
+ Scribd::User.login 'LOGIN', 'PASSWORD'
17
+ # Upload the document from a file
18
+ print "Uploading a document ... "
19
+
20
+ doc = Scribd::Document.upload(:file => 'test.txt')
21
+ puts "Done doc_id=#{doc.id}, doc_access_key=#{doc.access_key}"
22
+
23
+ # Poll API until conversion is complete
24
+ while (doc.conversion_status == 'PROCESSING')
25
+ puts "Document conversion is processing"
26
+ sleep(2) # Very important to sleep to prevent a runaway loop that will get your app blocked
27
+ end
28
+ puts "Document conversion is complete"
29
+
30
+ # Edit various options of the document
31
+ # Note you can also edit options before your doc is done converting
32
+ doc.title = 'This is a test doc!'
33
+ doc.description = "I'm testing out the Scribd API!"
34
+ doc.access = 'private'
35
+ doc.language = 'en'
36
+ doc.license = 'c'
37
+ doc.tags = 'test,api'
38
+ doc.show_ads = true
39
+ doc.save
40
+
41
+ # Delete the uploaded document
42
+ print "Deleting a document ... "
43
+ doc.destroy
44
+ puts "Done doc_id=#{doc.id}"
45
+ rescue Scribd::ResponseError => e
46
+ puts "failed code=#{e.code} error='#{e.message}'"
47
+ end
data/sample/02_user.rb ADDED
@@ -0,0 +1,44 @@
1
+ # Example 2 - Signing in as a user, and accessing a user's files.
2
+
3
+ require 'rubygems'
4
+ require 'rscribd'
5
+
6
+ # Use your API key / secret here
7
+ api_key = ''
8
+ api_secret = ''
9
+
10
+ # Edit these to a real Scribd username/password pair
11
+ username = ''
12
+ password = ''
13
+
14
+ # Create a scribd object
15
+ Scribd::API.instance.key = api_key
16
+ Scribd::API.instance.secret = api_secret
17
+ #Scribd::API.instance.debug = true
18
+
19
+ begin
20
+
21
+ # Login your Scribd API object as a particular user
22
+ # NOTE: Edit this to the username and password of a real Scribd user
23
+ user = Scribd::User.login username, password
24
+
25
+ docs = user.documents
26
+
27
+ puts "User #{user.username} has #{docs.size} docs"
28
+ if docs.size > 0
29
+ puts "User's docs:"
30
+ for doc in docs
31
+ puts "#{doc.title}"
32
+ end
33
+ end
34
+
35
+ results = Scribd::Document.find(:query => 'checklist') # Search over the user's docs for the string 'cat'
36
+ puts "Search through docs turned up #{results.size} results:"
37
+ for doc in results
38
+ puts "#{doc.title}"
39
+ end
40
+
41
+
42
+ rescue Scribd::ResponseError => e
43
+ puts "failed code=#{e.code} error='#{e.message}'"
44
+ end
data/sample/test.txt CHANGED
@@ -1 +1 @@
1
- Test text file.
1
+ This is a simple test text file to try out the neat Scribd API.
metadata CHANGED
@@ -1,64 +1,87 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
3
- specification_version: 1
4
2
  name: rscribd
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.0.2
7
- date: 2007-11-20 00:00:00 -08:00
8
- summary: Ruby interface to the Scribd API
9
- require_paths:
10
- - lib
11
- email: matt.jaynes@gmail.com
12
- homepage: http://rubyforge.org/projects/rscribd/
13
- rubyforge_project: rscribd
14
- description: rscribd is a Ruby implementation of the Scribd API. It includes a faithful reproduction of the published API as well as method encapsulation to provide more useful object mappings. rscribd features result caching to improve performance.
15
- autorequire: rake
16
- default_executable:
4
+ version: 0.0.3
5
+ platform: ""
6
+ authors:
7
+ - Jared Friedman
8
+ autorequire:
17
9
  bindir: bin
18
- has_rdoc: false
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
25
- platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
- authors: []
30
-
31
- files:
32
- - lib/scribd/base.rb
33
- - lib/scribd/docs.rb
34
- - lib/scribd/search.rb
35
- - lib/scribd.rb
36
- - sample/01-docs_upload-text.rb
37
- - sample/02-docs_upload-binary.rb
38
- - sample/03-docs_uploadFromUrl.rb
39
- - sample/04-docs_changeSettings.rb
40
- - sample/05-docs_getConversionStatus.rb
41
- - sample/test.gif
42
- - sample/test.txt
43
- test_files: []
44
-
45
- rdoc_options: []
46
-
47
- extra_rdoc_files: []
48
-
49
- executables: []
50
-
51
- extensions: []
52
-
53
- requirements: []
10
+ cert_chain: []
54
11
 
12
+ date: 2008-02-18 00:00:00 -08:00
13
+ default_executable:
55
14
  dependencies:
56
15
  - !ruby/object:Gem::Dependency
57
16
  name: mime-types
58
17
  version_requirement:
59
- version_requirements: !ruby/object:Gem::Version::Requirement
18
+ version_requirements: !ruby/object:Gem::Requirement
60
19
  requirements:
61
20
  - - ">"
62
21
  - !ruby/object:Gem::Version
63
22
  version: 0.0.0
64
23
  version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: hoe
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 1.5.0
32
+ version:
33
+ description: "== DESCRIPTION: This gem provides a simple and powerful library for the Scribd API, allowing you to write Ruby applications or Ruby on Rails websites that upload, convert, display, search, and control documents in many formats. == FEATURES/PROBLEMS: * Upload your documents to Scribd's servers and access them using the gem * Upload local files or from remote web sites * Search, tag, and organize documents * Associate documents with your users' accounts"
34
+ email: api@scribd.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - History.txt
41
+ - Manifest.txt
42
+ - README.txt
43
+ - sample/test.txt
44
+ files:
45
+ - History.txt
46
+ - Manifest.txt
47
+ - README.txt
48
+ - Rakefile
49
+ - lib/api.rb
50
+ - lib/doc.rb
51
+ - lib/exceptions.rb
52
+ - lib/multipart.rb
53
+ - lib/resource.rb
54
+ - lib/rscribd.rb
55
+ - lib/user.rb
56
+ - sample/01_upload.rb
57
+ - sample/02_user.rb
58
+ - sample/test.txt
59
+ has_rdoc: true
60
+ homepage:
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --main
64
+ - README.txt
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ requirements: []
80
+
81
+ rubyforge_project: rscribd
82
+ rubygems_version: 0.9.5
83
+ signing_key:
84
+ specification_version: 2
85
+ summary: Ruby client library for the Scribd API
86
+ test_files: []
87
+