Class: Chimps::Commands::Query
- Inherits:
-
Chimps::Command
- Object
- OptionParser
- Chimps::Command
- Chimps::Commands::Query
- Includes:
- Utils::UsesYamlData
- Defined in:
- lib/chimps/commands/query.rb
Overview
A command to issue a GET request against the Infochimps paid query API.
Constant Summary collapse
- BANNER =
"usage: chimps query [OPTIONS] DATASET [PROP=VALUE] ..."- HELP =
<<EOF Make a query of the given DATASET on the Infochimps paid query API (not the main Infochimps site). Properties and values can be supplied directly on the command line, from an input YAML file, or multiple YAML documents streamed in via STDIN, in order of decreasing precedence. You can learn more about the Infochimps query API, discover datasets to query, and look up the available parameters at http://api.infochimps.com You can learn about the main Infochimps site API at http://infochimps.org/api EOF
Instance Attribute Summary
Attributes included from Utils::UsesYamlData
Attributes inherited from Chimps::Command
Instance Method Summary collapse
-
#dataset ⇒ String
The dataset to query.
-
#define_query_options ⇒ Object
Define options for queries.
-
#execute! ⇒ Object
Issue the GET request.
- #ignore_first_arg_on_command_line ⇒ Object
-
#path ⇒ String
The path on the Infochimps query API to query.
-
#pretty_print? ⇒ true?
Should the query output be pretty-printed?.
-
#requests ⇒ Array<Chimps::QueryRequest>
The requests that will be sent to the server.
Methods included from Utils::UsesYamlData
#data, #ignore_yaml_files_on_command_line
Methods inherited from Chimps::Command
Constructor Details
This class inherits a constructor from Chimps::Command
Instance Method Details
#dataset ⇒ String
The dataset to query.
36 37 38 39 |
# File 'lib/chimps/commands/query.rb', line 36 def dataset raise CLIError.new("Must provide a dataset to query.") if argv.first.blank? argv.first end |
#define_query_options ⇒ Object
Define options for queries.
56 57 58 59 60 |
# File 'lib/chimps/commands/query.rb', line 56 def on_tail("-p", "--[no-]pretty-print", "Pretty print the output.") do |p| @pretty_print = p end end |
#execute! ⇒ Object
Issue the GET request.
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/chimps/commands/query.rb', line 74 def execute! requests.each do |request| response = request.get if response.error? response.print :to => $stderr else puts pretty_print? ? JSON.pretty_generate(response.data) : response.body end end end |
#ignore_first_arg_on_command_line ⇒ Object
29 30 31 |
# File 'lib/chimps/commands/query.rb', line 29 def ignore_first_arg_on_command_line true end |
#path ⇒ String
The path on the Infochimps query API to query.
44 45 46 |
# File 'lib/chimps/commands/query.rb', line 44 def path dataset + ".json" end |
#pretty_print? ⇒ true?
Should the query output be pretty-printed?
51 52 53 |
# File 'lib/chimps/commands/query.rb', line 51 def pretty_print? @pretty_print end |
#requests ⇒ Array<Chimps::QueryRequest>
The requests that will be sent to the server.
65 66 67 68 69 70 71 |
# File 'lib/chimps/commands/query.rb', line 65 def requests if data.is_a?(Hash) [QueryRequest.new(path, :query_params => data, :authenticate => true)] else # it's an Array, see Chimps::Utils::UsesYamlData data.map { |params| QueryRequest.new(path, :query_params => params, :authenticate => true) } end end |