diff --git a/lib/odata/entity.rb b/lib/odata/entity.rb index a0d4e69..59ac210 100644 --- a/lib/odata/entity.rb +++ b/lib/odata/entity.rb @@ -192,7 +192,7 @@ def set_property_lazy_load(name, xml_value ) def self.process_properties(entity, xml_doc) entity.instance_eval do - xml_doc.xpath('./content/properties/*').each do |property_xml| + xml_doc.xpath('./content/properties/*', './properties/*').each do |property_xml| property_name = property_xml.name if property_xml.attributes['null'] && property_xml.attributes['null'].value == 'true' @@ -234,4 +234,4 @@ def self.process_links(entity, xml_doc) end end end -end \ No newline at end of file +end diff --git a/lib/odata/query.rb b/lib/odata/query.rb index 9ac4094..b9df1ba 100644 --- a/lib/odata/query.rb +++ b/lib/odata/query.rb @@ -91,6 +91,14 @@ def limit(value) self end + # Add search term criteria to query. + # @param value + # @return [self] + def search_term(value) + criteria_set[:search_term] = value + self + end + # Add inline count criteria to query. # Not Supported in CRM2011 # @return [self] @@ -146,13 +154,15 @@ def setup_empty_criteria_set orderby: [], skip: 0, top: 0, - inline_count: false + inline_count: false, + search_term: nil } end def assemble_criteria criteria = [ filter_criteria, + search_term_criteria(:search_term), list_criteria(:orderby), list_criteria(:expand), list_criteria(:select), @@ -179,6 +189,13 @@ def inline_count_criteria criteria_set[:inline_count] ? '$inlinecount=allpages' : nil end + def search_term_criteria(name) + search = criteria_set[name].present? == 0 ? nil : "searchTerm='#{criteria_set[name]}'" + if search.present? + search += '&includePrerelease=false' + end + end + def paging_criteria(name) criteria_set[name] == 0 ? nil : "$#{name}=#{criteria_set[name]}" end diff --git a/lib/odata/query/result.rb b/lib/odata/query/result.rb index 41ab45e..e7ffae6 100644 --- a/lib/odata/query/result.rb +++ b/lib/odata/query/result.rb @@ -18,11 +18,20 @@ def initialize(query, result) # Provided for Enumerable functionality # @param block [block] a block to evaluate # @return [OData::Entity] each entity in turn for the query result + MAX_EXECUTIONS = 100 def each(&block) + last_processed_url = next_page_url process_results(&block) - until next_page.nil? - result = service.execute(next_page_url) + + finished_processing = last_processed_url == next_page_url + execution_count = 0 + until finished_processing + last_processed_url = next_page_url + result = service.execute(last_processed_url, {}, true) process_results(&block) + execution_count += 1 + finished_processing = next_page.nil? || last_processed_url == next_page_url + raise 'Possible infinite loop detected' if execution_count > MAX_EXECUTIONS end end @@ -52,7 +61,17 @@ def next_page end def next_page_url - next_page.attributes['href'].value.gsub(service.service_url, '') + next_page_value = next_page + return unless next_page_value && next_page_value.attributes['href'] + + # We used to get the url in http format, then it changed + # to https. Let's remove both + http_verison = service.service_url.sub('https://', 'http://') + https_version = service.service_url.sub('http://', 'https://') + next_page_value.attributes['href'] + .value + .gsub(http_verison, '') + .gsub(https_version, '') end end end diff --git a/lib/odata/service.rb b/lib/odata/service.rb index f66a18f..32776cb 100644 --- a/lib/odata/service.rb +++ b/lib/odata/service.rb @@ -123,9 +123,23 @@ def [](entity_set_name) # @param url_chunk [to_s] string to append to service url # @param additional_options [Hash] options to pass to Typhoeus # @return [Typhoeus::Response] - def execute(url_chunk, additional_options = {}) + def execute(url_chunk, additional_options = {}, escaped = false) + # There must be a better way to do it, + # but we only use it for Chocolatey, so it won't break anything else + # This just forces us to use Search endpoint for searching through + # Chocolatey instead of Packages. + if url_chunk && url_chunk.starts_with?('Packages?') + url_chunk = url_chunk.gsub('Packages?', 'Search()?') + end + + url = if escaped + "#{URI.escape(service_url)}/#{url_chunk}" + else + URI.escape("#{service_url}/#{url_chunk}") + end + request = ::Typhoeus::Request.new( - URI.escape("#{service_url}/#{url_chunk}"), + url, options[:typhoeus].merge({ method: :get }) .merge(additional_options) diff --git a/odata.gemspec b/odata.gemspec index 2f40de5..84dfd1d 100644 --- a/odata.gemspec +++ b/odata.gemspec @@ -28,7 +28,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'vcr', '~> 2.9.2' spec.add_development_dependency 'timecop', '~> 0.7.1' - spec.add_dependency 'nokogiri', '~> 1.6.2' - spec.add_dependency 'typhoeus', '~> 0.6.8' + spec.add_dependency 'nokogiri', '>= 1.6.2' + spec.add_dependency 'typhoeus', '>= 0.6.8' spec.add_dependency 'andand', '~> 1.3.3' end