Module: Israeli

Defined in:
lib/israeli.rb,
lib/israeli/luhn.rb,
lib/israeli/errors.rb,
lib/israeli/result.rb,
lib/israeli/railtie.rb,
lib/israeli/version.rb,
lib/israeli/sanitizer.rb,
lib/israeli/validators/id.rb,
lib/israeli/validators/phone.rb,
lib/israeli/validators/postal_code.rb,
lib/israeli/validators/bank_account.rb

Overview

Main namespace for the Israeli validators gem.

Provides validation utilities for Israeli identifiers including ID numbers (Mispar Zehut), postal codes, phone numbers, and bank accounts.

Examples:

Validate an Israeli ID

Israeli.valid_id?("123456782") # => true

Validate a phone number

Israeli.valid_phone?("0501234567", type: :mobile) # => true

See Also:

Defined Under Namespace

Modules: Luhn, Sanitizer, Validators Classes: BankAccountResult, Error, IdResult, InvalidBankAccountError, InvalidFormatError, InvalidIdError, InvalidPhoneError, InvalidPostalCodeError, PhoneResult, PostalCodeResult, Railtie, Result

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.format_bank_account(value, style: :domestic) ⇒ String?

Formats an Israeli bank account number.

Parameters:

  • value (String)

    The bank account to format

  • style (Symbol) (defaults to: :domestic)

    Format style: :domestic, :compact, or :iban

Returns:

  • (String, nil)

    Formatted bank account or nil if invalid



121
122
123
# File 'lib/israeli.rb', line 121

def (value, style: :domestic)
  Validators::BankAccount.format(value, style: style)
end

.format_id(value) ⇒ String?

Formats an Israeli ID number to standard 9-digit format.

Parameters:

  • value (String, Integer)

    The ID number to format

Returns:

  • (String, nil)

    Formatted ID or nil if invalid



94
95
96
# File 'lib/israeli.rb', line 94

def format_id(value)
  Validators::Id.format(value)
end

.format_phone(value, style: :dashed) ⇒ String?

Formats an Israeli phone number.

Parameters:

  • value (String)

    The phone number to format

  • style (Symbol) (defaults to: :dashed)

    Format style: :dashed, :international, or :compact

Returns:

  • (String, nil)

    Formatted phone or nil if invalid



103
104
105
# File 'lib/israeli.rb', line 103

def format_phone(value, style: :dashed)
  Validators::Phone.format(value, style: style)
end

.format_postal_code(value, style: :compact) ⇒ String?

Formats an Israeli postal code.

Parameters:

  • value (String)

    The postal code to format

  • style (Symbol) (defaults to: :compact)

    Format style: :compact or :spaced

Returns:

  • (String, nil)

    Formatted postal code or nil if invalid



112
113
114
# File 'lib/israeli.rb', line 112

def format_postal_code(value, style: :compact)
  Validators::PostalCode.format(value, style: style)
end

.parse_bank_account(value) ⇒ Israeli::BankAccountResult

Parses an Israeli bank account and returns a result object.

Examples:

result = Israeli.("4985622815429")
result.valid?     # => true
result.domestic?  # => true
result.formatted  # => "49-856-22815429"

Parameters:

  • value (String)

    The bank account to parse

Returns:



270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/israeli.rb', line 270

def (value)
  valid = Validators::BankAccount.valid?(value)
  reason = valid ? nil : Validators::BankAccount.invalid_reason(value)
  detected_format = detect_bank_format(value) if valid

  BankAccountResult.new(
    original: value,
    normalized: normalize_bank_value(value, detected_format),
    valid: valid,
    reason: reason,
    format: detected_format
  )
end

.parse_id(value) ⇒ Israeli::IdResult

Parses an Israeli ID number and returns a result object.

Examples:

result = Israeli.parse_id("123456782")
result.valid?     # => true
result.formatted  # => "123456782"

Invalid ID

result = Israeli.parse_id("123456789")
result.valid?  # => false
result.reason  # => :invalid_checksum

Parameters:

  • value (String, Integer)

    The ID number to parse

Returns:



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/israeli.rb', line 197

def parse_id(value)
  digits = Sanitizer.digits_only(value)
  normalized = digits&.rjust(9, "0")
  valid = Validators::Id.valid?(value)
  reason = valid ? nil : Validators::Id.invalid_reason(value)

  IdResult.new(
    original: value,
    normalized: normalized,
    valid: valid,
    reason: reason
  )
end

.parse_phone(value) ⇒ Israeli::PhoneResult

Parses an Israeli phone number and returns a result object.

Examples:

result = Israeli.parse_phone("0501234567")
result.valid?   # => true
result.type     # => :mobile
result.mobile?  # => true
result.formatted(style: :dashed)  # => "050-123-4567"

Parameters:

  • value (String)

    The phone number to parse

Returns:



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/israeli.rb', line 222

def parse_phone(value)
  normalized = Sanitizer.normalize_phone(value)
  valid = Validators::Phone.valid?(value)
  phone_type = Validators::Phone.detect_type(value)
  reason = valid ? nil : Validators::Phone.invalid_reason(value)

  PhoneResult.new(
    original: value,
    normalized: normalized,
    valid: valid,
    reason: reason,
    type: phone_type
  )
end

.parse_postal_code(value) ⇒ Israeli::PostalCodeResult

Parses an Israeli postal code and returns a result object.

Examples:

result = Israeli.parse_postal_code("2610101")
result.valid?    # => true
result.formatted # => "2610101"
result.formatted(style: :spaced)  # => "26101 01"

Parameters:

  • value (String)

    The postal code to parse

Returns:



247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/israeli.rb', line 247

def parse_postal_code(value)
  digits = Sanitizer.digits_only(value)
  valid = Validators::PostalCode.valid?(value)
  reason = valid ? nil : Validators::PostalCode.invalid_reason(value)

  PostalCodeResult.new(
    original: value,
    normalized: digits,
    valid: valid,
    reason: reason
  )
end

.phone_type(value) ⇒ Symbol?

Detects the type of phone number.

Examples:

Israeli.phone_type("0501234567")  # => :mobile
Israeli.phone_type("021234567")   # => :landline

Parameters:

  • value (String)

    The phone number to check

Returns:

  • (Symbol, nil)

    :mobile, :landline, :voip, or nil if invalid



73
74
75
# File 'lib/israeli.rb', line 73

def phone_type(value)
  Validators::Phone.detect_type(value)
end

.valid_bank_account!(value, format: :any) ⇒ true

Validates an Israeli bank account, raising an error if invalid.

Parameters:

  • value (String)

    The bank account to validate

  • format (Symbol) (defaults to: :any)

    Format: :domestic, :iban, or :any

Returns:

  • (true)

    Always returns true if valid

Raises:



173
174
175
176
177
178
# File 'lib/israeli.rb', line 173

def valid_bank_account!(value, format: :any)
  return true if valid_bank_account?(value, format: format)

  reason = Validators::BankAccount.invalid_reason(value, format: format)
  raise InvalidBankAccountError.new("Invalid Israeli bank account", reason: reason)
end

.valid_bank_account?(value, format: :any) ⇒ Boolean

Validates an Israeli bank account number.

Examples:

Israeli.valid_bank_account?("4985622815429")           # => true (domestic)
Israeli.valid_bank_account?("IL620108000000099999999") # => true (IBAN)

Parameters:

  • value (String)

    The bank account to validate

  • format (Symbol) (defaults to: :any)

    Format: :domestic, :iban, or :any

Returns:

  • (Boolean)

    true if valid, false otherwise



86
87
88
# File 'lib/israeli.rb', line 86

def valid_bank_account?(value, format: :any)
  Validators::BankAccount.valid?(value, format: format)
end

.valid_id!(value) ⇒ true

Validates an Israeli ID number, raising an error if invalid.

Examples:

Israeli.valid_id!("123456782")  # => true
Israeli.valid_id!("123456789")  # => raises InvalidIdError

Parameters:

  • value (String, Integer)

    The ID number to validate

Returns:

  • (true)

    Always returns true if valid

Raises:



137
138
139
140
141
# File 'lib/israeli.rb', line 137

def valid_id!(value)
  return true if valid_id?(value)

  raise InvalidIdError.new("Invalid Israeli ID", reason: Validators::Id.invalid_reason(value))
end

.valid_id?(value) ⇒ Boolean

Validates an Israeli ID number (Mispar Zehut).

Examples:

Israeli.valid_id?("123456782")     # => true
Israeli.valid_id?("12345678-2")    # => true (formatted)
Israeli.valid_id?("123456789")     # => false (bad checksum)

Parameters:

  • value (String, Integer)

    The ID number to validate

Returns:

  • (Boolean)

    true if valid, false otherwise



35
36
37
# File 'lib/israeli.rb', line 35

def valid_id?(value)
  Validators::Id.valid?(value)
end

.valid_phone!(value, type: :any) ⇒ true

Validates an Israeli phone number, raising an error if invalid.

Parameters:

  • value (String)

    The phone number to validate

  • type (Symbol) (defaults to: :any)

    Type of phone: :mobile, :landline, :voip, or :any

Returns:

  • (true)

    Always returns true if valid

Raises:



149
150
151
152
153
154
# File 'lib/israeli.rb', line 149

def valid_phone!(value, type: :any)
  return true if valid_phone?(value, type: type)

  reason = Validators::Phone.invalid_reason(value, type: type)
  raise InvalidPhoneError.new("Invalid Israeli phone number", reason: reason)
end

.valid_phone?(value, type: :any) ⇒ Boolean

Validates an Israeli phone number.

Examples:

Israeli.valid_phone?("0501234567")                    # => true
Israeli.valid_phone?("0501234567", type: :mobile)     # => true
Israeli.valid_phone?("+972501234567", type: :mobile)  # => true

Parameters:

  • value (String)

    The phone number to validate

  • type (Symbol) (defaults to: :any)

    Type of phone: :mobile, :landline, :voip, or :any

Returns:

  • (Boolean)

    true if valid, false otherwise



61
62
63
# File 'lib/israeli.rb', line 61

def valid_phone?(value, type: :any)
  Validators::Phone.valid?(value, type: type)
end

.valid_postal_code!(value) ⇒ true

Validates an Israeli postal code, raising an error if invalid.

Parameters:

  • value (String)

    The postal code to validate

Returns:

  • (true)

    Always returns true if valid

Raises:



161
162
163
164
165
# File 'lib/israeli.rb', line 161

def valid_postal_code!(value)
  return true if valid_postal_code?(value)

  raise InvalidPostalCodeError.new("Invalid Israeli postal code", reason: Validators::PostalCode.invalid_reason(value))
end

.valid_postal_code?(value) ⇒ Boolean

Validates an Israeli postal code (Mikud).

Examples:

Israeli.valid_postal_code?("2610101")   # => true
Israeli.valid_postal_code?("26101 01")  # => true (with space)

Parameters:

  • value (String)

    The postal code to validate

Returns:

  • (Boolean)

    true if valid, false otherwise



47
48
49
# File 'lib/israeli.rb', line 47

def valid_postal_code?(value)
  Validators::PostalCode.valid?(value)
end