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'
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment