Class: Gzr::Command

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Session
Defined in:
lib/gzr/command.rb

Instance Method Summary collapse

Methods included from Session

#build_connection_hash, #login, #logout_all, #pastel, #say_error, #say_ok, #say_warning, #v3_1_available?, #with_session

Constructor Details

#initializeCommand

Returns a new instance of Command.



36
37
38
39
40
# File 'lib/gzr/command.rb', line 36

def initialize
  @sdk = nil
  @access_token_stack = Array.new
  @options = Hash.new
end

Instance Method Details

#create_merge_query(merge_query) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/gzr/command.rb', line 91

def create_merge_query(merge_query)
  begin
    data = @sdk.create_merge_query(merge_query)
  rescue NoMethodError => nme
    say_error "The api endpoint create_merge_query() is not implemented on this Looker instance"
    raise
  rescue LookerSDK::Error => e
    say_error "Error creating merge_query(#{JSON.pretty_generate(merge_query)})"
    say_error e.message
    raise
  end
  data
end

#create_query(query) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/gzr/command.rb', line 66

def create_query(query)
  begin
    data = @sdk.create_query(query)
  rescue LookerSDK::Error => e
    say_error "Error creating query(#{JSON.pretty_generate(query)})"
    say_error e.message
    raise
  end
  data
end

#executeObject

Execute this command

Raises:

  • (NotImplementedError)


47
48
49
50
51
52
# File 'lib/gzr/command.rb', line 47

def execute(*)
  raise(
    NotImplementedError,
    "#{self.class}##{__method__} must be implemented"
  )
end

#field_expression(name) ⇒ Object

This method will accept a field name in a format like ‘c.e.g’ and convert it into ‘c&.e&.g’, which can be evaluated to get the value of g, or nil if any intermediate value is nil.



226
227
228
229
# File 'lib/gzr/command.rb', line 226

def field_expression(name)
  parts = name.split(/\./)
  parts.join('&.')
end

#field_names(opt_fields) ⇒ Object

This method accepts a string containing a list of fields. The fields can be nested in a format like…

‘a,b,c(d,e(f,g)),h’

representing a structure like

{

a: "val",
b: "val",
c: {
  d: "val",
  e: {
    f: "val",
    g: "val"
  }
},
h: "val"

}

That string will get parsed and yield an array like

a, b, c.d, c.e.f, c.e.g, h


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/gzr/command.rb', line 195

def field_names(opt_fields)
  fields = []
  token_stack = []
  last_token = false
  tokens = opt_fields.split /(\(|,|\))/
  tokens << nil
  tokens.each do |t|
    if t.nil? then
      fields << (token_stack + [last_token]).join('.') if last_token
    elsif t.empty? then
      next
    elsif t == ',' then
      fields << (token_stack + [last_token]).join('.') if last_token
    elsif t == '(' then
      token_stack.push(last_token)
    elsif t == ')' then
      fields << (token_stack + [last_token]).join('.') if last_token
      token_stack.pop
      last_token = false
    else
      last_token = t
    end
  end
  fields
end

#keys_to_keep(operation) ⇒ Object

This method accepts the name of an sdk operation, then finds the parameter for that operation in the data structures from the swagger.json file. The parameter is a json object. Some of the attributes of the json object are read-only, and some are read-write. A few are write-only. The list of read-write and write-only attribute names are returned as an array. That array can be used to take the json document that describes an object and strip out the read-only values, creating a document that can be used to create or update an object.

The pattern typically looks like this…

new_obj_hash = existing_obj_hash.select do |k,v|
  keys_to_keep('create_new_obj').include? k
end


132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/gzr/command.rb', line 132

def keys_to_keep(operation)
  o = @sdk.operations[operation]
  begin
    say_error "Operation #{operation} not found"
    return []
  end unless o

  parameters = o[:info][:parameters].select { |p| p[:in] == "body" && p[:schema] }

  say_warning "Expecting exactly one body parameter with a schema for operation #{operation}" unless parameters.length == 1
  schema_ref = parameters[0][:schema][:$ref].split(/\//)
  return @sdk.swagger[schema_ref[1].to_sym][schema_ref[2].to_sym][:properties].reject { |k,v| v[:readOnly] }.keys
end

#merge_query(merge_result_id) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gzr/command.rb', line 77

def merge_query(merge_result_id)
  data = nil
  begin
    data = @sdk.merge_query(merge_result_id)
  rescue NoMethodError => nme
    say_error "The api endpoint merge_query(#{merge_result_id}) is not implemented on this Looker instance"
  rescue LookerSDK::Error => e
    say_error "Error querying merge_query(#{merge_result_id})"
    say_error e.message
    raise
  end
  data
end

#pairs(a, b, *args) ⇒ Object

This method will accept two arrays, a and b, and create a third array like [ [a,b], [a,b], [a,b], …]. If either array is longer than the other, additional pairs will be generated with the shorter array padded out with nil values.

Any additional args will be added to each inner array.



239
240
241
242
243
244
245
246
247
248
249
# File 'lib/gzr/command.rb', line 239

def pairs(a, b, *args)
  pair_array = Array.new([a.count,b.count].max) do |i|
    pair = [a.fetch(i,nil),b.fetch(i,nil)]
    pair += args if args
    pair
  end

  return pair_array unless block_given?

  pair_array.collect { |e| yield(e) }
end

#query(query_id) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gzr/command.rb', line 54

def query(query_id)
  data = nil
  begin
    data = @sdk.query(query_id)
  rescue LookerSDK::Error => e
    say_error "Error querying query(#{query_id})"
    say_error e.message
    raise
  end
  data
end

#render_csv(t) ⇒ Object

The tty-table gem is normally used to output tabular data. This method accepts a Table object as used by the tty-table gem, and generates CSV output. It returns a string with crlf encoding



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/gzr/command.rb', line 151

def render_csv(t)
  io = StringIO.new
  io.puts (
    t.header.collect do |v|
      v ? "\"#{v.to_s.gsub(/"/, '""')}\"" : ""
    end.join(',')
  ) unless @options[:plain]
  t.each do |row|
    next if row === t.header
    io.puts (
      row.collect do |v|
        v ? "\"#{v.to_s.gsub(/"/, '""')}\"" : ""
      end.join(',')
    )
  end
  io.rewind
  io.gets(nil).encode(crlf_newline: true)
end

#run_inline_query(query) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/gzr/command.rb', line 105

def run_inline_query(query)
  begin
    data = @sdk.run_inline_query("json",query)
  rescue LookerSDK::Error => e
    say_error "Error running inline_query(#{JSON.pretty_generate(query)})"
    say_error e.message
    raise
  end
  data
end