rubygems の作成

# 概要
rubygems の作成方法

# 手順

## bundler インストール
```
% gem install bundler
Fetching: bundler-2.0.1.gem (100%)
Successfully installed bundler-2.0.1
Parsing documentation for bundler-2.0.1
Installing ri documentation for bundler-2.0.1
Done installing documentation for bundler after 4 seconds
1 gem installed

% gem update bundler
Updating installed gems
Nothing to update　
```

## 雛形の作成
```
% bundle gem sample_gems
Creating gem 'sample_gems'...
Do you want to include a code of conduct in gems you generate?
Codes of conduct can increase contributions to your project by contributors who prefer collaborative, safe spaces. You can read more about the code of conduct at contributor-covenant.org. Having a code of conduct means agreeing to the responsibility of enforcing it, so be sure that you are prepared to do that. Be sure that your email address is specified as a contact in the generated code of conduct so that people know who to contact in case of a violation. For suggestions about how to enforce codes of conduct, see https://bit.ly/coc-enforcement. y/(n):
      create  sample_gems/Gemfile
      create  sample_gems/lib/sample_gems.rb
      create  sample_gems/lib/sample_gems/version.rb
      create  sample_gems/sample_gems.gemspec
      create  sample_gems/Rakefile
      create  sample_gems/README.md
      create  sample_gems/bin/console
      create  sample_gems/bin/setup
      create  sample_gems/.gitignore
Initializing git repo in /Users/yuhori/Vagrant/centos7/sample_gems
Gem 'sample_gems' was successfully created. For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html
```

## コードを書く
次のファイルを編集

```
% cat lib/sample_gems.rb
require "sample_gems/version"

module SampleGems
  class Error < StandardError; end
  # Your code goes here...
end
```

今回は、受け取った文字列を装飾して返却するだけのメソッドを追加した。

```
% cat lib/sample_gems.rb
require "sample_gems/version"

module SampleGems
  class Error < StandardError; end

  def self.bold(msg)
    "**#{msg}**"
  end
end
```

## 必要ファイルを揃える
```
% bundle install                                                                      (git)-[master]
Fetching gem metadata from https://rubygems.org/.
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Fetching rake 10.5.0
Installing rake 10.5.0
Using bundler 2.0.1
Using sample_gems 0.1.0 from source at `.`
Bundle complete! 3 Gemfile dependencies, 3 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
```

## gemspec を修正
TODO が残っていると、うまくいかない

```
% rake build                                                                          (git)-[master]
rake aborted!
WARNING:  See http://guides.rubygems.org/specification-reference/ for help
ERROR:  While executing gem ... (Gem::InvalidSpecificationException)
    metadata['homepage_uri'] has invalid link: "TODO: Put your gem's website or public repo URL here."

Tasks: TOP => build
(See full trace by running task with --trace)
```

そのため、TODO の部分を修正する。

```
% rake build                                                                          (git)-[master]
sample_gems 0.1.0 built to pkg/sample_gems-0.1.0.gem.
```

## 動作確認
bin/console を起動して、動作確認ができる。

```
% ./bin/console                                                                       (git)-[master]
irb(main):001:0> SampleGems.bold("G")
=> "**G**"
```

問題なく動作していそう。

## build
```
% rake build                                                                          (git)-[master]
sample_gems 0.1.0 built to pkg/sample_gems-0.1.0.gem.
```

gem が作成できた。

## 公開
https://rubygems.org/ のアカウントを作成し、コマンドによってAccess Key を手に入れる。

```
% curl -u <user_name> https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials; chmod 0600 ~/.gem/credentials
```

## github にpush

```
% git add .
% git cm -m "initial"
% git remote add origin git@github.com:YuHori/sample_gems.git
% git push origin master                                                              (git)-[master]
Enumerating objects: 17, done.
Counting objects: 100% (17/17), done.
Delta compression using up to 4 threads
Compressing objects: 100% (14/14), done.
Writing objects: 100% (17/17), 3.12 KiB | 1.04 MiB/s, done.
Total 17 (delta 0), reused 0 (delta 0)
To github.com:YuHori/sample_gems.git
 * [new branch]      master -> master
```

## rubygems にpush
```
% rake release                                                                        (git)-[master]
sample_gems 0.1.0 built to pkg/sample_gems-0.1.0.gem.
Tag v0.1.0 has already been created.
Pushed sample_gems 0.1.0 to rubygems.org
```

これで、以下のようにgems が作成できた。
https://rubygems.org/gems/sample_gems

## 使ってみる
```
% gem install sample_gems                                                             (git)-[master]
Fetching: sample_gems-0.1.0.gem (100%)
Successfully installed sample_gems-0.1.0
Parsing documentation for sample_gems-0.1.0
Installing ri documentation for sample_gems-0.1.0
Done installing documentation for sample_gems after 0 seconds
1 gem installed
```