Class: Atlas::BoxProvider

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

Overview

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



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

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.



31
32
33
34
35
36
# File 'lib/atlas/box_provider.rb', line 31

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.



15
16
17
18
19
20
# File 'lib/atlas/box_provider.rb', line 15

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.



82
83
84
# File 'lib/atlas/box_provider.rb', line 82

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

#saveHash

Save the provider.

Returns:

  • (Hash)

    Atlas response object.



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

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.



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

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