Sha256: a716d1b91985f2e9b3d7e754b057231711725f77842a00c1160d7654ab92f406

Contents?: true

Size: 1.7 KB

Versions: 28

Compression:

Stored size: 1.7 KB

Contents

module Scrivito

class Revision < Struct.new(:id, :workspace, :base)
  def initialize(options)
    super(*options.values_at(:id, :workspace, :base))
  end

  def content_state
    base ? workspace.base_content_state : workspace.content_state
  end

  def content_state_id
    base ? workspace.base_content_state_id : workspace.content_state_id
  end

  def content_state_node
    @node ||= Backend::ContentStateNode.new(content_state_id)
  end

  # returns a list of raw rest-api hashes, one for each given id
  def obj_mget_request(ids)
    internal_obj_mget(ids, [])
  end

  # performs given search-query, returns the search-hits as a list of obj-ids
  def obj_search_request(query)
    if base
      raise Scrivito::InternalError, "search not possible for base revision"
    end

    internal_obj_search(query, [])
  end

  def api_request(verb, path, payload = nil)
    CmsRestApi.public_send(verb, "revisions/#{id}#{path}", payload)
  end

  def base?
    !!base
  end

  private

  def internal_obj_search(query, result)
    response = workspace.api_request(:get, "/objs/search", {
      query: query, include_deleted: true, size: 100, offset: result.size
    })

    cur_result = response['results'].map { |obj_data| obj_data['id'] }
    result += cur_result

    if result.size >= response['total'].to_i
      result
    else
      internal_obj_search(query, result)
    end
  end

  def internal_obj_mget(ids, result)
    response = api_request(
        :get, "/objs/mget", ids: ids, include_deleted: true)

    current_result = result + response["results"]

    if response["results"].size < ids.size
      internal_obj_mget(ids.drop(response["results"].size), current_result)
    else
      current_result
    end
  end
end

end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
scrivito_sdk-1.0.0 lib/scrivito/revision.rb
scrivito_sdk-1.0.0.rc4 lib/scrivito/revision.rb
scrivito_sdk-1.0.0.rc3 lib/scrivito/revision.rb
scrivito_sdk-1.0.0.rc2 lib/scrivito/revision.rb
scrivito_sdk-1.0.0.rc1 lib/scrivito/revision.rb
scrivito_sdk-0.90.0 lib/scrivito/revision.rb
scrivito_sdk-0.90.0.rc3 lib/scrivito/revision.rb
scrivito_sdk-0.90.0.rc2 lib/scrivito/revision.rb
scrivito_sdk-0.90.0.rc1 lib/scrivito/revision.rb
scrivito_sdk-0.70.2 lib/scrivito/revision.rb
scrivito_sdk-0.71.2 lib/scrivito/revision.rb
scrivito_sdk-0.71.1 lib/scrivito/revision.rb
scrivito_sdk-0.70.1 lib/scrivito/revision.rb
scrivito_sdk-0.71.0 lib/scrivito/revision.rb
scrivito_sdk-0.71.0.rc2 lib/scrivito/revision.rb
scrivito_sdk-0.71.0.rc1 lib/scrivito/revision.rb
scrivito_sdk-0.70.0 lib/scrivito/revision.rb
scrivito_sdk-0.70.0.rc3 lib/scrivito/revision.rb
scrivito_sdk-0.70.0.rc2 lib/scrivito/revision.rb
scrivito_sdk-0.70.0.rc1 lib/scrivito/revision.rb