Skip to content
This repository has been archived by the owner on Oct 24, 2019. It is now read-only.

Commit

Permalink
Adding support for docs.browse and docs.featured.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Watts committed Mar 16, 2010
1 parent 71f0a68 commit 63643ba
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 12 deletions.
63 changes: 55 additions & 8 deletions lib/scribddoc.rb
Expand Up @@ -26,7 +26,7 @@ module Scribd
# Aside from these two attributes, you can set other attributes that affect
# how the file is displayed on Scribd. See the API documentation online for a
# list of attributes, at
# http://www.scribd.com/publisher/api?method_name=docs.search (consult the
# http://www.scribd.com/developers/api?method_name=docs.search (consult the
# "Result explanation" section).
#
# These attributes can be accessed or changed directly
Expand Down Expand Up @@ -121,7 +121,7 @@ def save
fields[:url] = @attributes[:file]
response = API.instance.send_request 'docs.uploadFromUrl', fields
elsif uri.kind_of? URI::Generic or uri.nil? then
file_obj = is_file_object ? file : File.open(file, 'r')
file_obj = is_file_object ? file : File.open(file, 'rb')
fields[:file] = file_obj
response = API.instance.send_request 'docs.upload', fields
file_obj.close unless is_file_object
Expand Down Expand Up @@ -177,7 +177,7 @@ def self.update_all(docs, options)
# their content. You must at a minimum supply a +query+ option, with a
# string that will become the full-text search query. For a list of other
# supported options, please see the online API documentation at
# http://www.scribd.com/publisher/api?method_name=docs.search
# http://www.scribd.com/developers/api?method_name=docs.search
#
# The scope can be any value given for the +scope+ parameter in the above
# website, or <tt>:first</tt> to return the first result only (not an array
Expand All @@ -195,7 +195,7 @@ def self.update_all(docs, options)
#
# Passing in simply a numerical ID loads the document with that ID. You can
# pass additional options as defined at
# httphttp://www.scribd.com/publisher/api?method_name=docs.getSettings
# httphttp://www.scribd.com/developers/api?method_name=docs.getSettings
#
# Scribd::Document.find(108196)
#
Expand All @@ -222,19 +222,66 @@ def self.find(scope, options={})
return scope == :first ? documents.first : documents
end
end


# === Featured docs
#
# This method is called with a scope and a hash of options. For a list of
# supported options, please see the online API documentation at
# http://www.scribd.com/developers/api?method_name=docs.featured
#
# The scope can be either <tt>:first</tt> to return the first result only (not an array
# of results) or <tt>:all</tt> to return an array. Include a +scope+ option
# to control the parameter described in the API documentation.
#
# Scribd::Document.featured(:all, :scope => 'hot', :limit => 10)
#
# Documents returned from this method will have their +owner+ attributes set
# to nil.

def self.featured(scope, options = {})
response = API.instance.send_request('docs.featured', options)
documents = []
response.elements['/rsp/result_set'].elements.each do |doc|
documents << Document.new(:xml => doc)
end
scope == :first ? documents.first : documents
end

# === Browse docs
#
# This method is called with a scope and a hash of options. For a list of
# supported options, please see the online API documentation at
# http://www.scribd.com/developers/api?method_name=docs.browse
#
# The scope can be either <tt>:first</tt> to return the first result only (not an array
# of results) or <tt>:all</tt> to return an array.
#
# Scribd::Document.browse(:all, :sort => 'views', :category_id => 1, :limit => 10)
#
# Documents returned from this method will have their +owner+ attributes set
# to nil.

def self.browse(scope, options = {})
response = API.instance.send_request('docs.browse', options)
documents = []
response.elements['/rsp/result_set'].elements.each do |doc|
documents << Document.new(:xml => doc)
end
scope == :first ? documents.first : documents
end

class << self
alias_method :upload, :create
end

# Returns the conversion status of this document. When a document is
# uploaded it must be converted before it can be used. The conversion is
# non-blocking; you can query this method to determine whether the document
# is ready to be displayed.
#
# The conversion status is returned as a string. For a full list of
# conversion statuses, see the online API documentation at
# http://www.scribd.com/publisher/api?method_name=docs.getConversionStatus
# http://www.scribd.com/developers/api?method_name=docs.getConversionStatus
#
# Unlike other properties of a document, this is retrieved from the server
# every time it's queried.
Expand Down Expand Up @@ -277,7 +324,7 @@ def owner=(newuser) #:nodoc:

# Retrieves a document's download URL. You can provide a format for the
# download. Valid formats are listed at
# http://www.scribd.com/publisher/api?method_name=docs.getDownloadUrl
# http://www.scribd.com/developers/api?method_name=docs.getDownloadUrl
#
# If you do not provide a format, the link will be for the document's
# original format.
Expand Down
4 changes: 2 additions & 2 deletions lib/scribduser.rb
Expand Up @@ -18,7 +18,7 @@ module Scribd
# user = Scribd::API.instance.user
#
# For information on a user's attributes, please consult the online API
# documentation at http://www.scribd.com/publisher/api?method_name=user.login
# documentation at http://www.scribd.com/developers/api?method_name=user.login
#
# You can create a new account with the signup (a.k.a. create) method:
#
Expand Down Expand Up @@ -71,7 +71,7 @@ def save
# 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
# http://www.scribd.com/publisher/api?method_name=docs.getSettings
# http://www.scribd.com/developers/api?method_name=docs.getSettings

def documents(options = {})
response = API.instance.send_request('docs.getList', options.merge(:session_key => @attributes[:session_key]))
Expand Down
40 changes: 38 additions & 2 deletions spec/document_spec.rb
Expand Up @@ -442,9 +442,45 @@
end
end
end


describe ".featured" do
before :each do
@xml = REXML::Document.new("<rsp stat='ok'><result_set><result><access_key>abc123</access_key></result><result><access_key>abc321</access_key></result></result_set></rsp>")
end

it "should set the scope field according the options" do
Scribd::API.instance.should_receive(:send_request).with('docs.featured', hash_including(:scope => 'hot')).and_return(@xml)
Scribd::Document.featured(:all, :scope => 'hot', :limit => 10)
end

it "should return first result if :first is provided" do
Scribd::API.instance.should_receive(:send_request).with('docs.featured', {}).and_return(@xml)
docs = Scribd::Document.featured(:first)
docs.should be_kind_of(Scribd::Document)
docs.access_key.should eql('abc123')
end
end

describe ".browse" do
before :each do
@xml = REXML::Document.new("<rsp stat='ok'><result_set><result><access_key>abc123</access_key></result><result><access_key>abc321</access_key></result></result_set></rsp>")
end

it "should not pass the scope parameter in the options" do
Scribd::API.instance.should_receive(:send_request).with('docs.browse', hash_including(:sort => 'views', :category_id => 1, :limit => 10)).and_return(@xml)
Scribd::Document.browse(:all, :sort => 'views', :category_id => 1, :limit => 10)
end

it "should return first result if :first is provided" do
Scribd::API.instance.should_receive(:send_request).with('docs.browse', {}).and_return(@xml)
docs = Scribd::Document.browse(:first)
docs.should be_kind_of(Scribd::Document)
docs.access_key.should eql('abc123')
end
end

it "should have an upload synonym for the create method"

describe ".conversion_status" do
before :each do
@document = Scribd::Document.new(:xml => REXML::Document.new("<doc_id type='integer'>123</doc_id>"))
Expand Down

0 comments on commit 63643ba

Please sign in to comment.