Sha256: 869e014538f0a564a8952430e783f7aade2fbfe4e03e24e105f7d75e3c1ca88f

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

module Nwsdk
  class Query
    include Helpers

    attr_accessor :limit, :keys, :id1, :id2, :endpoint, :condition

    def initialize(*args)
      Hash[*args].each {|k,v| self.send("%s="%k, v)}
      @limit   ||= 10000
      @keys    ||= %w{ * }
    end

    def request
      result=build_request.execute
      if response_successful?(result)
        unroll_result(JSON.parse(result))
      else
        result
      end
    end

    def build_request
      endpoint.get_request(
        path: 'sdk',
        params:   {
          msg:   'query',
          query: format_select,
          size:  limit * width
        }
      )
    end

    def width
      if keys==["*"]
        100
      else
        keys.size
      end
    end

    def format_select
      sprintf("select %s where %s", keys.join(','), condition.format)
    end

    def unroll_result(result)
      grouped=result["results"]["fields"].group_by {|f| f["group"] }
      grouped.map do |gid,fields|
        report=Hash.new
        fields.each do |field|
          key = field["type"]
          val = decode_value(field)
          if report.has_key?(key)
            report[key]=[*report[key],val]
          else
            report[key]=val
          end
        end
        report
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nwsdk-1.2.0 lib/nwsdk/query.rb
nwsdk-1.1.7 lib/nwsdk/query.rb
nwsdk-1.1.3 lib/nwsdk/query.rb