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



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

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

#compile_filtersObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/reso_transport/query.rb', line 123

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



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/reso_transport/query.rb', line 139

def compile_params
  params = {}

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

  params['$filter'] = compile_filters unless sub_queries.empty?

  params
end

#countObject



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

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

#current_query_contextObject



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

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

#encode_value(key, val) ⇒ Object

Raises:



151
152
153
154
155
156
# File 'lib/reso_transport/query.rb', line 151

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



58
59
60
61
62
63
# File 'lib/reso_transport/query.rb', line 58

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

  self
end

#handle_response(response) ⇒ Object

Raises:



90
91
92
93
94
95
96
97
# File 'lib/reso_transport/query.rb', line 90

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



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

def include_count
  options[:count] = true
  self
end

#limit(size) ⇒ Object



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

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

#new_query_context(context) ⇒ Object



99
100
101
102
103
# File 'lib/reso_transport/query.rb', line 99

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



80
81
82
# File 'lib/reso_transport/query.rb', line 80

def next_link
  @next_link
end

#offset(size) ⇒ Object



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

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

#optionsObject



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

def options
  @options ||= {}
end

#order(field, dir = nil) ⇒ Object



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

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

#page(token) ⇒ Object



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

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

#responseObject



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

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

#resultsObject



71
72
73
74
75
76
77
# File 'lib/reso_transport/query.rb', line 71

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



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

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

  self
end

#sub_queriesObject



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

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