Class: Chimps::Commands::Query

Inherits:
Chimps::Command show all
Includes:
Utils::UsesParamValueData
Defined in:
lib/chimps-cli/commands/query.rb

Overview

A command to issue a GET request against the Infochimps paid query API.

Constant Summary collapse

USAGE =
"usage: chimps query [OPTIONS] ENDPOINT [PROP=VALUE] ..."
HELP =
<<EOF

Send a query to the given ENDPOINT on the Infochimps Query API with the
given parameters.

#{how_to_input_data}
You can learn more about the Infochimps query API, discover datasets
to query, and look up the available parameters at

  http://api.infochimps.com

Examples:

  $ chimps query soc/net/tw/trstrank screen_name=infochimps
  $ chimps query soc/net/tw/trstrank?screen_name=infochimps
  $ chimps query web/an/de/demographics ip=146.6.180.1
  $ chimps query soc/net/tw/trstrank --data=trstrank_queries.yml
EOF

Instance Attribute Summary

Attributes inherited from Chimps::Command

#config

Instance Method Summary collapse

Methods included from Utils::UsesParamValueData

#data, included

Methods inherited from Chimps::Command

#initialize, name, #name

Constructor Details

This class inherits a constructor from Chimps::Command

Instance Method Details

#endpointObject

Raises:



30
31
32
33
# File 'lib/chimps-cli/commands/query.rb', line 30

def endpoint
  raise CLIError.new(self.class::USAGE) unless config.argv.size > 1 && (! config.argv[1].empty?)
  config.argv[1]
end

#execute!Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/chimps-cli/commands/query.rb', line 44

def execute!
  requests.each do |request|
    response = request.get
    if response.error?
      response.print :to => $stderr
    else
      puts(config[:pretty] ? JSON.pretty_generate(JSON.parse(response.body)) : response.body)
    end
  end
end

#requestsObject



35
36
37
38
39
40
41
42
# File 'lib/chimps-cli/commands/query.rb', line 35

def requests
  # ensure_data_is_present! unless endpoint.include?("?")
  if data.is_a?(Hash)
    [QueryRequest.new(endpoint, :query_params => data, :sign => true)]
  else
    data.map { |params| QueryRequest.new(endpoint, :query_params => params, :sign => true) }
  end
end