Class: Ecoportal::API::V2::Registers

Inherits:
Object
  • Object
show all
Extended by:
Common::BaseClass
Includes:
Common::Content::DocHelpers, Enumerable
Defined in:
lib/ecoportal/api/v2/registers.rb,
lib/ecoportal/api/v2/registers/register.rb,
lib/ecoportal/api/v2/registers/template.rb,
lib/ecoportal/api/v2/registers/page_result.rb,
lib/ecoportal/api/v2/registers/stage_result.rb,
lib/ecoportal/api/v2/registers/stages_result.rb,
lib/ecoportal/api/v2/registers/search_results.rb,
lib/ecoportal/api/v2/registers/page_result/membrane_droplet.rb

Defined Under Namespace

Classes: PageResult, Register, SearchResults, StageResult, StagesResult, Template

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common::Content::DocHelpers

#array_id_index, #array_id_item, #array_ids, #get_body, #get_id

Constructor Details

#initialize(client) ⇒ Registers

Returns an instance object ready to make registers api requests.

Parameters:

  • client (Ecoportal::API::Common::Client)

    a Ecoportal::API::Common::Client object that holds the configuration of the api connection.



21
22
23
# File 'lib/ecoportal/api/v2/registers.rb', line 21

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientEcoportal::API::Common::Client (readonly)

a Ecoportal::API::Common::Client object that holds the configuration of the api connection.

Returns:

  • (Ecoportal::API::Common::Client)

    the current value of client



7
8
9
# File 'lib/ecoportal/api/v2/registers.rb', line 7

def client
  @client
end

Instance Method Details

#each(params: {}, &block) ⇒ Object



25
26
27
28
29
# File 'lib/ecoportal/api/v2/registers.rb', line 25

def each(params: {}, &block)
  return to_enum(:each, params: params) unless block

  get.each(&block)
end

#getEnumerable<Register>

Gets all the registers via api request.

Returns:

  • (Enumerable<Register>)

    an Enumerable with all schemas already wrapped as Register objects.



33
34
35
36
37
38
39
40
# File 'lib/ecoportal/api/v2/registers.rb', line 33

def get
  response = client.get("/templates")
  Ecoportal::API::Common::Content::WrappedResponse.new(
    response,
    register_class,
    key: "registers"
  )
end

#search(register_id, options = {}) {|result| ... } ⇒ Ecoportal::API::V2::Registers, Ecoportal::API::V2::Registers::SearchResults

Gets all the oozes/pages of register_id matching the options

Parameters:

  • register_id (String)

    the id of the target register to search on.

  • options (Hash) (defaults to: {})

    the search options

Options Hash (options):

  • :query (Hash<Symbol, String>)

    plain search (like the search box in register).

  • :filters (Hash<Symbol, Array<Object>>)

    the set of filters.

  • if (Boolean)

    true, it only performs the first search and results Ecoportal::API::V2::Registers::SearchResults.

Yields:

  • (result)

    something to do with search page-result.

Yield Parameters:

  • result (Ecoportal::V2::Registers::PageResult)

    a page result.

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ecoportal/api/v2/registers.rb', line 51

def search(register_id, options = {}) # rubocop:disable Metrics/AbcSize
  only_first = options.delete(:only_first)
  options    = build_options(options)

  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
  total     = nil

  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"]
    total ||= data["total"]
    if total != data["total"]
      msg  = "Change of total in search results. "
      msg << "Probably due to changes that affect the filter"
      msg << "(register: #{register_id}):"
      print_search_status(msg, total, results, cursor_id, data, options)
      #total = data["total"]
    end

    unless total&.zero?
      results += data["results"].length
      print_progress(results, total)
    end

    data["results"].each do |result|
      object = register_search_result_class.new(result)
      yield object
    end

    break if total <= results

    unless data["cursor_id"]
      msg = "Possible error... finishing search for lack of cursor_id in response:"
      print_search_status(msg, total, results, cursor_id, data, options)
    end

    break unless (cursor_id = data["cursor_id"])
  end

  self
end