Sha256: e6cd0c6104b2ace0e46e3f259fce2596238b59f7d1460a4a5aba66278b0ff2fb
Contents?: true
Size: 1.17 KB
Versions: 1
Compression:
Stored size: 1.17 KB
Contents
module SoapyCake class ResponseValue attr_reader :key, :value # Known string ids that should not be parsed as integers STRING_IDS = %w(tax_id transaction_id).freeze def initialize(key, value, time_converter) @key = key.to_s @value = value @time_converter = time_converter end def parse return parse_int if id? && !string_id? return false if false? return true if true? return time_converter.from_cake(value) if date? # cast to primitive string to get rid of Saxerator string class value.to_s end private attr_reader :time_converter def false? value == 'false'.freeze end def true? value == 'true'.freeze end def date? value =~ /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.?\d*\z/ end def id? key.end_with?('_id'.freeze) end def string_id? STRING_IDS.any? { |id| key.end_with?(id) } end def numeric? value =~ /\A-?\d*\z/ end def parse_int unless value.nil? || numeric? fail Error, "'#{key}' contains non-digit chars but was to be parsed as an integer id" end value.to_i end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
soapy_cake-1.16.2 | lib/soapy_cake/response_value.rb |