Class: Contentstack::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/contentstack/asset.rb

Overview

Asset class to fetch file details on Conentstack server.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Asset

Create instance of an Asset. Accepts either a uid of asset (String) or a complete asset JSON Usage for String parameter

@asset = @stack.asset("some_asset_uid")
@asset.fetch

Usage for Hash parameter

@asset = @stack.asset({
  :uid          => "some_asset_uid",
  :content_type => "file_type", # image/png, image/jpeg, application/pdf, video/mp4 etc.
  :filename    => "some_file_name",
  :file_size    => "some_file_size",
  :tags         => ["tag1", "tag2", "tag3"],
  :url          => "file_url"
})
@asset.fetch

Parameters:

  • attrs (String/Hash)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/contentstack/asset.rb', line 41

def initialize(attrs)
  if attrs.class == String
    @uid = attrs
  else
    attrs = attrs.symbolize_keys
    @uid = attrs[:uid]
    @content_type = attrs[:content_type]
    @filename = attrs[:filename]
    @file_size = attrs[:file_size]
    @tags = attrs[:tags]
    @url = attrs[:url]
  end

  self
end

Instance Attribute Details

#content_typeObject (readonly)

Content Type for the asset. image/png, image/jpeg, application/pdf, video/mp4 etc.



10
11
12
# File 'lib/contentstack/asset.rb', line 10

def content_type
  @content_type
end

#file_sizeObject (readonly)

Size of the asset.



16
17
18
# File 'lib/contentstack/asset.rb', line 16

def file_size
  @file_size
end

#filenameObject (readonly)

Name of the asset.



13
14
15
# File 'lib/contentstack/asset.rb', line 13

def filename
  @filename
end

#tagsObject (readonly)

Array of tags assigned to the asset.



19
20
21
# File 'lib/contentstack/asset.rb', line 19

def tags
  @tags
end

#uidObject (readonly)

Contentstack Asset UID for this asset



7
8
9
# File 'lib/contentstack/asset.rb', line 7

def uid
  @uid
end

#urlObject (readonly)

URL to fetch/render the asset



22
23
24
# File 'lib/contentstack/asset.rb', line 22

def url
  @url
end

Instance Method Details

#fetchObject

Fetch a particular asset using uid.

@asset = @stack.asset('some_asset_uid')
@asset.fetch
puts @asset.url


61
62
63
64
65
# File 'lib/contentstack/asset.rb', line 61

def fetch
  json = API.get_assets(@uid)
  # puts "json -- #{json}"
  self.class.new(json["asset"])
end