Class: Gzr::Commands::Query::RunQuery
- Inherits:
-
Gzr::Command
- Object
- Gzr::Command
- Gzr::Commands::Query::RunQuery
- Defined in:
- lib/gzr/commands/query/runquery.rb
Instance Method Summary collapse
- #execute(input: $stdin, output: $stdout) ⇒ Object
-
#initialize(query_def, options) ⇒ RunQuery
constructor
A new instance of RunQuery.
Methods inherited from Gzr::Command
#all_color_collections, #color_collection, #color_palette_lookup!, #create_merge_query, #create_query, #default_color_collection, #field_expressions_eval, #field_names, #find_color_palette_reference, #find_vis_config_reference, #get_auth, #get_user_by_id, #keys_to_keep, #keys_to_keep_internal, #merge_query, #pairs, #query, #render_csv, #rewrite_color_palette!, #run_inline_query, #update_color_palette!
Methods included from Session
#build_connection_hash, #login, #logout_all, #pastel, #read_token_data, #say_error, #say_ok, #say_warning, #sufficient_version?, #token_file, #update_auth, #with_session, #write_token_data
Constructor Details
#initialize(query_def, options) ⇒ RunQuery
Returns a new instance of RunQuery.
31 32 33 34 35 |
# File 'lib/gzr/commands/query/runquery.rb', line 31 def initialize(query_def,) super() @query_def = query_def @options = end |
Instance Method Details
#execute(input: $stdin, output: $stdout) ⇒ Object
37 38 39 40 41 42 43 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/gzr/commands/query/runquery.rb', line 37 def execute(input: $stdin, output: $stdout) unless @query_def raise Gzr::CLI::Error, "No query specified. A query id, query slug, or a query in json formation must be specified." end case @options[:format] when 'png','jpg','xlsx' raise Gzr::CLI::Error, "Output file must be specified with '--file=filename' when using '--format=#{@options[:format]}" unless @options[:file] when 'json', 'json_detail', 'csv', 'txt', 'html', 'md', 'sql' # these formats can be output to stdout else raise Gzr::CLI::Error, "'--format=#{@options[:format]}' not understood. The format must be one of json,json_detail,csv,txt,html,md,xlsx,sql,png,jpg" end case @query_def when /^[0-9]+$/ query_id = @query_def.to_i when /^[A-Za-z0-9]+$/ query_slug = @query_def else begin query_hash = JSON.parse(@query_def,{:symbolize_names => true}) rescue JSON::ParserError => e raise Gzr::CLI::Error, "The query specification is not a valid id, slug, or json document" end end f = File.open(@options[:file], "w") if @options[:file] with_session do if query_id || query_slug then begin @sdk.query(query_id) rescue LookerSDK::NotFound => e raise Gzr::CLI::Error, "Query with the id #{query_id} not found" end if query_id begin query_id = @sdk.query_for_slug(query_slug, { :fields => 'id' }).id.to_i rescue LookerSDK::NotFound => e raise Gzr::CLI::Error, "Query with the slug #{query_slug} not found" end if query_slug begin @sdk.run_query(query_id,@options[:format]) { |data,progress| (f || output).write(data) } rescue LookerSDK::Error => e say_error "Error in run_query(#{query_id},#{@options[:format]})})" say_error e raise end else begin @sdk.run_inline_query(@options[:format],query_hash) { |data,progress| (f || output).write(data) } rescue LookerSDK::Error => e say_error "Error in run_inline_query(#{@options[:format]},#{JSON.pretty_generate(query_hash)})" say_error e raise end end end ensure f.close if f end |