Class: Github::Repos::Contents

Inherits:
API
  • Object
show all
Defined in:
lib/github_api/repos/contents.rb

Overview

These API methods let you retrieve the contents of files within a repository as Base64 encoded content.

Constant Summary collapse

REQUIRED_CONTENT_OPTIONS =
%w[ path message content ]

Constants included from Github::Request

Github::Request::METHODS, Github::Request::METHODS_WITH_BODIES

Constants included from Connection

Connection::ALLOWED_OPTIONS

Constants included from Constants

Constants::ACCEPT, Constants::ACCEPTED_OAUTH_SCOPES, Constants::ACCEPT_CHARSET, Constants::CACHE_CONTROL, Constants::CONTENT_LENGTH, Constants::CONTENT_TYPE, Constants::DATE, Constants::ETAG, Constants::HEADER_LAST, Constants::HEADER_LINK, Constants::HEADER_NEXT, Constants::LOCATION, Constants::META_FIRST, Constants::META_LAST, Constants::META_NEXT, Constants::META_PREV, Constants::META_REL, Constants::OAUTH_SCOPES, Constants::PARAM_PAGE, Constants::PARAM_PER_PAGE, Constants::PARAM_START_PAGE, Constants::RATELIMIT_LIMIT, Constants::RATELIMIT_REMAINING, Constants::SERVER, Constants::USER_AGENT

Constants included from MimeType

MimeType::MEDIA_LOOKUP

Instance Attribute Summary

Attributes inherited from API

#current_options

Attributes included from Authorization

#scopes

Instance Method Summary collapse

Methods inherited from API

#api_methods_in, #append_arguments, #arguments, inherited, #initialize, #method_missing, #process_basic_auth, #set, #setup, #with, #yield_or_eval

Methods included from Github::RateLimit

#ratelimit, #ratelimit_remaining

Methods included from Github::Request

#delete_request, #get_request, #patch_request, #post_request, #put_request, #request

Methods included from Connection

#caching?, #clear_cache, #connection, #default_middleware, #default_options, #stack

Methods included from MimeType

#lookup_media, #parse

Methods included from Authorization

#auth_code, #authenticated?, #authentication, #authorize_url, #basic_authed?, #client, #get_token

Constructor Details

This class inherits a constructor from Github::API

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Github::API

Instance Method Details

#archive(*args) ⇒ Object

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"


178
179
180
181
182
183
184
185
# File 'lib/github_api/repos/contents.rb', line 178

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

#create(*args) ⇒ Object

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"


84
85
86
87
88
89
90
91
92
# File 'lib/github_api/repos/contents.rb', line 84

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

#delete(*args) ⇒ Object

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"


151
152
153
154
155
156
157
# File 'lib/github_api/repos/contents.rb', line 151

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(*args) ⇒ Object Also known as: find

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'


43
44
45
46
47
48
# File 'lib/github_api/repos/contents.rb', line 43

def get(*args)
  arguments(args, :required => [:user, :repo, :path])
  params = arguments.params

  get_request("/repos/#{user}/#{repo}/contents/#{path}", params)
end

#readme(*args) ⇒ Object

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


22
23
24
25
26
27
# File 'lib/github_api/repos/contents.rb', line 22

def readme(*args)
  arguments(args, :required => [:user, :repo])
  params = arguments.params

  get_request("/repos/#{user}/#{repo}/readme", params)
end

#update(*args) ⇒ Object

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"


115
116
117
# File 'lib/github_api/repos/contents.rb', line 115

def update(*args)
  create(*args)
end