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_instances(title) ⇒ Array<Xolo::Server::Version>

A list of all known versions of a title

Returns:



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

def all_version_instances(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

Returns:

  • (Array<String>)


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)


129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/xolo/server/helpers/versions.rb', line 129

def halt_on_existing_version(title, version)
  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



146
147
148
149
150
151
152
# File 'lib/xolo/server/helpers/versions.rb', line 146

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)


112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/xolo/server/helpers/versions.rb', line 112

def halt_on_missing_version(title, version)
  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(data = nil, title: nil, version: nil) ⇒ 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

Parameters:

  • data (Hash) (defaults to: nil)

    hash to use with .new

  • title (String, Xolo::Server::Title) (defaults to: nil)

    title to use with .load

  • version (String) (defaults to: nil)

    version to use with .load

Returns:



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
# File 'lib/xolo/server/helpers/versions.rb', line 78

def instantiate_version(data = nil, title: nil, version: nil)
  title_obj = nil

  if data
    title = data[:title]
  elsif title.is_a?(Xolo::Server::Title)
    title_obj = title
    title = title_obj.title
  end

  vers =
    if data.is_a? Hash
      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 data to instantiate a Xolo::Server::Version'
      log_error msg
      halt 400, { status: 400, error: msg }
    end

  vers.title_object = title_obj || instantiate_title(title)
  vers.server_app_instance = self
  vers
end