class GithubUploader
Constants
- AUTH_NOTE
Public Class Methods
new(login, username, repo, root=Dir.pwd)
click to toggle source
# File lib/github_uploader.rb, line 38 def initialize(login, username, repo, root=Dir.pwd) @login = login @username = username @repo = repo @root = root @token = check_token end
setup_uploader()
click to toggle source
# File lib/github_uploader.rb, line 7 def self.setup_uploader # get the github user name login = `git config github.user`.chomp # get repo from git config's origin url origin = `git config remote.origin.url`.chomp # url to origin # extract USERNAME/REPO_NAME # sample urls: https://github.com/emberjs/ember.js.git # git://github.com/emberjs/ember.js.git # git@github.com:emberjs/ember.js.git # git@github.com:emberjs/ember.js repoUrl = origin.match(/github\.com[\/:]((.+?)\/(.+?))(\.git)?$/) username = repoUrl[2] # username part of origin url repo = repoUrl[3] # repository name part of origin url uploader = GithubUploader.new(login, username, repo) uploader.authorize uploader end
upload_file(uploader, filename, description, file)
click to toggle source
# File lib/github_uploader.rb, line 29 def self.upload_file(uploader, filename, description, file) print "Uploading #{filename}..." if uploader.upload_file(filename, description, file) puts "Success" else puts "Failure" end end
Public Instance Methods
check_token()
click to toggle source
# File lib/github_uploader.rb, line 54 def check_token File.exist?(token_path) ? File.open(token_path, "rb").read : nil end
token_path()
click to toggle source
# File lib/github_uploader.rb, line 50 def token_path File.expand_path(".github-upload-token", @root) end
upload_file(filename, description, file)
click to toggle source
# File lib/github_uploader.rb, line 93 def upload_file(filename, description, file) return false unless authorized? gh = Github.new :user => @username, :repo => @repo, :oauth_token => @token # remvove previous download with the same name gh.repos.downloads do |download| if filename == download.name gh.repos.delete_download @username, @repo, download.id break end end # step 1 hash = gh.repos.create_download @username, @repo, "name" => filename, "size" => File.size(file), "description" => description, "content_type" => "application/json" # step 2 gh.repos.upload hash, file return true end