Skip to content

Instantly share code, notes, and snippets.

@fuzzmonkey
Forked from wedesoft/figshare.rb
Created June 24, 2013 10:18
Show Gist options
  • Save fuzzmonkey/5849118 to your computer and use it in GitHub Desktop.
Save fuzzmonkey/5849118 to your computer and use it in GitHub Desktop.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment