class Zine::UploaderGitHub

Upload a file to GitHub using its REST API

Public Class Methods

new(build_dir, options, credentials, delete_file_array, upload_file_array) click to toggle source

Requires zine.yaml to have a path to a credentials yaml file with access_token: … For instructions on how to create an access token: help.github.com/articles/creating-an-access-token-for-command-line-use/

# File lib/zine/uploader_github.rb, line 13
def initialize(build_dir, options, credentials, delete_file_array,
               upload_file_array)
  unless options['method'] == 'github'
    @no_upload = true
    return
  end
  @build_dir = build_dir
  @repo_full_name = options['path_or_repo']
  @client = Octokit::Client.new(access_token: credentials['access_token'])
  @verbose = options['verbose']
  @credentials = credentials
  @delete_file_array = delete_file_array
  @upload_file_array = upload_file_array
end

Public Instance Methods

upload() click to toggle source

Duplicates within & between the files already removed in Zine::Upload then .each… upload & delete - uses @build_dir to create relative paths

# File lib/zine/uploader_github.rb, line 30
def upload
  return if @no_upload
  @delete_file_array.each do |file_pathname|
    delete_file file_pathname
  end
  @upload_file_array.each do |file_pathname|
    upload_file file_pathname
  end
end

Private Instance Methods

delete_file(rel_path) click to toggle source

see if file exists, then delete it if it does returns commit hash (unused)

# File lib/zine/uploader_github.rb, line 44
def delete_file(rel_path)
  info_hash = info rel_path
  @client.delete_contents(@repo_full_name,
                          rel_path,
                          'Zine delete', # commit message
                          info_hash[:sha],
                          branch: 'gh-pages')
  puts "Deleted #{rel_path}" if @verbose
rescue Octokit::NotFound
  puts "Tried to delete nonexistent remote file #{rel_path}"
end
info(github_path) click to toggle source

return info on a file if it exsists, otherwise throws Octokit::NotFound

# File lib/zine/uploader_github.rb, line 57
def info(github_path)
  Octokit.contents(@repo_full_name,
                   path: github_path,
                   ref: 'gh-pages')
end
upload_create(rel_path) click to toggle source
# File lib/zine/uploader_github.rb, line 72
def upload_create(rel_path)
  res = @client.create_contents(@repo_full_name,
                                rel_path,
                                'Zine upload', # commit message
                                file: File.join(@build_dir, rel_path),
                                branch: 'gh-pages')
  puts "Created #{res[:content][:path]}" if @verbose
end
upload_file(rel_path) click to toggle source

see if file exists on GitHub, then either create or update it returns hash including [:content] (unused)

# File lib/zine/uploader_github.rb, line 65
def upload_file(rel_path)
  info_hash = info rel_path
  upload_update rel_path, info_hash[:sha]
rescue Octokit::NotFound
  upload_create rel_path
end
upload_update(rel_path, sha) click to toggle source
# File lib/zine/uploader_github.rb, line 81
def upload_update(rel_path, sha)
  res = @client.update_contents(@repo_full_name,
                                rel_path,
                                'Zine upload', # commit message
                                sha,
                                file: File.join(@build_dir, rel_path),
                                branch: 'gh-pages')
  puts "Updated #{res[:content][:path]}" if @verbose
end