class Layer::Content

@example

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

message = Layer::Message.create({
  sender: { name: 'Server' },
  parts: [
    { content: content, mime_type: 'image/png' }
  ]
})

@example

conversation = Layer::Conversation.find('layer-conversation-id-here')
content = conversation.contents.create('image/png', File.open('photo.png'))

@see developer.layer.com/docs/client/rest#rich-content Layer REST API documentation about rich content @see developer.layer.com/docs/platform/messages#rich-content Layer Platform API documentation about rich content @!macro various-apis

Public Class Methods

create(mime_type, file, client = self.client) click to toggle source

@!parse extend Layer::Operations::Create::ClassMethods @!parse extend Layer::Operations::Find::ClassMethods

# 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
url() click to toggle source
# File lib/layer/content.rb, line 40
def self.url
  '/content'
end

Public Instance Methods

as_json(*args) click to toggle source
# File lib/layer/content.rb, line 52
def as_json(*args)
  { id: id, size: size }
end
to_json(*args) click to toggle source
# File lib/layer/content.rb, line 56
def to_json(*args)
  as_json.to_json(*args)
end
upload(file) click to toggle source
# File lib/layer/content.rb, line 44
def upload(file)
  RestClient.put(upload_url, file)
end
url() click to toggle source
# File lib/layer/content.rb, line 48
def url
  attributes['refresh_url'] || "#{self.class.url}/#{Layer::Client.normalize_id(id)}"
end