You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Supports all the API methods(nearly 200). It's build in a modular way, that is, you can either instantiate the whole api wrapper Github.new or use parts of it e.i. Github::Repos.new if working solely with repositories is your main concern.
Features
Intuitive GitHub API interface navigation.
Modular design allows for working with parts of API.
Fully customizable including advanced middleware stack construction.
Its comprehensive, you can request all GitHub API resources.
Flexible arguments parsing, you can write expressive and natural queries.
Requests pagination with convenient DSL and automatic option.
Easy error handling split for client and server type errors.
Supports multithreaded environment.
Custom mime types specification (Status: In Progess)
Request results caching (Status: TODO)
Fully tested with test coverage above 90% with over 1,500 specs and 800 features.
Installation
Install the gem by issuing
geminstallgithub_api
or put it in your Gemfile and run bundle install
gem"github_api"
Usage
To start using the gem you can either perform direct call on the Github
At this stage you can also supply various configuration parameters, such as
adapter # http client used for performing requests
auto_pagination # by default false, set to true traverses requests page links
oauth_token # oauth authorization token
basic_auth # login:password string
client_id # oauth client id
client_secret # oauth client secret
user # global user used in requets if none provided
repo # global repository used in requests in none provided
org # global organization used in request if none provided
endpoint # enterprise api endpoint
site # enterprise api web endpoint
ssl # ssl settings
per_page # number of items per page, max 100
user_agent # custom user agent name, by default 'Github API'
which are used throughout the API. These can be passed directly as hash options:
github=Github.newoauth_token: 'token'
Alternatively, you can configure the Github settings by passing a block, for instance, with custom enterprise endpoint and website like
You can authenticate either using OAuth authentication convenience methods(see section OAuth) or through basic authentication by passing your login and password credentials
This gem closely mirros the GitHub Api hierarchy e.i. if you want to create a download resource,
lookup the github api spec and issue the request as in github.repos.downloads.create
For example to interact with GitHub Repositories API, issue the following calls that correspond directly to the GitHub API hierarchy
Further, arguments can be passed directly inside method called but then the order of parameters matters and hence please consult the method documentation or GitHub specification. For instance:
Some API methods apart from required parameters such as username, repository name
or organisation name, allow you to switch the way the data is returned to you, for instance
github=Github.newgithub.git_data.trees.get'peter-murach','github','c18647b75d72f19c1e0cc8af031e5d833b7f12ea'# => gets a treegithub.git_data.trees.get'peter-murach','github','c18647b75d72f19c1e0cc8af031e5d833b7f12ea',recursive: true# => gets a whole tree recursively
by passing a block you can iterate over the file tree
The github_api gem will use the default middleware stack which is exposed by calling stack on client instance. However, this stack can be freely modified with methods such as insert, insert_after, delete and swap. For instance to add your CustomMiddleware do
Some parts of GitHub API v3 require you to be autheticated, for instance the following are examples of APIs only for the authenticated user
Github::Users::EmailsGithub::Users::Keys
All method calls form ruby like sentences and allow for intuitive api navigation, for instance
github=Github.new:oauth_token=>'...'github.users.followers.following'wycats'# => returns users that 'wycats' is followinggithub.users.followers.following'wycats'# => returns true if following, otherwise false
For specification on all available methods go to http://developer.github.com/v3/ or
read the rdoc, all methods are documented there with examples of usage.
Alternatively, you can find out supported methods by calling actions on a class instance in your irb:
After you get your authorization code, call to receive your access_token
token=github.get_token(authorization_code)
Once you have your access token, configure your github instance following instructions under Configuration.
Authorizations API
Alternatively you can use OAuth Authorizations API. For instance, to create access token through GitHub API you required to pass your basic credentials as in the following:
You can add more than one scope from the user, public_repo, repo, gist or leave the scopes parameter out, in which case, the default read-only access will be assumed(includes public user profile info, public repo info, and gists).
For instance, download CA root certificates from Mozilla cacert and point ca_file at your certificate bundle location.
This will allow the client to verify the github.com ssl certificate as authentic.
MIME Types
Issues, PullRequests and few other API leverage custom mime types which are :json, :blob, :raw, :text, :html, :full. By default :raw is used.
Your header will contain 'Accept: "application/vnd.github-pull.full+json"' which in turn returns raw, text and html representations in response body.
Configuration
Certain methods require authentication. To get your GitHub OAuth v2 credentials,
register an app at https://github.com/settings/applications/
You will need to be logged in to register the application.
All parameters can be overwirtten as per method call. By passing parameters hash...
By default no caching will be performed. In order to set the cache do... If no cache type is provided a default memoization is done.
Pagination
Any request that returns multiple items will be paginated to 30 items by default. You can specify custom page and per_page query parameters to alter default behavior. For instance:
Then you can query pagination information included in the link header by:
res.links.first# Shows the URL of the first page of results.res.links.next# Shows the URL of the immediate next page of results.res.links.prev# Shows the URL of the immediate previous page of results.res.links.last# Shows the URL of the last page of results.
In order to iterate through the entire result set page by page, you can use convenience methods:
Alternatively, you can retrieve all pages in one invocation by passing auto_pagination option like so:
github=Github.newauto_pagination: true
Depending at what stage you pass the auto_pagination it will affect all or only single request:
Github::Repos.newauto_pagination: true# affects Repos part of APIGithub::Repos.new.listuser: '...',auto_pagination: true# affects single request
One can also navigate straight to specific page by:
res.count_pages# Number of pagesres.page5# Requests given page if it exists, nil otherwiseres.first_page# Get first pageres.next_page# Get next pageres.prev_page# Get previous pageres.last_page# Get last page
Error Handling
The generic error class Github::Error::GithubError will handle both the client(Github::Error::ClientError) and service(Github::Error::ServiceError) side errors. For instance in your code you can catch erros like
begin# Do something with github_api gemrescueGithub::Error::GithubError=>eputse.messageife.is_a?Github::Error::ServiceError# handle GitHub service errors such as 404elsife.is_a?Github::Error::ClientError# handle client errors e.i. missing required parameter in requestendend
Response Message
Each response comes packaged with methods allowing for inspection of HTTP start line and headers. For example to check for rate limits and status code issue
The test suite is split into two groups live and mock.
The live tests are the ones in features folder and they simply exercise the GitHub API by making live requests and then being cached with VCR in directory named features\cassettes. For details on how to get setup please navigate to features folder.
The mock tests are in spec directory and their primary concern is to test the gem internals without the hindrance of external calls.
Development
Questions or problems? Please post them on the issue tracker. You can contribute changes by forking the project and submitting a pull request. You can ensure the tests are passing by running bundle and rake.
Copyright
Copyright (c) 2011-2013 Piotr Murach. See LICENSE.txt for further details.