GitHub API v3
This is a simple and easy to use wrapper for GitHub's v3 API. Knowing a little about GitHub's API will aid in its use, but is not necessary. Nearly all methods of the API have been implemented, but there are a few left to go.
Documentation
Full documentation for the gem can be found at rdoc.info/gems/github_api_v3/frames. I strongly recommend checking it out, as the library is very well documented.
Installation
To install the gem, issue the following command:
$ gem install github_api_v3
Usage
require 'github_api_v3'
Unauthenticated Requests
There are multiple different unauthenticated requests to the API. These are performed when no credentials are given, and are shown below starting with GitHub
. Unauthenticated methods can certainly be called from an authenticated client, and this is recommended as to not reach the hourly rate limit.
Authenticated Requests
Some methods, such as retrieving private repos or emails, require authentication. To create one of these requests, you'll need to pass in your login and access token. You can create a personal access token on your account page.
client = GitHub.client(
:login => 'username',
:access_token => 'bf215181b5140522137b3d4f6b73544a'
)
client.emails
client.repos
client.follow('matz')
client.user
Anytime a method is shown below starting with client
, it is an authenticated method.
Sample usage
The following sections are some sample usages of the library for parts of the API that I consider to be very useful. The methods in this section are not comprehensive, but aim to give a nice overview of some usage of this library.
Users
The following are some sample usages of the Users module.
user = GitHub.user('caseyscarborough')
user.login
user.name
user.html_url
user.following
GitHub.users
client.emails
client.follow('caseyscarborough')
client.unfollow('caseyscarborough')
GitHub.follows?('caseyscarborough','matz')
GitHub.followers('caseyscarborough')
client.followers
GitHub.following('caseyscarborough')
client.following?('caseyscarborough')
GitHub.events('caseyscarborough')
client.notifications
client.watching
client.starring
GitHub.rate_limit
client.rate_limit
You can find the available attributes here. Check the Users documentation for the gem for a complete list and more examples.
Repositories
The following are some sample usages for the Repos module.
GitHub.repos('caseyscarborough')
client.repos
GitHub.all_repos
GitHub.repo('owner','repo-name')
client.create_repo('new-repo', description: 'New repository.', private: true)
client.delete_repo('repo-name')
GitHub.contributors('caseyscarborough','github')
GitHub.languages('caseyscarborough','github')
GitHub.tags('caseyscarborough','github')
GitHub.branches('caseyscarborough','github')
GitHub.collaborators('caseyscarborough','github')
GitHub.branch('caseyscarborough','github','master')
client.add_collaborator('owner','repo-name','user-to-add')
client.remove_collaborator('owner','repo-name','user-to-remove')
client.subscribe('caseyscarborough','github')
client.unsubscribe('caseyscarborough','github')
client.star('caseyscarborough','github')
client.unstar('caseyscarborough','github')
For a full list with descriptions, see the Repos documentation for the gem.
Events
The following are some sample usages of the Events module.
GitHub.user_events('caseyscarborough')
GitHub.public_user_events('caseyscarborough')
GitHub.public_events
GitHub.repo_events('owner','repo-name')
GitHub.organization_events('ruby')
Check out the Events documentation for a comprehensive list of methods.
Gists
The following are sample uses for the Gists module.
GitHub.gists('caseyscarborough')
client.gists
GitHub.gists
GitHub.gist(1234567)
client.create_gist(
:files => { "file1.txt" => { content: "File contents" } },
:description => "Gist description.",
:public => "false"
)
client.gist_starred?(1234567)
client.star_gist(1234567)
client.unstar_gist(1234567)
client.fork_gist(1234567)
client.delete_gist(1234567)
Check out the Gist documentation for more information.
Markdown
You can render any markdown test using the markdown method.
GitHub.markdown('# Markdown text!')
GitHub.markdown(File.read('markdown.md'))
Gitignore
You can retrieve .gitignore templates using the Gitignore API as well.
GitHub.gitignore_list
GitHub.gitignore("Ruby")
Authorizations
The Authorizations OAuth API requires that a user use basic authentication. This means that you'll need to instantiate a client using your username and password, as opposed to the username and access token. You'll then be able to use the Authorizations API methods on that client. See below:
client = GitHub.client(login: 'username', password: 'password')
client.authorizations
client.authorization(123)
client.create_authorization(
:note => 'New authorization',
:client_id => 'ab0487b031b18f9286a6',
:client_secret => '9d667c2b7fae7a329f32b6df17926154'
)
client.update_authorization(
123,
:note => 'Updated authorization',
:note_url => 'http://anyrandomurl.com'
)
client.delete_authorization(123)
For a full overview of the Authorizations API check out the OAuth documentation.
Contents API
The Contents API methods are used to retrieve, create, update, and delete files in a repository. File contents that are retrieved are Base64 encoded. Some example methods are shown below:
GitHub.contents('caseyscarborough', 'github')
GitHub.contents(
'caseyscarborough',
'github',
'lib',
'master'
)
client.create_file(
'caseyscarborough',
'github',
:path => 'lib/new_file.txt',
:message => 'Add lib/new_file.txt.',
:content => 'This is a test file.',
:committer => { :name => 'Casey Scarborough', :email => 'casey@example.com' }
)
client.update_file(
'caseyscarborough',
'github',
:path => 'lib/update_file.txt',
:message => 'Update lib/update_file.txt.',
:content => 'This is a test file.',
:sha => '329688480d39049927147c162b9d2deaf885005f',
:committer => { :name => 'Casey Scarborough', :email => 'casey@example.com' }
)
client.delete_file(
'caseyscarborough',
'github',
:path => 'lib/delete_me.txt',
:message => 'Delete lib/delete_me.txt.',
:sha => '329688480d39049927147c162b9d2deaf885005f',
:committer => { :name => 'Casey Scarborough', :email => 'casey@example.com' }
)
As you can see, the Commits API methods are not as intuitive as the rest of the API, so the documentation is very useful.
Conclusion
And if you've made it this far, you can check out the last method to get a little piece of random wisdom from GitHub.
GitHub.octocat
"MMM. .MMM
MMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMM _____________________
MMMMMMMMMMMMMMMMMMMMM | |
MMMMMMMMMMMMMMMMMMMMMMM | Design for failure. |
MMMMMMMMMMMMMMMMMMMMMMMM |_ _________________|
MMMM::- -:::::::- -::MMMM |/
MM~:~ ~:::::~ ~:~MM
.. MMMMM::. .:::+:::. .::MMMMM ..
.MM::::: ._. :::::MM.
MMMM;:::::;MMMM
-MM MMMMMMM
^ M+ MMMMMMMMM
MMMMMMM MM MM MM
MM MM MM MM
MM MM MM MM
.~~MM~MM~MM~MM~~.
~~~~MM:~MM~~~MM~:MM~~~~
~~~~~~==~==~~~==~==~~~~~~
~~~~~~==~==~==~==~~~~~~
:~==~==~==~==~~"
GitHub.octocat('Wisdom.')
To Do
Any functionality of the API listed at developer.github.com that hasn't currently been implemented.
Some main missing functionality:
- A good bit of the Repos API, such as:
- Hooks
- Forks
- Keys
- Downloads
- etc.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request