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
-
.float(value, default = 0.0) ⇒ Float
private
The parsed float, or the default.
-
.float?(value) ⇒ Float?
private
The parsed float, or nil when absent/unparseable.
-
.integer(value, default = 0) ⇒ Integer
private
The parsed integer, or the default.
-
.integer?(value) ⇒ Integer?
private
The parsed integer, or nil when absent/unparseable.
-
.records(value) ⇒ Hash
private
The value when it is a Hash, otherwise an empty Hash.
-
.string(value, default = "") ⇒ String
private
The value stringified, or the default.
-
.string?(value) ⇒ String?
private
‘?` marks the nullable variant, not a boolean predicate, so nil is intended.
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.
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.
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.
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.
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.
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.
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.
22 |
# File 'lib/sms_ru/coerce.rb', line 22 def string?(value) = value ? String(value) : nil # rubocop:disable Style/ReturnNilInPredicateMethodDefinition |