Class: Chimps::Commands::Query

Inherits:
Chimps::Command show all
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

"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

#data_file

Attributes inherited from Chimps::Command

#argv

Instance Method Summary collapse

Methods included from Utils::UsesYamlData

#data, #ignore_yaml_files_on_command_line

Methods inherited from Chimps::Command

#initialize, name, #name

Constructor Details

This class inherits a constructor from Chimps::Command

Instance Method Details

#datasetString

The dataset to query.

Returns:

Raises:



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_optionsObject

Define options for queries.



56
57
58
59
60
# File 'lib/chimps/commands/query.rb', line 56

def define_query_options
  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_lineObject



29
30
31
# File 'lib/chimps/commands/query.rb', line 29

def ignore_first_arg_on_command_line
  true
end

#pathString

The path on the Infochimps query API to query.

Returns:



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?

Returns:

  • (true, nil)


51
52
53
# File 'lib/chimps/commands/query.rb', line 51

def pretty_print?
  @pretty_print
end

#requestsArray<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