Revisions
-
fuzzmonkey revised this gist
Jun 24, 2013 . 1 changed file with 28 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,29 +1,41 @@ #!/usr/bin/env ruby require 'net/http/post/multipart' require 'mime/types' require 'oauth' require 'json' require 'uri' CONSUMER_KEY = '' CONSUMER_SECRET = '' OAUTH_TOKEN = '' OAUTH_SECRET = '' file = '/Users/gsheppard/Documents/figshare-test.odt' ct = {"Content-Type"=>"application/json"} # setup the oauth stuff consumer = OAuth::Consumer.new CONSUMER_KEY, CONSUMER_SECRET, {:site => 'http://api.figshare.com'} token = { :oauth_token => OAUTH_TOKEN, :oauth_token_secret => OAUTH_SECRET } client = OAuth::AccessToken.from_hash consumer, token # create an article body = JSON.generate 'title'=>'Test dataset', 'description'=>'Test description', 'defined_type'=>'dataset' result = client.post '/v1/my_data/articles', body, {"Content-Type"=>"application/json"} draft = JSON.parse result.body article_id = draft['article_id'] uri = URI.parse("http://api.figshare.com/v1/my_data/articles/#{article_id}/files") # upload file payload = {'filedata' => UploadIO.new(file, MIME::Types.of(file))} req = Net::HTTP::Put::Multipart.new(uri.path, payload, {'Content-Type' => 'multipart/form-data'}) oauth_params = {:consumer => consumer, :token => client, :request_uri => uri} oauth_helper = OAuth::Client::Helper.new(req, oauth_params) req.add_field "Authorization", oauth_helper.header # Signs the request http = Net::HTTP.new(uri.host, uri.port) # http.set_debug_output $stdout use this to debug net/http res = http.start do |http| http.request(req) end puts res.body -
wedesoft revised this gist
Jun 24, 2013 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,6 @@ draft = JSON.parse result.body article_id = draft['article_id'] url = URI.parse("http://api.figshare.com/v1/my_data/articles/#{article_id}/files") File.open(FILE) do |dataset| request = Net::HTTP::Put::Multipart.new url.path, 'filedata' => UploadIO.new(dataset, MIME::Types.of(FILE), FILE) -
wedesoft revised this gist
Jun 24, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,5 +26,5 @@ http.request request # <- Errno::EPIPE end end #result = client.post "/v1/my_data/articles/#{article_id}/action/make_private" print result.body -
wedesoft revised this gist
Jun 24, 2013 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ #!/usr/bin/env ruby require 'net/http' require 'net/http/post/multipart' require 'mime/types' require 'oauth' @@ -26,4 +27,4 @@ end end result = client.post "/v1/my_data/articles/#{article_id}/action/make_private" print result.body -
wedesoft created this gist
Jun 24, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ #!/usr/bin/env ruby require 'net/http/post/multipart' require 'mime/types' require 'oauth' require 'json' CONSUMER_KEY = 'yhbsRBVKqJUSDvGtiRQoXw' CONSUMER_SECRET = ... OAUTH_TOKEN = 'BDyaluIt5aktGxj0BbLOTAnRXTUzHCDFR2FMSvrRvlnAXDyaluIt5aktGxj0XbLOTA' OAUTH_SECRET = ... FILE = 'test.pdf' consumer = OAuth::Consumer.new CONSUMER_KEY, CONSUMER_SECRET, {:site => 'http://api.figshare.com'} token = { :oauth_token => OAUTH_TOKEN, :oauth_token_secret => OAUTH_SECRET } client = OAuth::AccessToken.from_hash consumer, token body = JSON.generate 'title'=>'Test dataset', 'description'=>'Test description', 'defined_type'=>'dataset' result = client.post '/v1/my_data/articles', body, {"Content-Type"=>"application/json"} draft = JSON.parse result.body article_id = draft['article_id'] url = URI.parse("http://api.figshare.com/v1/my_data/articles/#{article_id}/files") # https://github.com/geemus/excon/issues/196 File.open(FILE) do |dataset| request = Net::HTTP::Put::Multipart.new url.path, 'filedata' => UploadIO.new(dataset, MIME::Types.of(FILE), FILE) result = Net::HTTP.start(url.host, url.port) do |http| consumer.sign! request, client http.request request # <- Errno::EPIPE end end result = client.post "/v1/my_data/articles/#{article_id}/action/make_private" print result.body