Class: ResoTransport::Query

Inherits:
Struct
  • Object
show all
Defined in:
lib/reso_transport/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#resourceObject

Returns the value of attribute resource

Returns:

  • (Object)

    the current value of resource



2
3
4
# File 'lib/reso_transport/query.rb', line 2

def resource
  @resource
end

Instance Method Details

#all(*_contexts, &block) ⇒ Object



3
4
5
6
7
8
# File 'lib/reso_transport/query.rb', line 3

def all(*_contexts, &block)
  new_query_context('and')
  instance_eval(&block)
  clear_query_context
  self
end

#any(&block) ⇒ Object



10
11
12
13
14
15
# File 'lib/reso_transport/query.rb', line 10

def any(&block)
  new_query_context('or')
  instance_eval(&block)
  clear_query_context
  self
end

#clear_query_contextObject



109
110
111
112
# File 'lib/reso_transport/query.rb', line 109

def clear_query_context
  @last_query_context = @current_query_context
  @current_query_context = nil
end

#compile_filtersObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/reso_transport/query.rb', line 131

def compile_filters
  groups = sub_queries.dup
  global = groups.delete(:global)
  filter_groups = groups.values

  filter_chunks = []

  filter_chunks << global[:criteria].join(" #{global[:context]} ") if global && global[:criteria]&.any?

  filter_chunks << filter_groups.map do |g|
    "(#{g[:criteria].join(" #{g[:context]} ")})"
  end.join(' and ')

  filter_chunks.reject { |c| c == '' }.join(' and ')
end

#compile_paramsObject



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/reso_transport/query.rb', line 147

def compile_params
  params = {}

  options.each_pair do |k, v|
    params["$#{k}"] = v
  end

  params['$filter'] = compile_filters unless sub_queries.empty?
  params.merge!(query_parameters) unless query_parameters.empty?

  params
end

#countObject



70
71
72
73
74
# File 'lib/reso_transport/query.rb', line 70

def count
  limit(1).include_count
  parsed = handle_response response
  parsed.fetch('@odata.count', 0)
end

#current_query_contextObject



114
115
116
117
# File 'lib/reso_transport/query.rb', line 114

def current_query_context
  @current_query_context ||= nil
  sub_queries[@current_query_context || :global][:criteria]
end

#encode_value(key, val) ⇒ Object

Raises:



160
161
162
163
164
165
# File 'lib/reso_transport/query.rb', line 160

def encode_value(key, val)
  field = resource.property(key.to_s)
  raise EncodeError.new(resource, key) if field.nil?

  field.encode(val)
end

#expand(*names) ⇒ Object



63
64
65
66
67
68
# File 'lib/reso_transport/query.rb', line 63

def expand(*names)
  ex = options.fetch(:expand, '').split(',')
  options[:expand] = (ex + Array(names)).uniq.join(',')

  self
end

#handle_response(response) ⇒ Object

Raises:



94
95
96
97
98
99
100
101
# File 'lib/reso_transport/query.rb', line 94

def handle_response(response)
  raise RequestError.new(resource.request, response, resource) unless response.success?

  parsed = JSON.parse(response.body)
  raise ResponseError.new(resource.request, response, resource) if parsed.key?('error')

  parsed
end

#include_countObject



46
47
48
49
# File 'lib/reso_transport/query.rb', line 46

def include_count
  options[:count] = true
  self
end

#limit(size) ⇒ Object



31
32
33
34
# File 'lib/reso_transport/query.rb', line 31

def limit(size)
  options[:top] = size
  self
end

#new_query_context(context) ⇒ Object



103
104
105
106
107
# File 'lib/reso_transport/query.rb', line 103

def new_query_context(context)
  @last_query_context ||= 0
  @current_query_context = @last_query_context + 1
  sub_queries[@current_query_context][:context] = context
end

Can only be accessed after results call



84
85
86
# File 'lib/reso_transport/query.rb', line 84

def next_link
  @next_link
end

#offset(size) ⇒ Object



36
37
38
39
# File 'lib/reso_transport/query.rb', line 36

def offset(size)
  options[:skip] = size
  self
end

#optionsObject



119
120
121
# File 'lib/reso_transport/query.rb', line 119

def options
  @options ||= {}
end

#order(field, dir = nil) ⇒ Object



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

def order(field, dir = nil)
  options[:orderby] = [field, dir].join(' ').strip
  self
end

#page(token) ⇒ Object



51
52
53
54
# File 'lib/reso_transport/query.rb', line 51

def page(token)
  options[:next] = token
  self
end

#query_parametersObject



123
124
125
# File 'lib/reso_transport/query.rb', line 123

def query_parameters
  @query_parameters ||= {}
end

#responseObject



88
89
90
91
92
# File 'lib/reso_transport/query.rb', line 88

def response
  resource.get(compile_params)
rescue Faraday::ConnectionFailed
  raise NoResponse.new(resource.request, nil, resource)
end

#resultsObject



76
77
78
79
80
81
# File 'lib/reso_transport/query.rb', line 76

def results
  parsed = handle_response response
  @next_link = parsed.fetch('@odata.nextLink', nil)
  results = Array(parsed.delete('value'))
  resource.parse(results)
end

#select(*fields) ⇒ Object



56
57
58
59
60
61
# File 'lib/reso_transport/query.rb', line 56

def select(*fields)
  os = options.fetch(:select, '').split(',')
  options[:select] = (os + Array(fields)).uniq.join(',')

  self
end

#set_query_params(params) ⇒ Object



26
27
28
29
# File 'lib/reso_transport/query.rb', line 26

def set_query_params(params)
  query_parameters.merge!(params)
  self
end

#sub_queriesObject



127
128
129
# File 'lib/reso_transport/query.rb', line 127

def sub_queries
  @sub_queries ||= Hash.new { |h, k| h[k] = { context: 'and', criteria: [] } }
end