Class: Embulk::Input::GoogleSpreadsheets::Typecast::MinimalTypecast

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

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/embulk/input/google_spreadsheets/typecast/minimal_typecast.rb', line 35

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

  case value
  when TrueClass, FalseClass
    value
  when String
    value = value.downcase
    case value
    when 'true'
      true
    when 'false'
      false
    else
      raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast '#{value}' to a boolean value."
    end
  else
    raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast #{value.class} to a boolean value: \"#{value}\""
  end
end

#as_double(value) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/embulk/input/google_spreadsheets/typecast/minimal_typecast.rb', line 27

def as_double(value)
  return nil if value.nil?
  return nil if null_string?(value)
  value.to_f
rescue NoMethodError => e
  raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast #{value.class} to Double: \"#{value}\" because of '#{e}'"
end

#as_json(value) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/embulk/input/google_spreadsheets/typecast/minimal_typecast.rb', line 76

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

  # cf. https://github.com/embulk/embulk/blob/191ffd50e555565be77f810db15a21ba66cb7bf6/lib/embulk/page_builder.rb#L20
  # cf. https://github.com/embulk/embulk/blob/191ffd50e555565be77f810db15a21ba66cb7bf6/embulk-core/src/main/java/org/embulk/spi/util/DynamicPageBuilder.java#L97
  # cf. https://github.com/embulk/embulk/blob/191ffd50e555565be77f810db15a21ba66cb7bf6/embulk-core/src/main/java/org/embulk/spi/util/DynamicColumnSetterFactory.java#L66
  # cf. https://github.com/embulk/embulk/blob/997c7beb89d42122f7cb6fe844f8ca79a3cb666c/embulk-core/src/main/java/org/embulk/spi/util/dynamic/JsonColumnSetter.java#L50
  # cf. https://github.com/embulk/embulk/blob/191ffd50e555565be77f810db15a21ba66cb7bf6/embulk-core/src/main/java/org/embulk/spi/util/dynamic/AbstractDynamicColumnSetter.java#L47
  # cf. https://github.com/embulk/embulk/blob/191ffd50e555565be77f810db15a21ba66cb7bf6/embulk-core/src/main/java/org/embulk/spi/json/RubyValueApi.java#L57
  # NOTE: As long as reading the above code, any object can be set as Json
  #       (that must be primitive type or must have `to_msgpack` method.)
  case value
  when TrueClass, FalseClass, Integer, Float, 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 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



19
20
21
22
23
24
25
# File 'lib/embulk/input/google_spreadsheets/typecast/minimal_typecast.rb', line 19

def as_long(value)
  return nil if value.nil?
  return nil if null_string?(value)
  value.to_i
rescue NoMethodError => e
  raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast #{value.class} to Long: \"#{value}\" because of '#{e}'"
end

#as_string(value) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/embulk/input/google_spreadsheets/typecast/minimal_typecast.rb', line 11

def as_string(value)
  return nil if value.nil?
  return nil if null_string?(value)
  value.to_s
rescue NoMethodError => e
  raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast #{value.class} to String: \"#{value}\" because of '#{e}'"
end

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/embulk/input/google_spreadsheets/typecast/minimal_typecast.rb', line 57

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

  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, TypeError, NoMethodError => e
  raise TypecastError.new "`embulk-input-google_spreadsheets`: cannot typecast #{value.class} to Time: \"#{value}\" because of '#{e}'"
end