Octokit


## Octokit

[Octokit](http://octokit.github.io/)は、GitHub APIに手軽にアクセスするラッパープラグインです。

### 公式リポジトリ

* [octokit/octokit.rb · GitHub](https://github.com/octokit/octokit.rb)

### 公式ドキュメント

* [http://rdoc.info/gems/octokit](http://rdoc.info/gems/octokit)

### The Ruby Toolbox

* [https://www.ruby-toolbox.com/gems/octokit](https://www.ruby-toolbox.com/gems/octokit)

### 参考ドキュメント

* [Githubの情報をAPI経由で取得するgem "Octokit" のご紹介](http://tbaba.heroku.com/articles/introduce-octokit)

## インストール

Gemfileにoctokitを記述する。

```ruby:Gemfile
gem 'octokit'
```

インストールする。

```bash
$ bundle install
```

## 利用方法

ユーザー情報を取得する。

```bash
1.9.2p318 :001 > user = Octokit.user("fakestarbaby")

{"followers"=>15,
 "type"=>"User",
 "blog"=>"http://twitter.com/#!/fakestarbaby",
 "created_at"=>"2011-01-19T05:08:56Z",
 "email"=>nil,
 "bio"=>nil,
 "company"=>"Japan",
 "html_url"=>"https://github.com/FakeStarBaby",
 "avatar_url"=>
  "https://secure.gravatar.com/avatar/0e3e4d3a8e69ccae1e9b9dcc219f3f1f?d=https://a248.e.akamai.net/assets.github.com/images/gravatars/gravatar-140.png",
 "url"=>"https://api.github.com/users/FakeStarBaby",
 "public_gists"=>10,
 "following"=>69,
 "login"=>"FakeStarBaby",
 "public_repos"=>52,
 "gravatar_id"=>"0e3e4d3a8e69ccae1e9b9dcc219f3f1f",
 "name"=>"Yoshinori Hirasawa",
 "location"=>"Tokyo",
 "id"=>572020,
 "hireable"=>false}
```

ユーザーインスタンスを取得する。

* パスワード認証の場合

``` bash
1.9.2p318 :002 > user = Octokit::Client.new(:login => "fakestarbaby", :password => "password")
```

* OAuth認証の場合

```bash
1.9.2p318 :002 > user = Octokit::Client.new(:login => "fakestarbaby", :oauth_token => "token")
```

リポジトリ情報を取得する。

``` bash
1.9.2p318 :003 > user.repos.first

{"open_issues"=>0,
 "git_url"=>"git://github.com/FakeStarBaby/sample-octokit.git",
 "svn_url"=>"https://github.com/FakeStarBaby/sample-octokit",
 "pushed_at"=>"2012-04-17T00:15:51Z",
 "created_at"=>"2012-04-17T00:07:14Z",
 "description"=>"octokitのサンプルアプリケーションです。",
 "html_url"=>"https://github.com/FakeStarBaby/sample-octokit",
 "has_downloads"=>true,
 "watchers"=>1,
 "clone_url"=>"https://github.com/FakeStarBaby/sample-octokit.git",
 "url"=>"https://api.github.com/repos/FakeStarBaby/sample-octokit",
 "fork"=>false,
 "size"=>116,
 "homepage"=>"",
 "private"=>false,
 "has_wiki"=>true,
 "has_issues"=>true,
 "updated_at"=>"2012-04-17T00:15:51Z",
 "owner"=>
  {"avatar_url"=>
    "https://secure.gravatar.com/avatar/0e3e4d3a8e69ccae1e9b9dcc219f3f1f?d=https://a248.e.akamai.net/assets.github.com/images/gravatars/gravatar-140.png",
   "url"=>"https://api.github.com/users/FakeStarBaby",
   "login"=>"FakeStarBaby",
   "gravatar_id"=>"0e3e4d3a8e69ccae1e9b9dcc219f3f1f",
   "id"=>572020},
 "name"=>"sample-octokit",
 "permissions"=>{"admin"=>true, "pull"=>true, "push"=>true},
 "mirror_url"=>nil,
 "forks"=>1,
 "ssh_url"=>"git@github.com:FakeStarBaby/sample-octokit.git",
 "id"=>4047257,
 "language"=>"Ruby"}
```

コミット情報を取得する。

```bash
1.9.2p318 :004 > user.list_commits("fakestarbaby/sample-octokit").first

{"commit"=>
  {"url"=>
    "https://api.github.com/repos/FakeStarBaby/sample-octokit/git/commits/ee266e50537b9f27ad4b3ecb39204feec41a5540",
   "message"=>"トップページを新規作成。",
   "author"=>
    {"date"=>"2012-04-16T17:15:41-07:00",
     "name"=>"fakestarbaby",
     "email"=>"fakestarbaby@gmail.com"},
   "committer"=>
    {"date"=>"2012-04-16T17:15:41-07:00",
     "name"=>"fakestarbaby",
     "email"=>"fakestarbaby@gmail.com"},
   "tree"=>
    {"url"=>
      "https://api.github.com/repos/FakeStarBaby/sample-octokit/git/trees/0f1f2ac3e348046a86e46f49415d2834aa4cea0d",
     "sha"=>"0f1f2ac3e348046a86e46f49415d2834aa4cea0d"}},
 "parents"=>
  [{"url"=>
     "https://api.github.com/repos/FakeStarBaby/sample-octokit/commits/b83d80b1f6036565afe155df591b1ad27d54b9c7",
    "sha"=>"b83d80b1f6036565afe155df591b1ad27d54b9c7"}],
 "url"=>
  "https://api.github.com/repos/FakeStarBaby/sample-octokit/commits/ee266e50537b9f27ad4b3ecb39204feec41a5540",
 "sha"=>"ee266e50537b9f27ad4b3ecb39204feec41a5540",
 "author"=>
  {"url"=>"https://api.github.com/users/FakeStarBaby",
   "login"=>"FakeStarBaby",
   "gravatar_id"=>"0e3e4d3a8e69ccae1e9b9dcc219f3f1f",
   "avatar_url"=>
    "https://secure.gravatar.com/avatar/0e3e4d3a8e69ccae1e9b9dcc219f3f1f?d=https://a248.e.akamai.net/assets.github.com/images/gravatars/gravatar-140.png",
   "id"=>572020},
 "committer"=>
  {"url"=>"https://api.github.com/users/FakeStarBaby",
   "login"=>"FakeStarBaby",
   "gravatar_id"=>"0e3e4d3a8e69ccae1e9b9dcc219f3f1f",
   "avatar_url"=>
    "https://secure.gravatar.com/avatar/0e3e4d3a8e69ccae1e9b9dcc219f3f1f?d=https://a248.e.akamai.net/assets.github.com/images/gravatars/gravatar-140.png",
   "id"=>572020}}
```