Class: Atlas::Box

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

Overview

Representation and handling of Box objects.

Instance Attribute Summary collapse

Attributes inherited from Resource

#tag, #url_builder

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

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

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.



63
64
65
66
67
68
# File 'lib/atlas/box.rb', line 63

def initialize(tag, hash = {})
  hash['is_private'] = hash['private']
  hash['description'] = hash['description_markdown']

  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

#is_privateObject

Returns the value of attribute is_private.



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

def is_private
  @is_private
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
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.



42
43
44
45
46
47
# File 'lib/atlas/box.rb', line 42

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.



22
23
24
25
26
27
# File 'lib/atlas/box.rb', line 22

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.



101
102
103
# File 'lib/atlas/box.rb', line 101

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

#deleteHash

Delete the box.

Returns:

  • (Hash)

    response body from Atlas.



130
131
132
# File 'lib/atlas/box.rb', line 130

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

#saveHash

Save the box.

Returns:

  • (Hash)

    Atlas response object.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/atlas/box.rb', line 108

def save
  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
    response = Atlas.client.post('/boxes', body: body)
  end

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

  update_with_response(response, [:versions])
end