Class: ResoTransport::Query
- Inherits:
-
Struct
- Object
- Struct
- ResoTransport::Query
- Defined in:
- lib/reso_transport/query.rb
Instance Attribute Summary collapse
-
#resource ⇒ Object
Returns the value of attribute resource.
Instance Method Summary collapse
- #all(*_contexts, &block) ⇒ Object
- #any(&block) ⇒ Object
- #clear_query_context ⇒ Object
- #compile_filters ⇒ Object
- #compile_params ⇒ Object
- #count ⇒ Object
- #current_query_context ⇒ Object
- #encode_value(key, val) ⇒ Object
- #expand(*names) ⇒ Object
- #handle_response(response) ⇒ Object
- #include_count ⇒ Object
- #limit(size) ⇒ Object
- #new_query_context(context) ⇒ Object
-
#next_link ⇒ Object
Can only be accessed after results call.
- #offset(size) ⇒ Object
- #options ⇒ Object
- #order(field, dir = nil) ⇒ Object
- #page(token) ⇒ Object
- #response ⇒ Object
- #results ⇒ Object
- #select(*fields) ⇒ Object
- #sub_queries ⇒ Object
Instance Attribute Details
#resource ⇒ Object
Returns the value of attribute 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_context ⇒ Object
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_filters ⇒ Object
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_params ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/reso_transport/query.rb', line 139 def compile_params params = {} .each_pair do |k, v| params["$#{k}"] = v end params['$filter'] = compile_filters unless sub_queries.empty? params end |
#count ⇒ Object
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_context ⇒ Object
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
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 (*names) ex = .fetch(:expand, '').split(',') [:expand] = (ex + Array(names)).uniq.join(',') self end |
#handle_response(response) ⇒ Object
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_count ⇒ Object
41 42 43 44 |
# File 'lib/reso_transport/query.rb', line 41 def include_count [:count] = true self end |
#limit(size) ⇒ Object
26 27 28 29 |
# File 'lib/reso_transport/query.rb', line 26 def limit(size) [: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 |
#next_link ⇒ Object
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) [:skip] = size self end |
#options ⇒ Object
115 116 117 |
# File 'lib/reso_transport/query.rb', line 115 def @options ||= {} end |
#order(field, dir = nil) ⇒ Object
36 37 38 39 |
# File 'lib/reso_transport/query.rb', line 36 def order(field, dir = nil) [: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) [:next] = token self end |
#response ⇒ Object
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 |
#results ⇒ Object
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 = .fetch(:select, '').split(',') [:select] = (os + Array(fields)).uniq.join(',') self end |
#sub_queries ⇒ Object
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 |