Howtos: Upload a file through Ruby

11 views
Skip to first unread message

Bala

unread,
Nov 4, 2009, 3:20:04 PM11/4/09
to Scribd Platform Talk
Experts,

I'm trying to upload a file through ruby code. i tried docs.upload
through
but it always says you have to upload through Multiform/POST.
Even i tried with http://stackoverflow.com/questions/184178/ruby-how-to-post-a-file-via-http-as-multipart-form-data
but still i have no luck,

My View Code

<form name="upload_form_scribd" enctype="multipart/form-
data" method="POST" action="/scribd_upload_data"
target="upload_target" method="post" onsubmit="new Ajax.Request('/
scribd/upload_data', {asynchronous:true, evalScripts:true,
onComplete:function(request){Element.hide
('progress2_id')},onLoading:function(request){Element.show
('progress2_id')}, parameters:Form.serialize(this)}); return false;">
<% form_remote_for(:upload_target, :url => {:action =>
'upload_data'}, :loading => "Element.show('progress2_id')", :success
=> "Element.hide('progress2_id')",:html => { :multipart => true }) do |
f| -%>
<div class="column">
<div class="label">Browse</div>
<div class="form-field-content"><input type="file"
name="file_by_browse" id="title_field" class="text-field" value=""></
div>
</div>
<div class="column">
<div class="label">Title</div>
<div class="form-field-content"><input type="text"
name="title" id="title_field" class="text-field" value=""></div>
</div>
<div class="column">
<div class="label">Description</div>
<div class="form-field-content"><input type="text"
name="description" id="description_field" class="text-field"
value=""></div>
</div>
<div class="column">
<div class="label">Notes/Tags</div>
<div class="form-field-content"><input type="text"
name="tags" id="tags_field" class="text-field" value=""></div>
</div>
<div class="column">
<div class="label">Access</div>
<div class="form-field-content">
<select name="access" class="text-field">
<option value="private">Private</option>
<option value="public">Public</option>
</select>
</div>
</div>
<div class="column">
<div class="label">Show Ads</div>
<div class="form-field-content">
<select name="show_ads" class="text-field">
<option value="true">Yes</option>
<option value="false">No</option>
</select>
</div>
</div>
<input type="submit" value="Upload" class="submit-button-scribd"/>
<input type="submit" value="Cancel" class="submit-button-scribd"/>
</form>

my controller view
begin
data, headers = Multipart::Post.prepare_query(params
[:file_by_browse])
@scribd_data = scribd_upload(:file => data,
:title => params[:title], :description => params
[:description],
:tags => params[:tags], :access => params[:access],
:show_ads => params[:showads], :session_id => session
[:scribd_id])
render :update do |page|
page.replace_html 'notify_message', @scribd_data
end
rescue Exception => e
render :update do |page|
page.replace_html 'notify_message', e.message
end
end

my module code
module ScribdApi
require 'rest_client'
require 'uri'
require 'hpricot'
require 'net/http'
require 'json/pure'
require 'open-uri'
require 'rexml/document'

BASE_API_URL = "http://api.scribd.com/api?"
API_KEY = "2tl7vmql6kn8yi4raq04o"
API_SECRET_KEY = "sec-3i7wnz8cbqdghl4plndi805mua"
PUBLISH_ID = "pub-30597062110055297727"
SCRIBD_METHOD = {"login" => "user.login", "signup" => "user.signup",
"upload" => "docs.upload", "uploadFromUrl" =>
"docs.uploadFromUrl",
"getList" => "docs.getList", "getConversionStatus" =>
"docs.getConversionStatus",
"getSettings" => "docs.getSettings", "changeSettings" =>
"docs.changeSettings",
"getDownloadUrl" => "docs.getDownloadUrl", "delete" =>
"docs.delete",
"search" => "docs.search", "getAutoSigninUrl" =>
"user.getAutoSigninUrl",
"setAccess" => "security.setAccess", "getDocumentAccessList" =>
"security.getDocumentAccessList",
"getUserAccessList" => "security.getUserAccessList"
}

def scribd_login(options)
response = Net::HTTP.get_response(parse_url(build_url('login',
options))).body
doc = REXML::Document.new(response)
if doc.elements["rsp"].attributes["stat"] == "fail"
hash_data = {
:message => doc.elements["rsp/error"].attributes
["message"].to_s,
:status => 'fail'
}
return hash_data
else
session[:scribd_id] = doc.elements["rsp/session_key"].text.to_s
hash_data = {
:session_value => session[:scribd_id],
:status => doc.elements["rsp"].attributes["stat"].to_s
}
return hash_data
end
end

def scribd_upload(options)
response = Net::HTTP.get_response(parse_url(build_url('upload',
options))).body
doc = REXML::Document.new(response)
return doc.elements["rsp/error"].attributes["message"].to_s
end

def scribd_signup(options)
response = Net::HTTP.get_response(parse_url(build_url('signup',
options))).body
doc = REXML::Document.new(response)
if doc.elements["rsp"].attributes["stat"] == "fail"
hash_data = {
:message => doc.elements["rsp/error"].attributes
["message"].to_s,
:status => 'fail'
}
return hash_data
else
session[:scribd_id] = doc.elements["rsp/session_key"].text.to_s
hash_data = {
:session_value => session[:scribd_id],
:status => doc.elements["rsp"].attributes["stat"].to_s
}
return hash_data
end

end

def scribd_login_from_cookie
#response = Net::HTTP.get_response(parse_url(build_url('login',
options))).body
end


private
def parse_url(strURL)
strURL = BASE_API_URL + strURL
return URI::parse(strURL)
end

def build_url(method_type, options)
strURL = "method=" + SCRIBD_METHOD[method_type]
strURL += "&api_key=" + API_KEY
strURL += "&username=" + options[:username] if options[:username]
strURL += "&password=" + options[:password] if options[:password]
strURL += "&name=" + options[:name] if options[:name]
strURL += "&email=" + options[:email] if options[:email]
strURL += "&file=" + options[:file] if options[:file]
strURL += "&doc_type=" + options[:doc_type] if options[:doc_type]
strURL += "&access=" + options[:access] if options[:access]
strURL += "&rev_id=" + options[:rev_id] if options[:rev_id]
strURL += "&paid_content=" + options[:paid_content] if options
[:paid_content]
strURL += "&secure_document=" + options[:secure_document] if
options[:secure_document]
strURL += "&download_and_drm=" + options[:download_and_drm] if
options[:download_and_drm]
strURL += "&api_sig=" + options[:api_sig] if options[:api_sig]
strURL += "&session_key=" + options[:session_key] if options
[:session_key]
strURL += "&my_user_id=" + options[:my_user_id] if options
[:my_user_id]
strURL += "&use_api_content=" + options[:use_api_content] if
options[:use_api_content]
strURL += "&limit=" + options[:limit] if options[:limit]
strURL += "&offset=" + options[:offset] if options[:offset]
strURL += "&doc_id=" + options[:doc_id] if options[:doc_id]
strURL += "&api_id=" + options[:api_id] if options[:api_id]
strURL += "&isbn=" + options[:isbn] if options[:isbn]
strURL += "&title=" + options[:title] if options[:title]
strURL += "&description=" + options[:description] if options
[:description]
strURL += "&license=" + options[:license] if options[:license]
strURL += "&show_ads=" + options[:show_ads] if options[:show_ads]
strURL += "&link_back_url=" + options[:link_back_url] if options
[:link_back_url]
strURL += "&category=" + options[:category] if options[:category]
strURL += "&tags=" + options[:tags] if options[:tags]
strURL += "&author=" + options[:author] if options[:author]
strURL += "&publisher=" + options[:publisher] if options
[:publisher]
strURL += "&when_publisher=" + options[:when_publisher] if options
[:when_publisher]
strURL += "&edition=" + options[:edition] if options[:edition]
strURL += "&disable_upload_link=" + options[:disable_upload_link]
if options[:disable_upload_link]
strURL += "&disable_select_text=" + options[:disable_select_text]
if options[:disable_select_text]
strURL += "&disable_about_dialog=" + options
[:disable_about_dialog] if options[:disable_about_dialog]
strURL += "&disable_info_dialog=" + options[:disable_info_dialog]
if options[:disable_info_dialog]
strURL += "&disable_view_mode_change=" + options
[:disable_view_mode_change] if options[:disable_view_mode_change]
strURL += "&disable_related_docs=" + options
[:disable_related_docs] if options[:disable_related_docs]
strURL += "&disable_print=" + options[:disable_print] if options
[:disable_print]
strURL += "&query=" + options[:query] if options[:query]
strURL += "&num_results=" + options[:num_results] if options
[:num_results]
strURL += "&num_start=" + options[:num_start] if options
[:num_start]
strURL += "&scope=" + options[:scope] if options[:scope]
strURL += "&user_identifier=" + options[:user_identifier] if
options[:user_identifier]
strURL += "&allowed=" + options[:allowed] if options[:allowed]
strURL += "&session_id=" + options[:session_id] if options
[:session_id]
return strURL
end

end

please help me guys, i'm stuck here. No luck i tried in many ways.

Jared Friedman

unread,
Nov 4, 2009, 3:25:01 PM11/4/09
to scribd-platfo...@googlegroups.com
Hi there,

Did you look at the rscribd gem? Doing multipart POSTs in ruby can
indeed be tricky, and the rscribd gem already has a working
implementation.

Jared

Bala

unread,
Nov 4, 2009, 5:27:36 PM11/4/09
to Scribd Platform Talk
Hi Jared,

yeah i tried initally but my clients requirement to use Javascript API
and that too, we had loads of issue while installing gems, we were
more concern
with version.

Thanks,
Bala

On Nov 5, 1:25 am, Jared Friedman <ja...@scribd.com> wrote:
> Hi there,
>
> Did you look at the rscribd gem?  Doing multipart POSTs in ruby can
> indeed be tricky, and the rscribd gem already has a working
> implementation.
>
> Jared
>
> On Wed, Nov 4, 2009 at 12:20 PM, Bala <mbb...@gmail.com> wrote:
>
> > Experts,
>
> > I'm trying to upload a file through ruby code. i tried docs.upload
> > through
> > but it always says you have to upload through Multiform/POST.
> > Even i tried withhttp://stackoverflow.com/questions/184178/ruby-how-to-post-a-file-via...
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages