Add support for creating downloads #97

Merged
merged 4 commits into from Jun 18, 2012
@@ -24,6 +24,39 @@ def downloads(repo, options={})
def download(repo, id, options={})
get("repos/#{Repository.new(repo)}/downloads/#{id}", options, 3)
end
+
+
+ def create_download(repo, name, options={})
+ options[:content_type] ||= 'text/plain'
+ file = Faraday::UploadIO.new(name, options[:content_type])
+ resource = create_download_resource(repo, file.original_filename, file.size, options)
+
+ resource_hash = {
+ 'key' => resource.path,
+ 'acl' => resource.acl,
+ 'success_action_status' => 201,
+ 'Filename' => resource.name,
+ 'AWSAccessKeyId' => resource.accesskeyid,
+ 'Policy' => resource.policy,
+ 'Signature' => resource.signature,
+ 'Content-Type' => resource.mime_type,
+ 'file' => file
+ }
+
+ conn = Faraday.new(resource.s3_url) do |builder|
+ builder.request :multipart
+ builder.request :url_encoded
+ builder.adapter :net_http
+ end
+
+ response = conn.post '/', resource_hash
+ response.status == 201
+ end
+
+ private
+ def create_download_resource(repo, name, size, options={})
+ post("/repos/#{Repository.new(repo)}/downloads", options.merge({:name => name, :size => size}))
+ end
end
end
end
@@ -0,0 +1,21 @@
+{
+ "url": "https://api.github.com/repos/octocat/Hello-World/downloads/1",
+ "html_url": "https://github.com/repos/octocat/Hello-World/downloads/new_file.jpg",
+ "id": 1,
+ "name": "new_file.jpg",
+ "description": "Description of your download",
+ "size": 1024,
+ "download_count": 40,
+ "content_type": ".jpg",
+ "policy": "ewogICAg...",
+ "signature": "mwnFDC...",
+ "bucket": "github",
+ "accesskeyid": "1ABCDEFG...",
+ "path": "downloads/ocotocat/Hello-World/new_file.jpg",
+ "acl": "public-read",
+ "expirationdate": "2011-04-14T16:00:49Z",
+ "prefix": "downloads/octocat/Hello-World/",
+ "mime_type": "image/jpeg",
+ "redirect": false,
+ "s3_url": "https://github.s3.amazonaws.com/"
+}
@@ -7,7 +7,7 @@
@client = Octokit::Client.new(:login => 'sferik')
end
- describe ".downloads" do
+ describe ".getting downloads" do
it "should list available downloads" do
stub_get("/repos/github/hubot/downloads").
@@ -16,10 +16,6 @@
downloads.first.description.should == "Robawt"
end
- end
-
- describe ".download" do
-
it "should get a single download" do
stub_get("/repos/github/hubot/downloads/165347").
to_return(:body => fixture("v3/download.json"))
@@ -30,4 +26,25 @@
end
+ describe ".create a download" do
+ before(:each) do
+ stub_post("/repos/octocat/Hello-World/downloads").
+ with(:body => {:name => "download_create.json", :size => 690,
+ :description => "Description of your download",
+ :content_type => "text/plain" }).
+ to_return(:body => fixture("v3/download_create.json"))
+ end
+ it "should create a download resource" do
+ resource = @client.send(:create_download_resource, "octocat/Hello-World", "download_create.json", 690, {:description => "Description of your download", :content_type => "text/plain"})
+ resource.s3_url.should == "https://github.s3.amazonaws.com/"
+ end
+
+ it "should post to a S3 url" do
+ stub_post("https://github.s3.amazonaws.com/").
+ to_return(:status => 201)
+ file_path = File.expand_path 'spec/fixtures/v3/download_create.json'
+ @client.create_download("octocat/Hello-World", file_path, {:description => "Description of your download", :content_type => "text/plain"}).should == true
+ end
+ end
+
end