Class: Gzr::Command
- Inherits:
-
Object
- Object
- Gzr::Command
- Extended by:
- Forwardable
- Includes:
- Session
- Defined in:
- lib/gzr/command.rb
Direct Known Subclasses
Gzr::Commands::Connection::Dialects, Gzr::Commands::Connection::Ls, Gzr::Commands::Dashboard::Cat, Gzr::Commands::Dashboard::Import, Gzr::Commands::Dashboard::Rm, Gzr::Commands::Group::Ls, Gzr::Commands::Group::MemberGroups, Gzr::Commands::Group::MemberUsers, Gzr::Commands::Look::Cat, Gzr::Commands::Look::Import, Gzr::Commands::Look::Rm, Gzr::Commands::Model::Ls, Gzr::Commands::Permissions::Ls, Gzr::Commands::Plan::Cat, Gzr::Commands::Plan::Disable, Gzr::Commands::Plan::Enable, Gzr::Commands::Plan::Failures, Gzr::Commands::Plan::Import, Gzr::Commands::Plan::Ls, Gzr::Commands::Plan::Rm, Gzr::Commands::Plan::RunIt, Gzr::Commands::Query::RunQuery, Gzr::Commands::Role::Cat, Gzr::Commands::Role::GroupAdd, Gzr::Commands::Role::GroupLs, Gzr::Commands::Role::GroupRm, Gzr::Commands::Role::Ls, Gzr::Commands::Role::Rm, Gzr::Commands::Role::UserAdd, Gzr::Commands::Role::UserLs, Gzr::Commands::Role::UserRm, Gzr::Commands::Space::Cat, Gzr::Commands::Space::Create, Gzr::Commands::Space::Export, Gzr::Commands::Space::Ls, Gzr::Commands::Space::Rm, Gzr::Commands::Space::Top, Gzr::Commands::Space::Tree, Gzr::Commands::User::Cat, Gzr::Commands::User::Delete, Gzr::Commands::User::Disable, Gzr::Commands::User::Enable, Gzr::Commands::User::Ls, Gzr::Commands::User::Me
Instance Method Summary collapse
- #create_merge_query(merge_query) ⇒ Object
- #create_query(query) ⇒ Object
-
#execute ⇒ Object
Execute this command.
-
#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.
-
#field_names(opt_fields) ⇒ Object
This method accepts a string containing a list of fields.
-
#initialize ⇒ Command
constructor
A new instance of Command.
-
#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.
- #merge_query(merge_result_id) ⇒ Object
- #pairs(a, b, *args) ⇒ Object
- #query(query_id) ⇒ Object
-
#render_csv(t) ⇒ Object
The tty-table gem is normally used to output tabular data.
- #run_inline_query(query) ⇒ Object
Methods included from Session
#build_connection_hash, #login, #logout_all, #pastel, #say_error, #say_ok, #say_warning, #v3_1_available?, #with_session
Constructor Details
#initialize ⇒ Command
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. 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. raise end data end |
#execute ⇒ Object
Execute this command
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. raise end data end |
#pairs(a, b, *args) ⇒ Object
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. 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. raise end data end |