4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

勢いでgemを作ってみた話

Last updated at Posted at 2019-05-08

はじめに

railsエンジニアの@emono といいます。
rubyを仕事で使っているのに、gemの作り方とは?って感じだったので作ってみました〜。
おととい作り始めて、昨日リリースしました。(完全に勢いでリリースしました。。)

とりあえず最初だったので、日常的に使う lineのgemにしました。
せっかくなので自分が作ったgemの宣伝と(よかったら使ってください〜)、gemの作り方のメモをざっくりまとめます。

作ったgemについて

lineにメッセージが送信できるだけの、めっちゃシンプルなapi_clinetです。
gemのサイト
ぎっとはぶ

使い方

# インストール
$ gem 'line-notify-client'
# 実際にポストする
$ irb 
>> require 'line-notify-client' 
>> token = "さっき作ったtoken"
>> message = "lineに送りたいメッセージ"
>> Line::Notify::Client.message(token: token, message: message)

以上。こんな感じで通知されます〜〜(個人で作っているbotの通知で使える気がする)
S__28033029.jpg

gemの作り方について

以下を参考にしました。
https://qiita.com/9sako6/items/72994b8b1c00af4e61fe
https://guides.rubygems.org/make-your-own-gem/
細けえことはいいからざっくり理解したいんだよって人だけ読み進めてください。

ざっくり作りたいgemの内容と名前を考える

  • gemの名前はsnake_caseがbetterっぽい
  • hoge_hoge → ○ / hoge-hoge → ✗
  • hoge-hogeでも作れるけど、自動生成される内部のディレクトリ構造が深くなっちゃうのでおすすめしない(直そうとしてちょっと詰まった)

名前がかぶらないかを確認しとく + アカウント登録しておく

作る

  • 雛形作る
# これだけ(hoge_hogeはgem名)
$ bundle gem hoge_hoge -t
  • こんな感じで自動生成される(すげえ)

image.png

  • buildするためにgemspecの必須項目を埋めます
hogehoge.gemspec
  spec.email         = ["TODO: Write your email address"] # アドレス
  spec.summary       = %q{TODO: Write a short summary, because RubyGems requires one.} # gemの内容1
  spec.description   = %q{TODO: Write a longer description or delete this line.} # gemの内容2
  spec.homepage      = "TODO: Put your gem's website or public repo URL here." # url(githubのリンクとか)
  • ここらへんを削除(一般公開する用)
hogehoge.gemspec
  # ここらへん削除すると一般公開される
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
  # to allow pushing to a single host or delete this section to allow pushing to any host.
  if spec.respond_to?(:metadata)
    spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
  else
    raise "RubyGems 2.0 or newer is required to protect against " \
      "public gem pushes."
  end
  • 実装がんばる
    • lib配下にあるので頑張ってください
  • 確認 & 実行
    • bin/console で確認出来ます(railsで言うところのreload!が出来ないので誰か助けてください)
  • テスト書く
    • spec配下にあるので頑張ってください
    • bundle exec rake spec で走らせられます。
  • 公開
  • build
    • rake build
  • 公開
    • rake release
  • 多分これで公開されてる

細かいことは↓
https://guides.rubygems.org/make-your-own-gem/
https://qiita.com/9sako6/items/72994b8b1c00af4e61fe

おわりに

記事作成時点で173ダウンロードでした。
昨日リリースしたばかりなのに誰かが使ってくれていて嬉しい。
image.png
クソコードを世に出してしまったので、指摘ください。あとgem作るの楽しい。以上!!!あざした!!!

4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?