Skip to content

Instantly share code, notes, and snippets.

@fuzzmonkey
Forked from wedesoft/figshare.rb
Created June 24, 2013 10:18

Revisions

  1. fuzzmonkey revised this gist Jun 24, 2013. 1 changed file with 28 additions and 16 deletions.
    44 changes: 28 additions & 16 deletions figshare.rb
    Original file line number Diff line number Diff line change
    @@ -1,29 +1,41 @@
    #!/usr/bin/env ruby
    require 'net/http'
    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'
    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']
    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)
    result = Net::HTTP.start(url.host, url.port) do |http|
    consumer.sign! request, client
    http.request request # <- Errno::EPIPE
    end
    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
    #result = client.post "/v1/my_data/articles/#{article_id}/action/make_private"
    print result.body

    puts res.body
  2. @wedesoft wedesoft revised this gist Jun 24, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion figshare.rb
    Original 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")
    # 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)
  3. @wedesoft wedesoft revised this gist Jun 24, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion figshare.rb
    Original 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"
    #result = client.post "/v1/my_data/articles/#{article_id}/action/make_private"
    print result.body
  4. @wedesoft wedesoft revised this gist Jun 24, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion figshare.rb
    Original 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
    print result.body
  5. @wedesoft wedesoft created this gist Jun 24, 2013.
    29 changes: 29 additions & 0 deletions figshare.rb
    Original 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