Class: RedashExporter::Query

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/redash_exporter/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Query

Returns a new instance of Query.



11
12
13
14
# File 'lib/redash_exporter/query.rb', line 11

def initialize(raw)
  @data = raw.symbolize_keys
  @sort_key = :retrieved_at
end

Instance Attribute Details

#sort_keyObject

Returns the value of attribute sort_key.



9
10
11
# File 'lib/redash_exporter/query.rb', line 9

def sort_key
  @sort_key
end

Instance Method Details

#<=>Object



33
34
35
# File 'lib/redash_exporter/query.rb', line 33

def <=>
  @data[@sort_key] || -1
end

#[](key) ⇒ Object



16
17
18
# File 'lib/redash_exporter/query.rb', line 16

def [](key)
  @data[key.to_sym]
end

#export(dir = '.') ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/redash_exporter/query.rb', line 37

def export(dir = '.')
  path = File.expand_path("#{dir}/#{file_name}")

  if File.exist?(path)
    puts "overwrite #{path} [yes(y),no(n)] ?"
    ok = %w[yes y ok].include?(STDIN.gets.strip)
    unless ok
      puts "Not create #{path}."
      return
    end
  end

  File.open(path, 'w') do |file|
    file.print to_s
  end
end

#to_sObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/redash_exporter/query.rb', line 20

def to_s
  <<~EOS
  /**
   * #{@data[:name]}
   * created at #{@data[:created_at]}
   * last updated at #{@data[:updated_at]}
   * created by #{@data.dig(:user, :name)} (#{@data.dig(:user, :email)})
   **/

  #{@data[:query]}
  EOS
end