Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

github

> 开发工具
Open source

Ruby interface to GitHub API

1.2K stars0 likes2 views
WebsiteGitHub

About

Ruby interface to GitHub API

GithubAPI

Website | Wiki | RDocs

A Ruby client for the official GitHub API.

Supports all the API methods. It's built in a modular way. You can either instantiate the whole API wrapper Github.new or use parts of it i.e. Github::Client::Repos.new if working solely with repositories is your main concern. Intuitive query methods allow you easily call API endpoints.

Features

  • Intuitive GitHub API interface navigation.
  • It's comprehensive. You can request all GitHub API resources.
  • Modular design allows for working with parts of API.
  • Fully customizable including advanced middleware stack construction.
  • Supports OAuth2 authorization.
  • Flexible argument parsing. You can write expressive and natural queries.
  • Requests pagination with convenient DSL and automatic options.
  • Easy error handling split for client and server type errors.
  • Supports multithreaded environment.
  • Custom media type specification through the 'media' parameter.
  • Request results caching
  • Fully tested with unit and feature tests hitting the live api.

Installation

Install the gem by running

ruby
gem install github_api

or put it in your Gemfile and run bundle install

ruby
gem "github_api"

Contents

  • 1. Usage
    • 1.1 API Navigation
    • 1.2 Modularity
    • 1.3 Arguments
    • 1.4 Response Querying
      • 1.4.1 Response Body
      • 1.4.2 Response Headers
      • 1.4.3 Response Success
    • 1.5 Request Headers
      • 1.5.1 Media Types
  • 2. Configuration
    • 2.1 Basic
    • 2.2 Advanced
    • 2.3 SSL
    • 2.4 Caching
  • 3. Authentication
    • 3.1 Basic
    • 3.2 Authorizations API
    • 3.3 Scopes
    • 3.4 Application OAuth
    • 3.5 Two-Factor
  • 4. Pagination
    • 4.1 Auto pagination
  • 5. Error Handling
    • 5.1 Client Error
    • 5.2 Service Error
      • 5.2.1 Data
      • 5.2.2 Error Messages
  • 6. Examples
    • 6.1 Rails
    • 6.2 Manipulating Files
  • 7. Testing

1 Usage

To start using the gem, you can either perform requests directly on Github namespace:

ruby
Github.repos.list user: 'piotrmurach'

or create a new client instance like so

ruby
github = Github.new

and then call api methods, for instance, to list a given user repositories do

ruby
github.repos.list user: 'piotrmurach'

1.1 API Navigation

The github_api closely mirrors the GitHub API hierarchy. For example, if you want to create a new file in a repository, look up the GitHub API spec. In there you will find contents sub category underneath the repository category. This would translate to the request:

ruby
github = Github.new
github.repos.contents.create 'piotrmurach', 'finite_machine', 'hello.rb',
                             path: 'hello.rb',
                             content: "puts 'hello ruby'"

The whole library reflects the same api navigation. Therefore, if you need to list releases for a repository do:

ruby
github.repos.releases.list 'piotrmurach', 'finite_machine'

or to list a user's followers:

ruby
github.users.followers.list 'piotrmurach'

The code base has been extensively documented with examples of how to use each method. Please refer to the documentation under the Github::Client class name.

Alternatively, you can find out which methods are supported by an api by calling actions on a class or instance. For example, in order to find out available endpoints for Github::Client::Repos::Contents api call actions method:

ruby
Github::Client::Repos::Contents.actions
=> [:archive, :create, :delete, :find, :get, :readme, :update]

1.2 Modularity

The code base is modular. This means that you can work specifically with a given part of GitHub API. If you want to only work with activity starring API do the following:

ruby
starring = Github::Client::Activity::Starring.new oauth_token: token
starring.star 'piotrmurach', 'github'

Please refer to the documentation and look under Github::Client to see all available classes.

1.3 Arguments

The github_api library allows for flexible argument parsing.

Arguments can be passed directly inside the method called. The required arguments are passed in first, followed by optional parameters supplied as hash options:

ruby
issues = Github::Client::Issues.new
issues.milestones.list 'piotrmurach', 'github', state: 'open'

In the previous example, the order of arguments is important. However, each method also allows you to specify required arguments using hash symbols and thus remove the need for ordering. Therefore, the same example could be rewritten like so:

ruby
issues = Github::Client::Issues.new
issues.milestones.list user: 'piotrmurach', repo: 'github', state: 'open'

Furthermore, required arguments can be passed during instance creation:

ruby
issues = Github::Client::Issues.new user: 'piotrmurach', repo: 'github'
issues.milestones.list state: 'open'

Similarly, the required arguments for the request can be passed inside the current scope such as:

ruby
issues = Github::Client::Issues.new
issues.milestones(user: 'piotrmurach', repo: 'github').list state: 'open'

But why limit ourselves? You can mix and match arguments, for example:

ruby
issues = Github::Client::Issues.new user: 'piotrmurach'
issues.milestones(repo: 'github').list
issues.milestones(repo: 'tty').list

You can also use a bit of syntactic sugar whereby "username/repository" can be passed as well:

ruby
issues = Github::Client::Issues.new
issues.milestones('piotrmurach/github').list
issues.milestones.list 'piotrmurach/github'

Finally, use the with scope to clearly denote your requests

ruby
issues = Github::Client::Issues.new
issues.milestones.with(user: 'piotrmurach', repo: 'github').list

Please consult the method documentation or GitHub specification to see which arguments are required and what are the option parameters.

1.4 Response Querying

The response is of type Github::ResponseWrapper and allows traversing all the json response attributes like method calls. In addition, if the response returns more than one resource, these will be automatically yielded to the provided block one by one.

For example, when request is issued to list all the branches on a given repository, each branch will be yielded one by one:

ruby
repos = Github::Client::Repos.new
repos.branches user: 'piotrmurach', repo: 'github' do |branch|
  puts branch.name
end

1.4.1 Response Body

The ResponseWrapper allows you to call json attributes directly as method calls. there is no magic here, all calls are delegated to the response body. Therefore, you can directly inspect request body by calling body method on the ResponseWrapper like so:

ruby
response = repos.branches user: 'piotrmurach', repo: 'github'
response.body  # => Array of branches

1.4.2 Response Headers

Each response comes packaged with methods allowing for inspection of HTTP start line and headers. For example, to check for rate limits and status codes do:

ruby
response = Github::Client::Repos.branches 'piotrmurach', 'github'
response.headers.ratelimit_limit     # "5000"
response.headers.ratelimit_remaining # "4999"
response.headers.status              # "200"
response.headers.content_type        # "application/json; charset=utf-8"
response.headers.etag                # "\"2c5dfc54b3fe498779ef3a9ada9a0af9\""
response.headers.cache_control       # "public, max-age=60, s-maxage=60"

1.4.3 Response Success

If you want to verify if the response was success, namely, that the 200 code was returned call the success? like so:

ruby
response = Github::Client::Repos.branches 'piotrmurach', 'github'
response.success?  # => true

1.5 Request Headers

It is possible to specify additional header information which will be added to the final request.

For example, to set etag and X-Poll_Interval headers, use the :headers hash key inside the :options hash like in the following:

ruby
events = Github::Client::Activity::Events.new
events.public headers: {
    'X-Poll-Interval': 60,
    'ETag': "a18c3bded88eb5dbb5c849a489412bf3"
  }

1.5.1 Media Types

In order to set custom media types for a request use the accept header. By using the :accept key you can determine media type like in the example:

ruby
issues = Github::Client::Issues.new
issues.get 'piotrmurach', 'github', 108, accept: 'application/vnd.github.raw'

2 Configuration

The github_api provides ability to specify global configuration options. These options will be available to all api calls.

2.1 Basic

The configuration options can be set by using the configure helper

ruby
Github.configure do |c|
  c.basic_auth = "login:password"
  c.adapter    = :typheous
  c.user       = 'piotrmurach'
  c.repo       = 'finite_machine'
end

Alternatively, you can configure the settings by passing a block to an instance like:

ruby
Github.new do |c|
  c.endpoint    = 'https://github.company.com/api/v3'
  c.site        = 'https://github.company.com'
  c.upload_endpoint = 'https://github.company.com/api/uploads'
end

or simply by passing hash of options to an instance like so

ruby
github = Github.new basic_auth: 'login:password',
                    adapter: :typheous,
                    user: 'piotrmurach',
                    repo: 'finite_machine'

The following is the full list of available configuration options:

…

2.2 Advanced

The github_api will use the default middleware stack which is exposed by calling stack on a 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:

ruby
Github.configure do |c|
  c.stack.insert_after Github::Response::Helpers, CustomMiddleware
end

Furthermore, you can build your entire custom stack and specify other connection options such as adapter by doing:

ruby
Github.new do |c|
  c.adapter :excon

  c.stack do |builder|
    builder.use Github::Response::Helpers
    builder.use Github::Response::Jsonize
  end
end

2.3 SSL

By default requests over SSL are set to OpenSSL::SSL::VERIFY_PEER. However, you can turn off peer verification by

ruby
github = Github.new ssl: { verify: false }

If your client fails to find CA certs, you can pass other SSL options to specify exactly how the information is sourced

ruby
ssl: {
  client_cert: "/usr/local/www.example.com/client_cert.pem"
  client_key:  "/user/local/www.example.com/client_key.pem"

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyapi-clientapi-navigationapi-wrappergithub-api

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具