Rubygems in the making, First Step!
In this opportunity, I want to share how to make Rubygems study case and the name is Watesan
.
Watesan is a name taken from Javanese language (my hometown) which mean limit
- just intermezzo guys 😄
So, firstly let’s build our own rubygems
Build own rubygems
1. Generate with boilerplate from rubygems
In the first step don’t be confused about how to make rubygems, because there (bundle-gem) is a boilerplate for us to make some gem.
bundle gem watesan --coc --mit -t=rspec --ci=github --linter=rubocop
command in the above is the first command for how to build rubygems. here is the explanation.
--coc
: Add a CODE_OF_CONDUCT.md file to the root of the generated project. If this option is unspecified, an interactive prompt will be displayed and the answer will be saved in Bundler´s global config for future bundle gem use.--mit
: Add an MIT license to a LICENSE.txt file in the root of the generated project. Your name from the global git config is used for the copyright statement. If this option is unspecified, an interactive prompt will be displayed and the answer will be saved in Bundler´s global config for future bundle gem use.-t
: Specify the test framework that Bundler should use when generating the project and in this case I chooserspec
--ci
: Specify the continuous integration service that Bundler should use when generating the project and in this case I choosegithub
because i want to save this repository atGithub
—-linter
: Specify the linter and code formatter that Bundler should add to the project´s development dependencies and I chooserubocop
and after you execute the first command you can see the directory
❯ tree watesan
watesan
├── CHANGELOG.md
├── CODE_OF_CONDUCT.md
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── bin
│ ├── console
│ └── setup
├── lib
│ ├── watesan
│ │ └── version.rb
│ └── watesan.rb
├── sig
│ └── watesan.rbs
├── spec
│ ├── spec_helper.rb
│ └── watesan_spec.rb
└── watesan.gemspec
6 directories, 14 files
here is the explanation (I will explain just for the important and workspace when we develop rubygems) :
Gemfile
: specifies the gem dependencies required by the project.lib/
: almost code has been written in this place.spec/
: the place for unit test.watesan.gemspec
: it is used to define metadata and dependencies for a rubygems it self.
2. Configure .gemspec
before we start writing code, we must configure the .gemspec first. So, you can write by following the instruction, don’t forget to create account at Rubygems. and here my own .gemspec
:
# frozen_string_literal: true
require_relative 'lib/watesan/version'
Gem::Specification.new do |spec|
spec.name = 'watesan'
spec.version = Watesan::VERSION
spec.authors = ['Hammam Firdaus']
spec.email = ['hammamxalf@gmail.com']
spec.summary = 'Gem for Rate Limiter using Redis'
spec.description = 'Gem for Rate Limiter using Redis'
spec.homepage = '<https://github.com/mamxalf>'
spec.license = 'MIT'
spec.required_ruby_version = '>= 2.6.0'
# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server '<https://example.com>'"
#
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "<https://github.com/mamxalf/watesan>"
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(__dir__) do
`git ls-files -z`.split("\\x0").reject do |f|
(f == __FILE__) || f.match(%r{\\A(?:(?:bin|test|spec|features)/|\\.(?:git|circleci)|appveyor)})
end
end
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{\\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']
# Uncomment to register a new dependency of your gem
spec.add_dependency 'redis', '~> 5.0', '>= 5.0.6'
spec.metadata['rubygems_mfa_required'] = 'true'
end
3. Build and Push Rubygems
after we have configured about the gemspec, you can to next step to build the gem.
❯ gem build watesan.gemspec
WARNING: description and summary are identical
WARNING: See <https://guides.rubygems.org/specification-reference/> for help
Successfully built RubyGem
Name: watesan
Version: 0.1.0
File: watesan-0.1.0.gem
command above will generate file with format .gem
and with this file you can put this library into rubygems. after watesan-0.1.0.gem
generated you can push (upload) with this command.
❯ gem push watesan-0.1.0.gem
Enter your RubyGems.org credentials.
Don't have an account yet? Create one at <https://rubygems.org/sign_up>
Email: hammamxalf@gmail.com
Password:
Signed in with API key: MacBooks-MacBook-Air.local-mamxalf-20230523192526.
Pushing gem to <https://rubygems.org>...
Rubygem requires owners to enable MFA. You must enable MFA before pushing new version.
if you get result same as above you can create or integrate Multi-factor authentication
first. and in this case i'm use Authy
. after you configured the MFA
you can retry to push the gem.
❯ gem push watesan-0.1.0.gem
Pushing gem to <https://rubygems.org>...
You have enabled multi-factor authentication. Please enter OTP code.
Code: 416545
Successfully registered gem: watesan (0.1.0)
After you got a message Successfully registered gem
you can check in your rubygems profile. and congratulation you have built your first ruby gems.
After this article i will write rubygems for rate limiter backed by Redis
.
Bye bye 👋