LoginSignup
1
0

More than 5 years have passed since last update.

ゴリラでもできる!RubyGem開発【CIを使ってGemを開発してみた】

Last updated at Posted at 2019-01-08

Gemについて:gem:

GemはRubyGemsが公開しているライブラリのこと。
RubyGemsはRuby用のパッケージ管理ツールで、ライブラリの作成や公開、インストールを助けるシステム。
要は、誰かが作った先人の知恵

railsとかもgem
https://rubygems.org/gems/rails

今回作ったGem

一般的な人が利用しているのはRGB色空間 R:赤, G:緑, B:青
この3つの要素で色を表しているが、実際に輝度成分や彩度などが知りたい場合には、適した色空間に変更する必要がある。XYZ色空間, LAB色空間 etc...。
その色空間の変換をしてくれるGemであるColorSpaceConverterを作成しました!
https://github.com/TaigaMikami/color_space_converter

使ったCI:cop:

Travis CI

https://travis-ci.org/
設定したGitHubリポジトリから自動でソースコードをチェックアウトしてあらかじめ指定しておいたビルドやテスト処理を実行する
テスト結果をTravis CIのサイト上や各種API、メールなどで開発者に通知する

Coveralls

https://coveralls.io/
Coverallsはプログラムのテストなどを行った時に、 どれ位の範囲が実際にテストされているか調べて表示するサービス

Gemの作り方:pencil2:

テンプレート作成

ターミナルで

$ bundle gem hoge(Gem名)

テストはRSpec
MITライセンス
で作成した。

gemspecの編集

hoge.gemspecファイルの修正
buildすると注意される5つの項目があるのでそこを修正する

あとは、作成するGemに必要なGemもこのファイルに記載する

gemspec
spec.add_development_dependency "rspec", "~> 3.0"

ここに記載しておくと、 Gemfileにインポートされているので bundle install の際にインストールしてくれる

Gemfile
source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gemspec <ー ここにあるから、インポートされる
$ bundle install

色空間の変換とテスト

ColorSpaceConverter実装例

lib/color_space_converterディレクトリを作成し、
計算用のmoduleであるcompute.rbファイルを作成する。

ディレクトリ構成は以下を参照してください。
https://github.com/TaigaMikami/color_space_converter/tree/master/lib/color_space_converter

例えば、RGB値をXYZ値に変換プログラムを作成してみる

compute.rb
module ColorSpaceConverter
  module Compute
    def rgb2xyz(r, g, b)
      s_rgb = []
      [r, g, b].each do |n|
        n = n.to_f / 255
        if n <= 0.0405
          s_n = n / 12.92
        else
          s_n = ((n+0.055) / 1.055) ** 2.4
        end
        s_rgb << s_n
      end

      x = s_rgb[0]*0.4124 + s_rgb[1]*0.3576 + s_rgb[2]*0.1805
      y = s_rgb[0]*0.2126 + s_rgb[1]*0.7152 + s_rgb[2]*0.0722
      z = s_rgb[0]*0.0193+ s_rgb[1]*0.1192+ s_rgb[2]*0.9505
      [x*100, y*100, z*100]
    end
  end
end

テストをspec/color_space_converter/compute_spec.rbに作成

compute_spec.rb
require 'spec_helper'

RSpec.describe ColorSpaceConverter::Compute do
  let(:cs) { ColorSpaceConverter::Compute }

  context '#rgb2xyz' do
    subject { cs.rgb2xyz(128, 128, 128)}
    it { expect(subject).to eq([20.51754053582612, 21.586050011389922, 23.507208462403632]) }
  end
end
$ bubndle exec rspec

でテストが通るか確認する。

CI設定

Travis CI・coverallsについて

travis.yml
---
sudo: false
language: ruby
cache: bundler
rvm: |
  - 2.5.3
before_install: gem install bundler -v 1.17.2
coveralls.yml
service_name: travis-pro
repo_token: xxxxxxxxxxxxxxxxxxx <ー coverallsにリポジトリ登録したら確認できる

困ったら、以下を参考にしよう!
https://morizyun.github.io/blog/travis-ci-code-climate-rubygem-org-coverall-gemnusium-inch-ci/index.html

rubocop

コーディング規約に従ってきれいなコードを書きたかったので、静的コード解析ツールとしてrubocopを入れた

以下を追加した。
https://github.com/TaigaMikami/color_space_converter/commit/c32c93e870b9eec9e9d4c556b47b431f23e443a0

最後の方に追記

travis.yml
script: rubocop --fail-level=W

READMEにバッジを書く

そしたらREADMEにバッジを追加して、GitHubにpushすると以下のようになるはず!

README.md
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/TaigaMikami/color_space_converter/blob/master/LICENSE.txt)
[![Build Status](https://travis-ci.org/TaigaMikami/color_space_converter.svg?branch=master)](https://travis-ci.org/TaigaMikami/color_space_converter)
[![Coverage Status](https://coveralls.io/repos/github/TaigaMikami/color_space_converter/badge.svg?branch=master)](https://coveralls.io/github/TaigaMikami/color_space_converter?branch=master)
[![Gem Version](https://badge.fury.io/rb/color_space_converter.svg)](https://badge.fury.io/rb/color_space_converter)

スクリーンショット 2019-01-08 22.39.03.png

Travisでは、以下のようにテストの結果が確認できる

スクリーンショット 2019-01-08 22.42.16.png

Coverallsでは、以下のように自分が書いたテストがどのくらい網羅しているかが確認できる

スクリーンショット 2019-01-08 22.41.24.png

Gemを公開する:gift:

RubyGemsに登録 APIkey発行

RubyGems.orgでアカウントを作成し、

Edit profile で curl -u hogehoge https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials; chmod 0600 ~/.gem/credentials をコピペする。
スクリーンショット 2019-01-08 22.46.27.png

公開する

buildして

$ bundle exec rake build

release

$ bundle exec rake release

そうするとこんな感じでリリースできる:tada:
スクリーンショット 2019-01-08 22.50.17.png

参考:bow:

作ろうとしてる内容が近かったので、めちゃくちゃ参考にした。
https://www.mk-mode.com/octopress/2016/07/12/ruby-calendar-calculation-by-my-gem/
https://morizyun.github.io/blog/ruby-gem-easy-publish-library-rails/index.html

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0