Module: SmsRu::Coerce Private

Defined in:
lib/sms_ru/coerce.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Normalizes loosely-typed SMS.ru JSON values into the types the result objects declare. SMS.ru is inconsistent on the wire — ‘/my/limit` returns `total_limit` as the string `“10”` but `used_today` as the number `0`, and some counters arrive as `null` — so each field is coerced here rather than trusted as-is.

Each type has two helpers: the ‘?` variant returns nil for a missing/blank/unparseable value (for the nullable fields), while the plain variant falls back to a default (`“”`/`0`/`0.0`, overridable) for the fields the API always populates — so call sites declare their nullability by name.

Class Method Summary collapse

Class Method Details

.float(value, default = 0.0) ⇒ Float

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the parsed float, or the default.

Parameters:

  • value (Object, nil)

    a raw JSON value (number or numeric string)

  • default (Float) (defaults to: 0.0)

    returned when the value is absent/unparseable

Returns:

  • (Float)

    the parsed float, or the default



45
# File 'lib/sms_ru/coerce.rb', line 45

def float(value, default = 0.0) = float?(value) || default

.float?(value) ⇒ Float?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the parsed float, or nil when absent/unparseable.

Parameters:

  • value (Object, nil)

    a raw JSON value (number or numeric string)

Returns:

  • (Float, nil)

    the parsed float, or nil when absent/unparseable



40
# File 'lib/sms_ru/coerce.rb', line 40

def float?(value) = Float(value, exception: false)

.integer(value, default = 0) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the parsed integer, or the default.

Parameters:

  • value (Object, nil)

    a raw JSON value (number or numeric string)

  • default (Integer) (defaults to: 0)

    returned when the value is absent/unparseable

Returns:

  • (Integer)

    the parsed integer, or the default



36
# File 'lib/sms_ru/coerce.rb', line 36

def integer(value, default = 0) = integer?(value) || default

.integer?(value) ⇒ Integer?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the parsed integer, or nil when absent/unparseable.

Parameters:

  • value (Object, nil)

    a raw JSON value (number or numeric string)

Returns:

  • (Integer, nil)

    the parsed integer, or nil when absent/unparseable



31
# File 'lib/sms_ru/coerce.rb', line 31

def integer?(value) = Integer(value, exception: false)

.records(value) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the value when it is a Hash, otherwise an empty Hash.

Parameters:

  • value (Object, nil)

    a raw JSON value expected to be an object

Returns:

  • (Hash)

    the value when it is a Hash, otherwise an empty Hash



49
# File 'lib/sms_ru/coerce.rb', line 49

def records(value) = Hash.try_convert(value) || {}

.string(value, default = "") ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the value stringified, or the default.

Parameters:

  • value (Object, nil)

    a raw JSON value

  • default (String) (defaults to: "")

    returned when the value is absent

Returns:

  • (String)

    the value stringified, or the default



27
# File 'lib/sms_ru/coerce.rb', line 27

def string(value, default = "") = string?(value) || default

.string?(value) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

‘?` marks the nullable variant, not a boolean predicate, so nil is intended.

Parameters:

  • value (Object, nil)

    a raw JSON value

Returns:

  • (String, nil)

    the value stringified, or nil when absent



22
# File 'lib/sms_ru/coerce.rb', line 22

def string?(value) = value ? String(value) : nil # rubocop:disable Style/ReturnNilInPredicateMethodDefinition