Class: Atlas::Box

Inherits:
Resource show all
Defined in:
lib/atlas/box.rb

Overview

Representation and handling of Box objects.

Constant Summary

Constants inherited from Resource

Resource::INTERNAL_ATTRIBUTE_KEYS

Instance Attribute Summary collapse

Attributes inherited from Resource

#tag, #url_builder

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#attributes, date_accessor, date_writer, #inspect, #to_hash, #update_with_response

Methods included from Validations

included

Constructor Details

#initialize(tag, hash = {}) ⇒ Box

Initialize a box from a tag and object hash.

Parameters:

  • tag (String)

    the tag which represents the origin of the box.

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

    the attributes for the box

  • hash (String) (defaults to: {})

    :name The name of the box, used to identify it.

Options Hash (hash):

  • :username (String)

    The username to assign the box to.

  • :short_description (String)

    The short description is used on small box previews.

  • :description (String)

    Markdown text used as a full-length and in-depth description of the box.

  • :is_private (Boolean)

    A boolean if the box should be private or not.



65
66
67
68
69
70
# File 'lib/atlas/box.rb', line 65

def initialize(tag, hash = {})
  hash.replace_key!("private", "is_private")
  hash.replace_key!("description_markdown", "description")

  super(tag, hash)
end

Instance Attribute Details

#current_versionObject

Returns the value of attribute current_version.



13
14
15
# File 'lib/atlas/box.rb', line 13

def current_version
  @current_version
end

#descriptionObject

Returns the value of attribute description.



13
14
15
# File 'lib/atlas/box.rb', line 13

def description
  @description
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/atlas/box.rb', line 13

def name
  @name
end

#privateObject

Returns the value of attribute private.



13
14
15
# File 'lib/atlas/box.rb', line 13

def private
  @private
end

#short_descriptionObject

Returns the value of attribute short_description.



13
14
15
# File 'lib/atlas/box.rb', line 13

def short_description
  @short_description
end

#usernameObject

Returns the value of attribute username.



13
14
15
# File 'lib/atlas/box.rb', line 13

def username
  @username
end

#versionsObject

Returns the value of attribute versions.



13
14
15
# File 'lib/atlas/box.rb', line 13

def versions
  @versions
end

Class Method Details

.create(attr = {}) ⇒ Box

Create a new Box.

Parameters:

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

    attributes to create the box with

  • attr (String) (defaults to: {})

    :name The name of the box, used to identify it.

Options Hash (attr):

  • :username (String)

    The username to assign the box to.

  • :short_description (String)

    The short description is used on small box previews.

  • :description (String)

    Markdown text used as a full-length and in-depth description of the box.

  • :is_private (Boolean)

    A boolean if the box should be private or not.

Returns:

  • (Box)

    a newly created box.



44
45
46
47
48
49
# File 'lib/atlas/box.rb', line 44

def self.create(attr = {})
  tag = "#{attr.fetch(:username, '')}/#{attr[:name]}"
  box = new(tag, attr)
  box.save
  box
end

.find(tag) ⇒ Box

Find a box by it’s tag.

Parameters:

  • tag (String)

    the tag of the box.

Returns:

  • (Box)

    a representation of the box.



24
25
26
27
28
29
# File 'lib/atlas/box.rb', line 24

def self.find(tag)
  url_builder = UrlBuilder.new tag
  response = Atlas.client.get(url_builder.box_url)

  new(tag, response)
end

Instance Method Details

#create_version(attr) ⇒ BoxVersion

Create a version for this box.

Parameters:

  • attr (Hash)

    attributes to set on the version.

Returns:

  • (BoxVersion)

    a BoxVersion representing the new version.



103
104
105
# File 'lib/atlas/box.rb', line 103

def create_version(attr)
  BoxVersion.create(tag, attr)
end

#deleteHash

Delete the box.

Returns:

  • (Hash)

    response body from Atlas.



136
137
138
# File 'lib/atlas/box.rb', line 136

def delete
  Atlas.client.delete(url_builder.box_url)
end

#saveHash

Save the box.

Returns:

  • (Hash)

    Atlas response object.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/atlas/box.rb', line 110

def save
  validate!

  body = { box: to_hash }

  # versions are saved seperately
  body[:box].delete(:versions)

  # update or create the box
  begin
    response = Atlas.client.put(url_builder.box_url, body: body)
  rescue Atlas::Errors::NotFoundError
    body[:box].replace_key!(:private, :is_private)

    response = Atlas.client.post("/boxes", body: body)
  end

  # trigger the same on versions
  versions.each(&:save) if versions

  update_with_response(response, [:versions])
end