# encoding: utf-8
module Github
# These API methods let you retrieve the contents of files within a repository
# as Base64 encoded content.
class Repos::Contents < API
REQUIRED_CONTENT_OPTIONS = %w[ path message content ]
# Get the README
#
# This method returns the preferred README for a repository.
#
# = Examples
# github = Github.new
# github.repos.contents.readme 'user-name', 'repo-name'
#
# content = Github::Repos;:Contents.new user: 'user-name', 'repo-name'
# content.readme
#
def readme(*args)
arguments(args, :required => [:user, :repo])
params = arguments.params
get_request("/repos/#{user}/#{repo}/readme", params)
end
# Get contents
#
# This method returns the contents of any file or directory in a repository.
#
# = Parameters
# * :ref - Optional string - valid Git reference, defaults to master
#
# = Examples
# github = Github.new
# github.repos.contents.get 'user-name', 'repo-name', 'path'
#
# github = Github.new user: 'user-name', repo: 'repo-name'
# github.repos.contents.get path: 'README.md'
#
def get(*args)
arguments(args, :required => [:user, :repo, :path])
params = arguments.params
get_request("/repos/#{user}/#{repo}/contents/#{path}", params)
end
alias :find :get
# Create a file
#
# This method creates a new file in a repository
#
# = Parameters
# * :path - Requried string - The content path
# * :message - Requried string - The commit message
# * :content - Requried string - The new file content,
# which will be Base64 encoded
# * :branch - Optional string - The branch name. If not provided,
# uses the repository’s default branch (usually master)
# = Optional Parameters
#
# The author section is optional and is filled in with the
# committer information if omitted. If the committer
# information is omitted, the authenticated user’s information is used.
#
# You must provide values for both name and email, whether
# you choose to use author or committer. Otherwise, you’ll
# receive a 500 status code.
#
# * author.name - string - The name of the author of the commit
# * author.email - string - The email of the author of the commit
# * committer.name - string - The name of the committer of the commit
# * committer.email - string - The email of the committer of the commit
#
# = Examples
# github = Github.new
# github.repos.contents.create 'user-name', 'repo-name', 'path',
# path: 'hello.rb',
# content: "puts 'hello ruby'",
# message: "my commit message"
#
def create(*args)
arguments(args, :required => [:user, :repo, :path]) do
assert_required REQUIRED_CONTENT_OPTIONS
end
params = arguments.params
params.strict_encode64('content')
put_request("/repos/#{user}/#{repo}/contents/#{path}", params)
end
# Update a file
#
# This method updates a file in a repository
#
# = Parameters
# * :path - Requried string - The content path
# * :message - Requried string - The commit message
# * :content - Requried string - The new file content,
# which will be Base64 encoded
# * :sha - Requried string - The blob SHA of the file being replaced.
# * :branch - Optional string - The branch name. If not provided,
# uses the repository’s default branch (usually master)
#
# = Examples
# github = Github.new
# github.repos.contents.update 'user-name', 'repo-name', 'path',
# path: 'hello.rb',
# content: "puts 'hello ruby again'",
# message: "my commit message",
# sha: "25b0bef9e404bd2e3233de26b7ef8cbd86d0e913"
#
def update(*args)
create(*args)
end
# Delete a file
#
# This method deletes a file in a repository
#
# = Parameters
# * :path - Requried string - The content path
# * :message - Requried string - The commit message
# * :sha - Requried string - The blob SHA of the file being removed.
# * :branch - Optional string - The branch name. If not provided,
# uses the repository’s default branch (usually master)
# = Optional Parameters
#
# The author section is optional and is filled in with the
# committer information if omitted. If the committer
# information is omitted, the authenticated user’s information is used.
#
# You must provide values for both name and email, whether
# you choose to use author or committer. Otherwise, you’ll
# receive a 500 status code.
#
# * author.name - string - The name of the author of the commit
# * author.email - string - The email of the author of the commit
# * committer.name - string - The name of the committer of the commit
# * committer.email - string - The email of the committer of the commit
#
# = Examples
# github = Github.new
# github.repos.contents.delete 'user-name', 'repo-name', 'path',
# path: 'hello.rb',
# message: "delete hello.rb file",
# sha: "329688480d39049927147c162b9d2deaf885005f"
#
def delete(*args)
arguments(args, :required => [:user, :repo, :path]) do
assert_required %w[ path message sha ]
end
delete_request("/repos/#{user}/#{repo}/contents/#{path}", arguments.params)
end
# Get archive link
#
# This method will return a 302 to a URL to download a tarball or zipball
# archive for a repository. Please make sure your HTTP framework is configured
# to follow redirects or you will need to use the Location header to make
# a second GET request.
#
# Note: For private repositories, these links are temporary and expire quickly.
#
# = Parameters
# * :archive_format - Required string - either tarball or zipball
# * :ref - Optional string - valid Git reference, defaults to master
#
# = Examples
# github = Github.new
# github.repos.contents.archive 'user-name', 'repo-name',
# "archive_format" => "tarball",
# "ref" => "master"
#
def archive(*args)
arguments(args, :required => [:user, :repo])
params = arguments.params
archive_format = params.delete('archive_format') || 'zipball'
ref = params.delete('ref') || 'master'
get_request("/repos/#{user}/#{repo}/#{archive_format}/#{ref}", params)
end
end # Repos::Contents
end # Github