module Ecoportal module API class V2 # @attr_reader client [Common::Client] a `Common::Client` object that holds the configuration of the api connection. class Registers extend Common::BaseClass include Enumerable include Common::Content::DocHelpers class_resolver :register_class, "Ecoportal::API::V2::Registers::Register" class_resolver :register_search_result_class, "Ecoportal::API::V2::Registers::PageResult" class_resolver :register_search_results, "Ecoportal::API::V2::Registers::SearchResults" attr_reader :client # @param client [Common::Client] a `Common::Client` object that holds the configuration of the api connection. # @return [Registers] an instance object ready to make registers api requests. def initialize(client) @client = client end def each(params: {}, &block) return to_enum(:each) unless block get.each(&block) end # Gets all the registers via api request. # @return [Enumerable] an `Enumerable` with all schemas already wrapped as `Register` objects. def get response = client.get("/templates") Common::Content::WrappedResponse.new(response, register_class, key: "registers") end # Gets all the oozes/pages of `register_id` matching the `options` # @param register_id [String] the `id` of the target register to search on. # @param options [Hash] the search options # @option options [Hash] :query plain search (like the search box in register). # @option options [Hash>] :filters the set of filters. # @option options [Boolean] if `true`, it only performs the first search and results `Ecoportal::API::V2::Registers::SearchResults`. # @yield [result] something to do with search page-result. # @yieldparam result [Ecoportal::V2::Registers::PageResult] a page result. # @return [Ecoportal::API::V2::Registers, Ecoportal::API::V2::Registers::SearchResults] def search(register_id, options = {}) only_first = options.delete(:only_first) # supply a query string # or a filter array (copy/paste from dev tools in the browser) options = {query: nil, filters: []}.update(options) options = {}.tap do |ret| options.each do |key, value| if key == :filters && value.any? ret[key] = {filters: value}.to_json else ret[key] = value if key end end end if only_first response = client.get("/registers/#{register_id}/search", params: options) raise "Request failed - Status #{response.status}: #{response.body}" unless response.success? return register_search_results.new(response.body["data"]) end cursor_id = nil results = 0 loop do options.update(cursor_id: cursor_id) if cursor_id response = client.get("/registers/#{register_id}/search", params: options) raise "Request failed - Status #{response.status}: #{response.body}" unless response.success? data = response.body["data"] unless (total = data["total"]) == 0 results += data["results"].length percent = results * 100 / total msg = "Registers SEARCH" print "#{msg}: #{percent.round}% (of #{total})\r" $stdout.flush end data["results"].each do |result| object = register_search_result_class.new(result) yield object end # break unless (cursor_id = data["cursor_id"]) break if total == results end self end end end end end require 'ecoportal/api/v2/registers/template' require 'ecoportal/api/v2/registers/register' require 'ecoportal/api/v2/registers/stage_result' require 'ecoportal/api/v2/registers/stages_result' require 'ecoportal/api/v2/registers/page_result' require 'ecoportal/api/v2/registers/search_results'