forked from emberjs/data
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
164 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,86 @@ | |||
require "rest-client" | |||
require "github_api" | |||
|
|||
class GithubUploader | |||
|
|||
def initialize(login, username, repo, token=nil, root=Dir.pwd) | |||
@login = login | |||
@username = username | |||
@repo = repo | |||
@root = root | |||
@token = token || check_token | |||
end | |||
|
|||
def authorized? | |||
!!@token | |||
end | |||
|
|||
def token_path | |||
File.expand_path(".github-upload-token", @root) | |||
end | |||
|
|||
def check_token | |||
File.exist?(token_path) ? File.open(token_path, "rb").read : nil | |||
end | |||
|
|||
def authorize | |||
return if authorized? | |||
|
|||
puts "There is no file named .github-upload-token in this folder. This file holds the OAuth token needed to communicate with GitHub." | |||
puts "You will be asked to enter your GitHub password so a new OAuth token will be created." | |||
print "GitHub Password: " | |||
system "stty -echo" # disable echoing of entered chars so password is not shown on console | |||
pw = STDIN.gets.chomp | |||
system "stty echo" # enable echoing of entered chars | |||
puts "" | |||
|
|||
# check if the user already granted access for Ember.js Uploader by checking the available authorizations | |||
response = RestClient.get "https://#{@login}:#{pw}@api.github.com/authorizations" | |||
JSON.parse(response.to_str).each do |auth| | |||
if auth["note"] == "Ember.js Uploader" | |||
# user already granted access, so we reuse the existing token | |||
@token = auth["token"] | |||
end | |||
end | |||
|
|||
## we need to create a new token | |||
unless @token | |||
payload = { | |||
:scopes => ["public_repo"], | |||
:note => "Ember.js Uploader", | |||
:note_url => "https://github.com/#{@username}/#{@repo}" | |||
} | |||
response = RestClient.post "https://#{@login}:#{pw}@api.github.com/authorizations", payload.to_json, :content_type => :json | |||
@token = JSON.parse(response.to_str)["token"] | |||
end | |||
|
|||
# finally save the token into .github-upload-token | |||
File.open(".github-upload-token", 'w') {|f| f.write(@token)} | |||
end | |||
|
|||
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.list @username, @repo do |download| | |||
if filename == download.name | |||
gh.repos.downloads.delete @username, @repo, download.id | |||
break | |||
end | |||
end | |||
|
|||
# step 1 | |||
hash = gh.repos.downloads.create @username, @repo, | |||
"name" => filename, | |||
"size" => File.size(file), | |||
"description" => description | |||
|
|||
# step 2 | |||
gh.repos.downloads.upload hash, file | |||
|
|||
return true | |||
end | |||
|
|||
end |