Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for creating downloads #97

Merged
merged 4 commits into from
Jun 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/octokit/client/downloads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions spec/fixtures/v3/download_create.json
Original file line number Diff line number Diff line change
@@ -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/"
}
27 changes: 22 additions & 5 deletions spec/octokit/client/downloads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand All @@ -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"))
Expand All @@ -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