Project

moca_rlibs

0.0
No release in over a year
A toolset for ruby and rails.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Project Readme

Ruby用ツールセット

Build Status codecov

注意

grape 1.6.1以降のバージョンのみ使用可能

インストール

gem 'moca_rlibs'

Dockerコンテナ内かどうかを判別する

MocaRlibs::Docker.running_on_docker?

CodeBuild上で"直接"実行されているかを判別する。

MocaRlibs::CodeBuild.running_on_codebuild?

ActiveModelカスタムバリデーション

半角アルファベットのみを許可する

class UserModel
  include ActiveModel::Validations

  validates :user_name, only_alpha: true
end

半角アルファベットと数字のみを許可する

class UserModel
  include ActiveModel::Validations

  validates :user_name, only_alpha_numeric: true
end

数字のみの文字列を許可する

class UserModel
  include ActiveModel::Validations

  validates :user_name, only_numbers: true
end

アスキー文字のみの文字列を許可する

class UserModel
  include ActiveModel::Validations

  validates :user_name, only_ascii: true
end

出力可能なアスキー文字のみかどうかをチェックする(半角英数字、一部の記号)

class UserModel
  include ActiveModel::Validations

  validates :user_name, only_printable: true
end

メールアドレスのフォーマットチェック https://github.com/K-and-R/email_validator

class UserModel
  include ActiveModel::Validations

  validates :user_email, email: true
  validates :user_email, email: { mode: :strict, require_fqdn: true }
  validates :user_email, email: { domain: 'example.com' }
end

IPアドレスのフォーマットをチェックする

class UserModel
  include ActiveModel::Validations

  validates :last_login_ip, ip: true
  validates :last_login_ip, ip: { version: :v4 }
  validates :last_login_ip, ip: { version: :v6 }
end

Grapeカスタムバリデーション

メールアドレスのフォーマットチェック(簡易チェック)

params do
  requires :email, type: String, email: true
end

メールアドレスのフォーマットチェック(厳密チェック)(推奨)

params do
  requires :email, type: String, email_strict: true
end

メールアドレスのフォーマットチェック(RFC-2822, RFC-5321)

params do
  requires :email, type: String, email_rfc: true
end

URLのフォーマットチェック

params do
  requires :url, type: String, url: true
end

文字列の長さ制限

params do
  requires :name, type: String, length: 4
  requires :bio,  type: String, length: 4..512
end

プレフィックス制限

params do
  requires :website, type: String, start_with: "https://"
  requires :website, type: String, start_with: %w(http:// https://)
end

サフィックス制限

params do
  requires :price, type: String, end_with: "JPY"
  requires :price, type: String, end_with: %w(JPY USD)
end

数値の最大値制限

params do
  requires :level, type: Integer, max: 5
end

数値の最大値制限(Proc)

params do
  requires :level, type: Integer, max: ->(params) { params[:foo] + 1 }
end

数値の最小値制限

params do
  requires :age, type: Integer, min: 0
end

数値の最小値制限(Proc)

params do
  requires :level, type: Integer, min: ->(params) { params[:bar] - 1 }
end

アスキー文字しか含まないこと

params do
  requires :text, type: String, only_ascii: true
end

文字列にアルファベットしか含まないこと

params do
  requires :text, type: String, only_alpha: true
end

文字列に数字しか含まれないこと

params do
  requires :text, type: String, only_numbers: true
end

文字列に英数字しか含まれないこと

params do
  requires :text, type: String, only_alpha_numeric: true
end

出力可能なアスキー文字しか含まれないこと(半角英数字、一部の表示可能な記号)

params do
  requires :text, type: String, only_printable: true
end

IPアドレスのフォーマットをチェックする

params do
  optional :ip,   type: String, ip_address: true
  optional :ipv4, type: String, ip_address: :v4
  optional :ipv6, type: String, ip_address: :v6
end

Slack通知送信用クラス

# インスタンス化
client = MocaRlibs::SlackNotifier.new

# シンプルなテキストを送信する
#
# @param [String] message 送信メッセージ
# @param [String] at       メンション先
client.send('some message')

# エラーログを送信
#
# @param [StandardError] err エラーオブジェクト
client.error(StandardError.new('some error occurred'))

# シンプルなテキストを送信する
#
# @param [String] message  送信メッセージ
# @param [String] channel  チャンネル名
# @param [String] username ユーザー名
# @param [String] webhook  Web Hook URL
# @param [String] at       メンション先
MocaRlibs::SlackNotifier.send('some message')

# エラーログを送信
#
# @param [StandardError] err      エラーオブジェクト
# @param [String]        channel  チャンネル名
# @param [String]        username ユーザー名
# @param [String]        webhook  Web Hook URL
MocaRlibs::SlackNotifier.error(StandardError.new('some error occurred'))

ruby拡張(Kernelモジュール)

pythonのpassと同様、「何もしないこと」を明示的に示すため

pass