Sha256: b660afdeb726fc53a86365baeea92d301e9170cdf8041cff0690b4a115164875

Contents?: true

Size: 656 Bytes

Versions: 8

Compression:

Stored size: 656 Bytes

Contents

# frozen_string_literal: true
require 'hashie'

class Nano::Response < Hash
  include ::Hashie::Extensions::MergeInitializer
  include ::Hashie::Extensions::IndifferentAccess
  include ::Hashie::Extensions::MethodAccess

  def initialize(hash = {})
    super
    coerce_values
  end

  private

  def coerce_values
    merge!(self) { |_k, v| to_f_or_i_or_s(v) }
  end

  def to_f_or_i_or_s(v)
    return if v.nil?
    return v.to_i if big_integer?(v)
    (float = Float(v)) && (float % 1.0).zero? ? float.to_i : float
  rescue ArgumentError, TypeError
    v
  end

  def big_integer?(v)
    v.respond_to?(:to_i) && v.to_i > 1_000_000_000_000_000
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
nano_rpc-0.9.0 lib/nano_rpc/response.rb
nano_rpc-0.8.0 lib/nano_rpc/response.rb
nano_rpc-0.7.0 lib/nano_rpc/response.rb
nano_rpc-0.6.0 lib/nano_rpc/response.rb
nano_rpc-0.5.0 lib/nano_rpc/response.rb
nano_rpc-0.4.0 lib/nano_rpc/response.rb
nano_rpc-0.3.0 lib/nano_rpc/response.rb
nano_rpc-0.2.0 lib/nano/response.rb