Class: Layer::Content

Inherits:
Resource show all
Extended by:
Operations::Create::ClassMethods, Operations::Find::ClassMethods
Includes:
Operations::Create, Operations::Find
Defined in:
lib/layer/content.rb

Overview

Note:

This is available in both the REST and the Platform API. Please refer to the respective documentation to see which methods are available.

Examples:

content = Layer::Content.create('image/png', File.open('photo.png'))

message = Layer::Message.create({
  sender: { name: 'Server' },
  parts: [
    { content: content, mime_type: 'image/png' }
  ]
})
conversation = Layer::Conversation.find('layer-conversation-id-here')
content = conversation.contents.create('image/png', File.open('photo.png'))

See Also:

Instance Attribute Summary

Attributes inherited from Resource

#attributes, #client

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Operations::Create::ClassMethods

create

Methods included from Operations::Find::ClassMethods

find

Methods included from Operations::Find

#reload

Methods inherited from Resource

class_name, from_response, #id, #initialize, #respond_to_missing?

Constructor Details

This class inherits a constructor from Layer::Resource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Layer::Resource

Class Method Details

.create(mime_type, file, client = self.client) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/layer/content.rb', line 27

def self.create(mime_type, file, client = self.client)
  response = client.post(url, {}, {
    'Upload-Content-Type' => mime_type,
    'Upload-Content-Length' => file.size
  })

  attributes = response.merge('size' => file.size, 'mime_type' => mime_type)

  from_response(attributes, client).tap do |content|
    content.upload(file)
  end
end

.urlObject



40
41
42
# File 'lib/layer/content.rb', line 40

def self.url
  '/content'
end

Instance Method Details

#as_json(*args) ⇒ Object



52
53
54
# File 'lib/layer/content.rb', line 52

def as_json(*args)
  { id: id, size: size }
end

#to_json(*args) ⇒ Object



56
57
58
# File 'lib/layer/content.rb', line 56

def to_json(*args)
  as_json.to_json(*args)
end

#upload(file) ⇒ Object



44
45
46
# File 'lib/layer/content.rb', line 44

def upload(file)
  RestClient.put(upload_url, file)
end

#urlObject



48
49
50
# File 'lib/layer/content.rb', line 48

def url
  attributes['refresh_url'] || "#{self.class.url}/#{Layer::Client.normalize_id(id)}"
end