From 63643baa99c3952773153b26def4bab045e6d218 Mon Sep 17 00:00:00 2001 From: Mike Watts Date: Tue, 16 Mar 2010 12:05:55 -0700 Subject: [PATCH] Adding support for docs.browse and docs.featured. --- lib/scribddoc.rb | 63 +++++++++++++++++++++++++++++++++++++------ lib/scribduser.rb | 4 +-- spec/document_spec.rb | 40 +++++++++++++++++++++++++-- 3 files changed, 95 insertions(+), 12 deletions(-) diff --git a/lib/scribddoc.rb b/lib/scribddoc.rb index 120bc9d..ab7464d 100644 --- a/lib/scribddoc.rb +++ b/lib/scribddoc.rb @@ -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 @@ -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 @@ -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 :first to return the first result only (not an array @@ -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) # @@ -222,11 +222,58 @@ 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 :first to return the first result only (not an array + # of results) or :all 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 :first to return the first result only (not an array + # of results) or :all 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 @@ -234,7 +281,7 @@ class << self # # 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. @@ -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. diff --git a/lib/scribduser.rb b/lib/scribduser.rb index da4f4b8..8e8a26f 100644 --- a/lib/scribduser.rb +++ b/lib/scribduser.rb @@ -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: # @@ -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])) diff --git a/spec/document_spec.rb b/spec/document_spec.rb index f70c325..1d003ca 100644 --- a/spec/document_spec.rb +++ b/spec/document_spec.rb @@ -442,9 +442,45 @@ end end end - + + describe ".featured" do + before :each do + @xml = REXML::Document.new("abc123abc321") + 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("abc123abc321") + 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("123"))