Module: RunSignup::DataCoercion

Included in:
Race, RunSignupApi::Client
Defined in:
lib/run_signup/data_coercion.rb

Instance Method Summary collapse

Instance Method Details

#coerce_for_api(hash) ⇒ Object



37
38
39
40
41
# File 'lib/run_signup/data_coercion.rb', line 37

def coerce_for_api hash
  hash.each do |k, v|
    hash[k] = coerce_value_for_api v
  end
end

#coerce_from_api(hash) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/run_signup/data_coercion.rb', line 4

def coerce_from_api hash
  if hash
    hash.each do |k, v|
      hash[k] = coerce_value_from_api v
      if k == 'events'
        hash[k] = hash[k].map { |event| RunSignup::Event.new(event) }
      end
    end
  end
  hash
end

#coerce_value_for_api(value) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/run_signup/data_coercion.rb', line 43

def coerce_value_for_api value
  case value
  when true
    'T'
  when false
    'F'
  else
    value
  end
end

#coerce_value_from_api(value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/run_signup/data_coercion.rb', line 16

def coerce_value_from_api value
  if value.is_a?(Array)
    value.map { |v1| coerce_value_from_api(v1) }
  elsif value.is_a?(Hash)
    coerce_from_api(value)
  else
    case value
    when 'T'
      true
    when 'F'
      false
    when /^\d{1,2}\/\d{1,2}\/\d{4} \d{2}:\d{2}$/
      DateTime.strptime(value, '%m/%d/%Y %H:%M').to_s
    when /^\d{1,2}\/\d{1,2}\/\d{4}$/
      Date.strptime(value, '%m/%d/%Y').to_s
    else
      value
    end
  end
end