class SoapyCake::ResponseValue

Constants

STRING_IDS

Known string ids that should not be parsed as integers

Attributes

key[R]
time_converter[R]
value[R]

Public Class Methods

new(key, value, time_converter) click to toggle source
# File lib/soapy_cake/response_value.rb, line 10
def initialize(key, value, time_converter)
  @key = key.to_s
  @value = value
  @time_converter = time_converter
end

Public Instance Methods

parse() click to toggle source
# File lib/soapy_cake/response_value.rb, line 16
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 Instance Methods

date?() click to toggle source
# File lib/soapy_cake/response_value.rb, line 38
def date?
  value =~ /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.?\d*\z/
end
false?() click to toggle source
# File lib/soapy_cake/response_value.rb, line 30
def false?
  value == 'false'
end
id?() click to toggle source
# File lib/soapy_cake/response_value.rb, line 42
def id?
  key.end_with?('_id')
end
numeric?() click to toggle source
# File lib/soapy_cake/response_value.rb, line 50
def numeric?
  value =~ /\A-?\d*\z/
end
parse_int() click to toggle source
# File lib/soapy_cake/response_value.rb, line 54
def parse_int
  unless value.nil? || numeric?
    raise Error, "'#{key}' contains non-digit chars but was to be parsed as an integer id"
  end

  value.to_i
end
string_id?() click to toggle source
# File lib/soapy_cake/response_value.rb, line 46
def string_id?
  STRING_IDS.any? { |id| key.end_with?(id) }
end
true?() click to toggle source
# File lib/soapy_cake/response_value.rb, line 34
def true?
  value == 'true'
end