Class: GithubDownloads::Uploader
- Inherits:
-
Object
- Object
- GithubDownloads::Uploader
- Defined in:
- lib/github_downloads/uploader.rb
Instance Attribute Summary collapse
-
#login ⇒ Object
readonly
Returns the value of attribute login.
-
#repo ⇒ Object
readonly
Returns the value of attribute repo.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #authorize ⇒ Object
- #authorized? ⇒ Boolean
- #check_token ⇒ Object
- #init_login(login = nil) ⇒ Object
- #init_repo(repo = nil) ⇒ Object
- #init_username(username = nil) ⇒ Object
-
#initialize(login = nil, username = nil, repo = nil, token = nil, root = Dir.pwd) ⇒ Uploader
constructor
A new instance of Uploader.
- #remove_file(filename) ⇒ Object
- #repo_url ⇒ Object
- #token_path ⇒ Object
- #upload_file(filename, description, file) ⇒ Object
Constructor Details
#initialize(login = nil, username = nil, repo = nil, token = nil, root = Dir.pwd) ⇒ Uploader
Returns a new instance of Uploader.
10 11 12 13 14 15 16 |
# File 'lib/github_downloads/uploader.rb', line 10 def initialize(login=nil, username=nil, repo=nil, token=nil, root=Dir.pwd) @login = init_login(login) @username = init_username(username) @repo = init_repo(repo) @root = root @token = token || ENV['GH_OAUTH_TOKEN'] || check_token end |
Instance Attribute Details
#login ⇒ Object (readonly)
Returns the value of attribute login.
8 9 10 |
# File 'lib/github_downloads/uploader.rb', line 8 def login @login end |
#repo ⇒ Object (readonly)
Returns the value of attribute repo.
8 9 10 |
# File 'lib/github_downloads/uploader.rb', line 8 def repo @repo end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
8 9 10 |
# File 'lib/github_downloads/uploader.rb', line 8 def token @token end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
8 9 10 |
# File 'lib/github_downloads/uploader.rb', line 8 def username @username end |
Instance Method Details
#authorize ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/github_downloads/uploader.rb', line 47 def return if require 'cgi' 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 for #{@login}: " 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://#{CGI.escape(@login)}:#{CGI.escape(pw)}@api.github.com/authorizations" JSON.parse(response.to_str).each do |auth| if auth["note"] == "GitHub Downloads Gem" # 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 => "GitHub Downloads Gem", :note_url => "https://github.com/pangratz/github_downloads" } response = RestClient.post "https://#{CGI.escape(@login)}:#{CGI.escape(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(token_path, 'w') {|f| f.write(@token)} # add entry to .gitignore if not already exists gitignore = File.(".gitignore", @root) system "touch #{gitignore}" includes = File.open(gitignore).lines.any? { |line| line.chomp == '.github-upload-token' } if !includes File.open(gitignore, "a") do |f| f.puts("\n# .github-upload-token stores OAuth token, used by github_downloads gem") f.puts(".github-upload-token") end end end |
#authorized? ⇒ Boolean
35 36 37 |
# File 'lib/github_downloads/uploader.rb', line 35 def !!@token end |
#check_token ⇒ Object
43 44 45 |
# File 'lib/github_downloads/uploader.rb', line 43 def check_token File.exist?(token_path) ? File.open(token_path, "rb").read : nil end |
#init_login(login = nil) ⇒ Object
18 19 20 |
# File 'lib/github_downloads/uploader.rb', line 18 def init_login(login=nil) login || ENV['GH_LOGIN'] || `git config github.user`.chomp end |
#init_repo(repo = nil) ⇒ Object
26 27 28 |
# File 'lib/github_downloads/uploader.rb', line 26 def init_repo(repo=nil) repo || ENV['GH_REPOSITORY'] || repo_url[3] end |
#init_username(username = nil) ⇒ Object
22 23 24 |
# File 'lib/github_downloads/uploader.rb', line 22 def init_username(username=nil) username || ENV['GH_USERNAME'] || repo_url[2] end |
#remove_file(filename) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/github_downloads/uploader.rb', line 95 def remove_file(filename) return false unless 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 end |
#repo_url ⇒ Object
30 31 32 33 |
# File 'lib/github_downloads/uploader.rb', line 30 def repo_url origin = `git config remote.origin.url`.chomp origin.match(/github\.com[\/:]((.+?)\/(.+?))(\.git)?$/) || [] end |
#token_path ⇒ Object
39 40 41 |
# File 'lib/github_downloads/uploader.rb', line 39 def token_path File.(".github-upload-token", @root) end |
#upload_file(filename, description, file) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/github_downloads/uploader.rb', line 109 def upload_file(filename, description, file) return false unless remove_file(filename) gh = Github.new :user => @username, :repo => @repo, :oauth_token => @token # 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 |