Module: Flydata::Heroku::ClassMethods

Defined in:
lib/flydata/heroku.rb

Instance Method Summary collapse

Instance Method Details

#format_date_for_redshift(time) ⇒ String, Objct

Formats a Time object for Redshift

Parameters:

  • (#strftime)

Returns:

  • (String, Objct)

90
91
92
# File 'lib/flydata/heroku.rb', line 90

def format_date_for_redshift(time)
  time.respond_to?(:strftime) ? time.strftime('%Y-%m-%d') : time
end

#format_time_for_redshift(time) ⇒ String, Objct

Formats a Time object for Redshift

Parameters:

  • (#strftime)

Returns:

  • (String, Objct)

82
83
84
# File 'lib/flydata/heroku.rb', line 82

def format_time_for_redshift(time)
  time.respond_to?(:strftime) ? time.strftime('%Y-%m-%d %H:%M:%S') : time
end

#send_to(*args) ⇒ Object

Puts a JSON string that represents args to $stdout

Examples:

send_to('table_name', { key: 'value' })
send_to('table_name', [{ key: 'value' }, { key: 'value' ])
send_to(:table_name, { key: 'value' })
send_to(:table_name', [{ key: 'value' }, { key: 'value' ])
send_to(active_record)
send_to(active_record, active_record)
send_to([user, user])

Parameters:

  • table (String, Symbol)

    name

  • values (Hash)
  • array (Array<Hash>)

    of value

  • record (ActiveRecord::Base)
  • model (ActiveModel::Serialization)
  • array (Array<ActiveRecord::Base>)

    of record

  • array (Array<ActiveModel::Serialization>)

    of model


44
45
46
47
48
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
# File 'lib/flydata/heroku.rb', line 44

def send_to(*args)
  if args[0].nil?
    raise ArgumentError, 'The argument of send_to must be non-nil'
  end

  if args[0].respond_to?(:empty?) && args[0].empty?
    raise ArgumentError, 'The 1st argument of send_to must not be empty'
  end

  if (args[0].kind_of?(String) || args[0].kind_of?(Symbol))
    unless structured_data?(args[1])
      raise ArgumentError, 'The 2nd argument of send_to must be an Array or Hash'
    end
  end

  obj = case args[0]
        when String
          structured_data_as_flydata(args[0], args[1])
        when Symbol
          structured_data_as_flydata(args[0].to_s, args[1])
        when Array
          ancestors = args[0][0].class.ancestors
          if defined?(ActiveModel) && ancestors.include?(ActiveModel::Serialization)
            active_models_as_flydata(args[0])
          end
        else
          ancestors = args[0].class.ancestors
          if defined?(ActiveModel) && ancestors.include?(ActiveModel::Serialization)
            multiple_active_models_as_flydata(args)
          end
        end
  $stdout.puts(obj.to_json) if obj
end