Class: GithubUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/github_uploader.rb

Constant Summary collapse

AUTH_NOTE =
"Github Uploader (gem)"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login, username, repo, root = Dir.pwd) ⇒ GithubUploader

Returns a new instance of GithubUploader.



38
39
40
41
42
43
44
# File 'lib/github_uploader.rb', line 38

def initialize(, username, repo, root=Dir.pwd)
  @login    = 
  @username = username
  @repo     = repo
  @root     = root
  @token    = check_token
end

Class Method Details

.setup_uploaderObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/github_uploader.rb', line 7

def self.setup_uploader
  # get the github user name
   = `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
  #              [email protected]:emberjs/ember.js.git
  #              [email protected]: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(, username, repo)
  uploader.authorize

  uploader
end

.upload_file(uploader, filename, description, file) ⇒ Object



29
30
31
32
33
34
35
36
# 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

Instance Method Details

#authorizeObject



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
# File 'lib/github_uploader.rb', line 58

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 "Githhub Uploader (gem)" 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"] == AUTH_NOTE
      # 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 => AUTH_NOTE,
      :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

#authorized?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/github_uploader.rb', line 46

def authorized?
  !!@token
end

#check_tokenObject



54
55
56
# File 'lib/github_uploader.rb', line 54

def check_token
  File.exist?(token_path) ? File.open(token_path, "rb").read : nil
end

#token_pathObject



50
51
52
# File 'lib/github_uploader.rb', line 50

def token_path
  File.expand_path(".github-upload-token", @root) 
end

#upload_file(filename, description, file) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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