Skip to content

Instantly share code, notes, and snippets.

@jeremywrowe
Created August 19, 2012 06:16
Show Gist options
  • Save jeremywrowe/3392593 to your computer and use it in GitHub Desktop.
Save jeremywrowe/3392593 to your computer and use it in GitHub Desktop.
An example of creating a download with the github v3 api
require 'rubygems'
require "net/https"
require "uri"
require "json"
login = ENV['BF_GITHUB_USER']
pass = ENV['BF_GITHUB_PASS']
repos = ENV['BF_GITHUB_REPO']
file_to_upload = 'http/git.ref'
file_download_name = file_to_download.split("/")[-1]
uri = URI.parse("https://api.github.com")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
payload = {
"size" => File.stat(file_to_upload).size,
"content_type" => `file -Ib #{file_to_upload}`.gsub(/\n/,"").split(";")[0],
"name" => file_download_name,
}.to_json
request = Net::HTTP::Post.new("/repos/#{repos}/downloads", initheader = {'Content-Type' =>'application/json'})
request.basic_auth(login, pass)
request.body = payload
http = Net::HTTP.new(uri.host, 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.start{|http| http.request(request)}
if response.code == "201"
json = JSON.parse(response.body)
`curl \
-F "key=#{json['path']}" \
-F "acl=#{json['acl']}" \
-F "success_action_status=201" \
-F "Filename=#{json['name']}" \
-F "AWSAccessKeyId=#{json['accesskeyid']}" \
-F "Policy=#{json['policy']}" \
-F "Signature=#{json['signature']}" \
-F "Content-Type=#{json['content_type']}" \
-F "file=@#{file_to_upload}" \
#{json["s3_url"]}`
else
raise "File upload was unsuccessful\n#{response.body}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment