Class: Appwrite::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/appwrite/payload.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, path, filename) ⇒ Payload

Returns a new instance of Payload.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/appwrite/payload.rb', line 8

def initialize(data, path, filename)
  if data.nil? && path.nil? then
    raise ArgumentError.new('Payload must have one of data or path')
  end

  @path = path
  @data = data

  @filename = filename
  @size = if @data then
    @data.bytesize
  else
    File.size(@path)
  end
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



5
6
7
# File 'lib/appwrite/payload.rb', line 5

def filename
  @filename
end

#sizeObject (readonly)

Returns the value of attribute size.



6
7
8
# File 'lib/appwrite/payload.rb', line 6

def size
  @size
end

Class Method Details

.from_binary(bytes, filename: nil) ⇒ Payload

Parameters:

  • bytes (String)
  • filename (String, nil) (defaults to: nil)

Returns:



58
59
60
# File 'lib/appwrite/payload.rb', line 58

def self.from_binary(bytes, filename: nil)
  new(bytes, nil, filename)
end

.from_file(path, filename: nil) ⇒ Payload

Parameters:

  • path (String)
  • filename (String, nil) (defaults to: nil)

Returns:

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
# File 'lib/appwrite/payload.rb', line 84

def self.from_file(path, filename: nil)
  raise ArgumentError.new('File not found') if !File.exists?(path)
  filename = if filename.nil? then
    File.basename(path)
  else
    filename
  end
  new(nil, path, filename)
end

.from_json(object, filename: nil) ⇒ Payload

Parameters:

  • object (Hash, Array)
  • filename (String, nil) (defaults to: nil)

Returns:



73
74
75
76
77
78
79
# File 'lib/appwrite/payload.rb', line 73

def self.from_json(object, filename: nil)
  if !object.is_a?(Hash) && !object.is_a?(Array) then
    raise ArgumentError.new('Object must be a Hash or Array')
  end
  json = JSON.generate(object)
  self.from_string(json, filename: filename)
end

.from_string(string, filename: nil) ⇒ Payload

Parameters:

  • string (String)
  • filename (String, nil) (defaults to: nil)

Returns:



65
66
67
68
# File 'lib/appwrite/payload.rb', line 65

def self.from_string(string, filename: nil)
  bytes = string.encode('UTF-8')
  new(bytes, nil, filename)
end

Instance Method Details

#to_binary(offset = 0, length = nil) ⇒ String

Parameters:

  • offset (Integer) (defaults to: 0)
  • length (Integer) (defaults to: nil)

Returns:

  • (String)


27
28
29
30
31
32
33
34
# File 'lib/appwrite/payload.rb', line 27

def to_binary(offset = 0, length = nil)
  length ||= @size - offset
  if @data then
    @data.byteslice(offset, length)
  else
    IO.read(@path, length, offset)
  end
end

#to_file(path) ⇒ void

This method returns an undefined value.

Parameters:

  • path (String)


50
51
52
53
# File 'lib/appwrite/payload.rb', line 50

def to_file(path)
  FileUtils.mkdir_p(File.dirname(path))
  File.open(path, 'wb') { |f| f.write(to_binary()) }
end

#to_jsonHash

Returns:

  • (Hash)


44
45
46
# File 'lib/appwrite/payload.rb', line 44

def to_json()
  JSON.parse(to_s())
end

#to_sString Also known as: to_string

Returns:

  • (String)


37
38
39
# File 'lib/appwrite/payload.rb', line 37

def to_s()
  to_binary().force_encoding('UTF-8')
end