# encoding: utf-8moduleGithubclassReposmoduleDownloadsREQUIRED_PARAMS=%w[ name size ]VALID_DOWNLOAD_PARAM_NAMES=%w[
name
size
description
content_type
].freezeREQUIRED_S3_PARAMS=%w[
path
acl
name
accesskeyid
policy
signature
mime_type
].freeze# Status code for successful upload to Amazon S3 serviceSUCCESS_STATUS=201# List downloads for a repository## = Examples# @github = Github.new# @github.repos.downloads 'user-name', 'repo-name'# @github.repos.downloads 'user-name', 'repo-name' { |downl| ... }#defdownloads(user_name=nil,repo_name=nil,params={})_update_user_repo_params(user_name,repo_name)_validate_user_repo_params(user,repo)unlessuser?&&repo?_normalize_params_keys(params)response=get("/repos/#{user}/#{repo}/downloads")returnresponseunlessblock_given?response.each{|el|yieldel}endalias:list_downloads:downloadsalias:get_downloads:downloads# Get a single download## = Examples# @github = Github.new# @github.repos.download 'user-name', 'repo-name'#defdownload(user_name,repo_name,download_id,params={})_update_user_repo_params(user_name,repo_name)_validate_user_repo_params(user,repo)unlessuser?&&repo?_validate_presence_ofdownload_id_normalize_params_keys(params)get("/repos/#{user}/#{repo}/downloads/#{download_id}")endalias:get_download:download# Delete download from a repository## = Examples# @github = Github.new# @github.repos.delete_download 'user-name', 'repo-name', 'download-id'#defdelete_download(user_name,repo_name,download_id,params={})_update_user_repo_params(user_name,repo_name)_validate_user_repo_params(user,repo)unlessuser?&&repo?_validate_presence_ofdownload_id_normalize_params_keys(params)delete("/repos/#{user}/#{repo}/downloads/#{download_id}")end# Creating a new download is a two step process.# You must first create a new download resource using this method.# Response from this method is to be used in #upload method.## = Inputs# * <tt>:name</tt> - Required string - name of the file that is being created.# * <tt>:size</tt> - Required number - size of file in bytes.# * <tt>:description</tt> - Optional string# * <tt>:content_type</tt> - Optional string## = Examples# @github = Github.new# @github.repos.create_download 'user-name', 'repo-name',# "name" => "new_file.jpg",# "size" => 114034,# "description" => "Latest release",# "content_type" => "text/plain"#defcreate_download(user_name=nil,repo_name=nil,params={})_update_user_repo_params(user_name,repo_name)_validate_user_repo_params(user,repo)unlessuser?&&repo?_normalize_params_keys(params)_filter_params_keys(VALID_DOWNLOAD_PARAM_NAMES,params)raiseArgumentError,"Required parameters are: #{REQUIRED_PARAMS.join(', ')}"unless_validate_inputs(REQUIRED_PARAMS,params)post("/repos/#{user}/#{repo}/downloads",params)end# Upload a file to Amazon, using the reponse instance from# Github::Repos::Downloads#create_download. This can be done by passing# the response object as an argument to upload method.## = Parameters# * <tt>resource</tt> - Required Hashie::Mash -resource of the create_download call# * <tt>:size</tt> - Required number - size of file in bytes.#defupload(resource,filename)_validate_presence_ofresource,filenameraiseArgumentError,'Need to provied resource of Github::Repose::Downloads#create_download call'unlessresource.is_a?Hashie::MashREQUIRED_S3_PARAMS.eachdo|key|raiseArgumentError,"Expected following key: #{key}"unlessresource.respond_to?(key)end# TODO use ordered hash if Ruby < 1.9hash=ruby_18{require'active_support'ActiveSupport::OrderedHash.new}||ruby_19{Hash.new}mapped_params={'key'=>resource.path,'acl'=>resource.acl,'success_action_status'=>SUCCESS_STATUS,'Filename'=>resource.name,'AWSAccessKeyId'=>resource.accesskeyid,'Policy'=>resource.policy,'Signature'=>resource.signature,'Content-Type'=>resource.mime_type,'file'=>prepend_at_for(filename.to_s)}post('',mapped_params,{:url=>resource.s3_url})endalias:upload_to_s3:uploadalias:upload_to_amazon:uploadprivatedefprepend_at_for(file)/^@.*/=~file?'@'+file:fileendend# Downloadsend# Reposend# Github