Class: Atlas::BoxProvider

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

Overview

Representation and handling of Box Provider 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 = {}) ⇒ BoxProvider

Initialize a provider from a tag and object hash.

Parameters:

  • tag (String)

    the tag which represents the origin on the provider.

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

    the attributes for the box

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

    :name The name of the provider.

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

    :url An HTTP URL to the box file. Omit if uploading with Atlas.



47
48
49
50
51
# File 'lib/atlas/box_provider.rb', line 47

def initialize(tag, hash = {})
  hash[:url] = hash[:download_url] unless hash.key? :url

  super(tag, hash)
end

Instance Attribute Details

#download_urlObject

Returns the value of attribute download_url.



7
8
9
# File 'lib/atlas/box_provider.rb', line 7

def download_url
  @download_url
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/atlas/box_provider.rb', line 7

def name
  @name
end

#original_urlObject

Returns the value of attribute original_url.



7
8
9
# File 'lib/atlas/box_provider.rb', line 7

def original_url
  @original_url
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/atlas/box_provider.rb', line 7

def url
  @url
end

Class Method Details

.create(box_version_tag, attr = {}) ⇒ Object

Create a new Provider.

Parameters:

  • box_version_tag (String)

    the box version tag to create the provider under.

  • tag (String)

    the tag which represents the origin on the provider.

  • hash (Hash)

    the attributes for the box

  • hash (String)

    :name The name of the provider.

  • hash (String)

    :url An HTTP URL to the box file. Omit if uploading with Atlas.



33
34
35
36
37
38
# File 'lib/atlas/box_provider.rb', line 33

def self.create(box_version_tag, attr = {})
  tag = "#{box_version_tag}/#{attr[:name]}"
  provider = new(tag, attr)
  provider.save
  provider
end

.find(tag) ⇒ Provider

Find a provider by it’s tag.

Parameters:

  • tag (String)

    the tag of the provider.

Returns:

  • (Provider)

    a representation of the provider.



17
18
19
20
21
22
# File 'lib/atlas/box_provider.rb', line 17

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

  new(tag, response)
end

Instance Method Details

#deleteHash

Delete the provider.

Returns:

  • (Hash)

    Atlas response object.



84
85
86
# File 'lib/atlas/box_provider.rb', line 84

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

#saveHash

Save the provider.

Returns:

  • (Hash)

    Atlas response object.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/atlas/box_provider.rb', line 56

def save
  body = { provider: to_hash }

  begin
    response = Atlas.client.put(url_builder.box_provider_url, body: body)
  rescue Atlas::Errors::NotFoundError
    response = Atlas.client.post("#{url_builder.box_version_url}/providers",
                                 body: body)
  end

  update_with_response(response)
end

#upload(file) ⇒ Object

Upload a .box file for this provider.

Parameters:

  • file (File)

    a File object for the file.



72
73
74
75
76
77
78
79
# File 'lib/atlas/box_provider.rb', line 72

def upload(file)
  # get the path for upload
  response = Atlas.client.get("#{url_builder.box_provider_url}/upload")

  # upload the file
  upload_url = response['upload_path']
  Excon.put(upload_url, body: file)
end