Class: Embulk::Input::GoogleSpreadsheets::Typecast::StrictTypecast

Inherits:
Base
  • Object
show all
Defined in:
lib/embulk/input/google_spreadsheets/typecast/strict_typecast.rb

Direct Known Subclasses

LooseTypecast

Constant Summary collapse

BOOLEAN_TRUE_VALUES =
%w(yes y true t 1)
BOOLEAN_FALSE_VALUES =
%w(no n false f 0)

Instance Attribute Summary

Attributes inherited from Base

#null_string

Instance Method Summary collapse

Methods inherited from Base

#initialize, #logger, #to_json

Constructor Details

This class inherits a constructor from Embulk::Input::GoogleSpreadsheets::Typecast::Base

Instance Method Details

#as_boolean(value) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/embulk/input/google_spreadsheets/typecast/strict_typecast.rb', line 130

def as_boolean(value)
  return nil if null_string?(value)

  case value
  when nil
    nil
  when String, Integer
    return nil if value.is_a?(String) and value.empty?
    value = value.to_s unless value.is_a?(String)
    return true if BOOLEAN_TRUE_VALUES.include?(value.downcase)
    return false if BOOLEAN_FALSE_VALUES.include?(value.downcase)
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast '#{value}' to a boolean value." \
        " A boolean value must be one of #{BOOLEAN_TRUE_VALUES + BOOLEAN_FALSE_VALUES}: \"#{value}\""
  when TrueClass
    true
  when FalseClass
    false
    # when Integer then ... => merge with String case
  when Float
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast Float to a boolean value: \"#{value}\""
  when Array
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast Array to a boolean value: \"#{value}\""
  when Hash
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast Hash to a boolean value: \"#{value}\""
  when Time
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast Time to String: \"#{value}\""
  else
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast #{value.class} to a boolean value: \"#{value}\""
  end
end

#as_double(value) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/embulk/input/google_spreadsheets/typecast/strict_typecast.rb', line 88

def as_double(value)
  return nil if null_string?(value)

  # `to_f` issue incorrect typecast, so use Float()
  # example
  #   [1] pry(main)> '111:222:333'.to_f
  #   => 111.0
  #   [2] pry(main)> Float('111:222:333')
  #   ArgumentError: invalid value for Float(): "111:222:333"
  #   from (pry):2:in `Float'
  case value
  when nil
    nil
  when String, Integer
    return nil if value.is_a?(String) and value.empty?
    begin
      Float(value)
    rescue ArgumentError => e
      raise TypecastError.new "`embulk-input-google_spreadsheets`: #{e}"
    end
  when TrueClass
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast true to Float: \"#{value}\""
  when FalseClass
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast false to Float: \"#{value}\""
    # when Integer then ...
    # => merge with String case
  when Float
    value
  when Array
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast Array to Float: \"#{value}\""
  when Hash
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast Hash to Float: \"#{value}\""
  when Time
    value.to_f
  else
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast #{value.class} to Float: \"#{value}\""
  end
end

#as_json(value) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/embulk/input/google_spreadsheets/typecast/strict_typecast.rb', line 204

def as_json(value)
  return nil if null_string?(value)

  # NOTE: This method must do `JSON.parse` if `value` is String. ref. https://github.com/embulk/embulk/issues/499
  case value
  when nil, Array, Hash
    value
  when String
    begin
      JSON.parse(value)
    rescue JSON::ParserError => e
      raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast #{value.class} to JSON: \"#{value}\" because of '#{e}'"
    end
    # when TrueClass  then ... => merge with Time case
    # when FalseClass then ... => merge with Time case
    # when Integer    then ... => merge with Time case
    # when Float      then ... => merge with Time case
    # when Array      then ... => merge with nil case
    # when Hash       then ... => merge with nil case
  when TrueClass, FalseClass, Integer, Float, Array, Hash, Time
    # TODO: support Time class. Now call Exception to avoid format/timezone trouble.
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast Time to JSON: \"#{value}\""

  else
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast #{value.class} to JSON: \"#{value}\""
  end
end

#as_long(value) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/embulk/input/google_spreadsheets/typecast/strict_typecast.rb', line 49

def as_long(value)
  return nil if null_string?(value)

  # `to_i` issue incorrect typecast, so use Integer()
  # example
  #   [1] pry(main)> '111:222:333'.to_i
  #   => 111
  #   [2] pry(main)> Integer('111:222:333')
  #   ArgumentError: invalid value for Integer(): "111:222:333"
  #   from (pry):2:in `Integer'
  case value
  when nil
    nil
  when String
    return nil if value.is_a?(String) and value.empty?
    begin
      Integer(value)
    rescue ArgumentError => e
      raise TypecastError.new "`embulk-input-google_spreadsheets`: #{e}"
    end
  when TrueClass
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast true to Integer: \"#{value}\""
  when FalseClass
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast false to Integer: \"#{value}\""
  when Integer
    value
  when Float
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast Float to Integer: \"#{value}\""
  when Array
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast Array to Integer: \"#{value}\""
  when Hash
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast Hash to Integer: \"#{value}\""
  when Time
    value.to_i
  else
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast #{value.class} to Integer: \"#{value}\""
  end
end

#as_string(value) ⇒ Object

It has assumed that argument ‘value` is an instance of any of the below classes. NilClass String TrueClass FalseClass Integer Float Array Hash Time



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/embulk/input/google_spreadsheets/typecast/strict_typecast.rb', line 21

def as_string(value)
  return nil if null_string?(value)

  case value
  when nil
    nil
  when String
    value
  when TrueClass
    value.to_s
  when FalseClass
    value.to_s
  when Integer
    value.to_s
  when Float
    value.to_s
  when Array
    value.to_json
  when Hash
    value.to_json
  when Time
    # TODO: support Time class
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast Time to String: \"#{value}\""
  else
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast #{value.class} to String: \"#{value}\""
  end
end

#as_timestamp(value, timestamp_format = nil, timezone = nil) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/embulk/input/google_spreadsheets/typecast/strict_typecast.rb', line 161

def as_timestamp(value, timestamp_format = nil, timezone = nil)
  return nil if null_string?(value)

  case value
  when nil
    nil
  when String, Integer, Float
    return nil if value.is_a?(String) and value.empty?
    value = value.to_s unless value.is_a?(String)
    begin
      if timestamp_format and TimestampFormatUtil.timezone_format?(timestamp_format)
        Time.strptime(value, timestamp_format)
      elsif timestamp_format and timezone
        TimeWithZone.strptime_with_zone(value, timestamp_format, timezone)
      elsif timezone
        TimeWithZone.parse_with_zone(value, timezone)
      elsif timestamp_format
        Time.strptime(value, timestamp_format)
      else
        Time.parse(value)
      end
    rescue ArgumentError => e
      raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast #{value.class} to Time: \"#{value}\" because of '#{e}'"
    end
  when TrueClass
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast true to Time: \"#{value}\""
  when FalseClass
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast false to Time: \"#{value}\""
    # when Integer
    # ... => merge with String case
    # when Float
    # ... => merge with String case
  when Array
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast Array to Time: \"#{value}\""
  when Hash
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast Hash to Time: \"#{value}\""
  when Time
    value
  else
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast #{value.class} to Time: \"#{value}\""
  end
end