Module: Io::Flow::V0::HttpClient::Helper

Defined in:
lib/flow_commerce/flow_api_v0_client.rb

Constant Summary collapse

TRUE_STRINGS =
['t', 'true', 'y', 'yes', 'on', '1', 'trueclass']
FALSE_STRINGS =
['f', 'false', 'n', 'no', 'off', '0', 'falseclass']

Class Method Summary collapse

Class Method Details

.date_iso8601_to_string(value) ⇒ Object



73488
73489
73490
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73488

def Helper.date_iso8601_to_string(value)
  value.nil? ? nil : value.strftime('%Y-%m-%d')
end

.date_time_iso8601_to_string(value) ⇒ Object



73492
73493
73494
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73492

def Helper.date_time_iso8601_to_string(value)
  value.nil? ? nil : value.strftime('%Y-%m-%dT%H:%M:%S%z')
end

.symbolize_keys(hash) ⇒ Object



73445
73446
73447
73448
73449
73450
73451
73452
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73445

def Helper.symbolize_keys(hash)
  Preconditions.assert_class('hash', hash, Hash)
  new_hash = {}
  hash.each { |k, v|
    new_hash[k.to_sym] = v
  }
  new_hash
end

.to_big_decimal(value) ⇒ Object



73454
73455
73456
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73454

def Helper.to_big_decimal(value)
  value ? BigDecimal(value.to_s) : nil
end

.to_boolean(field_name, value) ⇒ Object



73499
73500
73501
73502
73503
73504
73505
73506
73507
73508
73509
73510
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73499

def Helper.to_boolean(field_name, value)
  string = value.to_s.strip.downcase
  if TRUE_STRINGS.include?(string)
    true
  elsif FALSE_STRINGS.include?(string)
    false
  elsif string != ""
    raise PreconditionException.new("Unsupported boolean value[#{string}]. For true, must be one of: #{TRUE_STRINGS.inspect}. For false, must be one of: #{FALSE_STRINGS.inspect}")
  else
    nil
  end
end

.to_date_iso8601(value) ⇒ Object



73468
73469
73470
73471
73472
73473
73474
73475
73476
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73468

def Helper.to_date_iso8601(value)
  if value.is_a?(Date)
    value
  elsif value
    Date.parse(value.to_s)
  else
    nil
  end
end

.to_date_time_iso8601(value) ⇒ Object



73478
73479
73480
73481
73482
73483
73484
73485
73486
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73478

def Helper.to_date_time_iso8601(value)
  if value.is_a?(DateTime)
    value
  elsif value
    DateTime.parse(value.to_s)
  else
    nil
  end
end

.to_object(value) ⇒ Object



73458
73459
73460
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73458

def Helper.to_object(value)
  value ? (value.is_a?(Hash) ? value : JSON.parse(value)) : nil
end

.to_uuid(value) ⇒ Object



73462
73463
73464
73465
73466
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73462

def Helper.to_uuid(value)
  Preconditions.check_state(value.nil? || value.match(/^\w\w\w\w\w\w\w\w\-\w\w\w\w\-\w\w\w\w\-\w\w\w\w\-\w\w\w\w\w\w\w\w\w\w\w\w$/),
                            "Invalid guid[%s]" % value)
  value
end