Sha256: 245af2cedd987c22957989aefc763e583f6b54fddcfa3b51d3fe58062808f2b9

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

require 'base64'
require 'excon'
require 'json'

# This module acts as a namespace, and contains a `.search` method which ties
# together functionality of its child modules.
module Sumo
  autoload :Config, 'sumo/config'
  autoload :Error, 'sumo/error'
  autoload :Formatter, 'sumo/formatter'
  autoload :QueryBuilder, 'sumo/query_builder'

  # Unless otherwise specified in the initializer, the configuration file
  # defaults to '~/.sumo_creds'.
  DEFAULT_CONFIG_FILE = File.expand_path('~/.sumo_creds').freeze

  # Search the given query. If any options are supplied, those are sent to the
  # QueryBuilder. If the key is supplied, that key will be attempted to be
  # extracted from the JSON response. If a config_file is supplied, that will
  # be read instead of the default.
  def search(query, options: {}, key: nil, config_file: DEFAULT_CONFIG_FILE)
    creds = Config.new(config_file).load_config!
    response = QueryBuilder.new(creds, options.merge('q' => query)).execute
    formatted = Formatter.format_json(response)
    key.nil? ? formatted : Formatter.extract_key(key, formatted)
  end
  module_function :search
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sumo-search-0.1.1 lib/sumo.rb
sumo-search-0.1.0 lib/sumo.rb
sumo-search-0.0.2 lib/sumo.rb
sumo-search-0.0.1 lib/sumo.rb