Class: Nwsdk::Query

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/nwsdk/query.rb

Constant Summary

Constants included from Helpers

Helpers::ATTACHMENT_FILENAME, Helpers::MULTIPART_BOUNDARY, Helpers::MULTIPART_END, Helpers::MULTIPART_PROLOGUE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#count_results, #decode_value, #each_multipart_response_entity, #format_timestamp, #get_boundary, #get_sessionids, #response_successful?

Constructor Details

#initialize(*args) ⇒ Query

Returns a new instance of Query.



7
8
9
10
11
# File 'lib/nwsdk/query.rb', line 7

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

Instance Attribute Details

#conditionObject

Returns the value of attribute condition.



5
6
7
# File 'lib/nwsdk/query.rb', line 5

def condition
  @condition
end

#endpointObject

Returns the value of attribute endpoint.



5
6
7
# File 'lib/nwsdk/query.rb', line 5

def endpoint
  @endpoint
end

#id1Object

Returns the value of attribute id1.



5
6
7
# File 'lib/nwsdk/query.rb', line 5

def id1
  @id1
end

#id2Object

Returns the value of attribute id2.



5
6
7
# File 'lib/nwsdk/query.rb', line 5

def id2
  @id2
end

#keysObject

Returns the value of attribute keys.



5
6
7
# File 'lib/nwsdk/query.rb', line 5

def keys
  @keys
end

#limitObject

Returns the value of attribute limit.



5
6
7
# File 'lib/nwsdk/query.rb', line 5

def limit
  @limit
end

Instance Method Details

#build_requestObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/nwsdk/query.rb', line 22

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

#format_selectObject



41
42
43
# File 'lib/nwsdk/query.rb', line 41

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

#requestObject



13
14
15
16
17
18
19
20
# File 'lib/nwsdk/query.rb', line 13

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

#unroll_result(result) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/nwsdk/query.rb', line 45

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

#widthObject



33
34
35
36
37
38
39
# File 'lib/nwsdk/query.rb', line 33

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