Module: Xolo::Server::Helpers::Versions

Defined in:
lib/xolo/server/helpers/versions.rb

Overview

constants and methods for working with Xolo Versions on the server As a helper, these are available in the App instance context, for all routes and views

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object

when this module is included



27
28
29
# File 'lib/xolo/server/helpers/versions.rb', line 27

def self.included(includer)
  Xolo.verbose_include includer, self
end

Instance Method Details

#all_version_objects(title) ⇒ Array<Xolo::Server::Version>

A list of all known versions of a title



56
57
58
# File 'lib/xolo/server/helpers/versions.rb', line 56

def all_version_objects(title)
  all_versions(title).map { |v| instantiate_version title: title, version: v }
end

#all_versions(title) ⇒ Array<String>

A list of all known versions of a title



49
50
51
# File 'lib/xolo/server/helpers/versions.rb', line 49

def all_versions(title)
  Xolo::Server::Version.all_versions(title)
end

#default_min_osObject

The default minimum OS for versions

@return [String] the default minimum OS for versions



38
39
40
41
42
43
44
# File 'lib/xolo/server/helpers/versions.rb', line 38

def default_min_os
  if Xolo::Server.config.default_min_os.pix_empty?
    Xolo::Core::BaseClasses::Version::DEFAULT_MIN_OS.to_s
  else
    Xolo::Server.config.default_min_os.to_s
  end
end

#halt_on_existing_version(title, version) ⇒ void

This method returns an undefined value.

Halt 409 if a title already exists

Raises:

  • (Xolo::NoSuchItemError)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/xolo/server/helpers/versions.rb', line 139

def halt_on_existing_version(title, version)
  log_debug "Checking for existing version '#{version}' of title '#{title}'"

  return unless all_versions(title).include? version

  msg = "Version '#{version}' of title '#{title}' already exists."
  log_debug "ERROR: #{msg}"
  resp_body = @streaming_now ? msg : { status: 409, error: msg }

  # don't halt if we're streaming, just error out
  raise Xolo::NoSuchItemError, msg if @streaming_now

  halt 409, resp_body
end

#halt_on_locked_version(title, version) ⇒ void

This method returns an undefined value.

Halt 409 if a version is locked



158
159
160
161
162
163
164
# File 'lib/xolo/server/helpers/versions.rb', line 158

def halt_on_locked_version(title, version)
  return unless Xolo::Server::Version.locked? title, version

  msg = "Version '#{version}' of title '#{title}' is being modified by another admin. Try again later."
  log_debug "ERROR: #{msg}"
  halt 409, { status: 409, error: msg }
end

#halt_on_missing_version(title, version) ⇒ void

This method returns an undefined value.

Halt 404 if a version doesn't exist

Raises:

  • (Xolo::NoSuchItemError)


121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/xolo/server/helpers/versions.rb', line 121

def halt_on_missing_version(title, version)
  log_debug "Checking for missing version '#{version}' of title '#{title}'"
  return if all_versions(title).include? version

  msg = "No version '#{version}' for title '#{title}'."
  log_debug "ERROR: #{msg}"
  resp_body = @streaming_now ? msg : { status: 404, error: msg }

  # don't halt if we're streaming, just error out
  raise Xolo::NoSuchItemError, msg if @streaming_now

  halt 404, resp_body
end

#instantiate_version(title: nil, version: nil, **data) ⇒ Xolo::Server::Version

Instantiate a Server::Version, with access to the Sinata App instance

If given a Hash, use it with .new to instantiate fresh

If given a title and version, the title may be a String, the title's title, or a Xolo::Server::Title object. If it's a Xolo::Server::Title that object will be used as the title_object for the version object.

In all cases, set the server_app_instance in the new version onject to use for access from the version object to the Sinatra App instance for the session and api connection objects



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
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/xolo/server/helpers/versions.rb', line 78

def instantiate_version(title: nil, version: nil, **data)
  log_debug "Instantiating version with title: '#{title}' (#{title.class}), version: '#{version},' data: #{data}"

  if title.is_a?(Xolo::Server::Title)
    title_obj = title
    title = title_obj.title
  elsif data[:title]
    title = data[:title]
    title_obj = instantiate_title(title)
  elsif title
    title_obj = instantiate_title(title)
  else
    halt 400, { status: 400, error: 'Missing title to instantiate a Xolo::Server::Version' }
    log_error 'Missing title to instantiate a Xolo::Server::Version'
  end

  vers =
    if !data.empty?
      # ensure the title and version are in the data
      data[:title] ||= title
      data[:version] ||= version
      Xolo::Server::Version.new data

    elsif title && version
      halt_on_missing_title title
      halt_on_missing_version title, version

      Xolo::Server::Version.load title, version
    else
      msg = "Invalid title, version, or data to instantiate a Xolo::Server::Version: #{data.class}:#{data} "
      log_error msg
      halt 400, { status: 400, error: msg }
    end

  vers.title_object = title_obj
  vers.server_app_instance = self
  vers
end