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 only available via the REST API.

Examples:

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

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

See Also:

Instance Attribute Summary

Attributes inherited from Resource

#attributes, #client

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Operations::Find::ClassMethods

find

Methods included from Operations::Create::ClassMethods

create

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


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/layer/content.rb', line 21

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

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

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

.urlObject


34
35
36
# File 'lib/layer/content.rb', line 34

def self.url
  '/content'
end

Instance Method Details

#to_json(*args) ⇒ Object


46
47
48
# File 'lib/layer/content.rb', line 46

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

#upload(file) ⇒ Object


38
39
40
# File 'lib/layer/content.rb', line 38

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

#urlObject


42
43
44
# File 'lib/layer/content.rb', line 42

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