Class: Appwrite::Payload
- Inherits:
-
Object
- Object
- Appwrite::Payload
- Defined in:
- lib/appwrite/payload.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Class Method Summary collapse
- .from_binary(bytes, filename: nil) ⇒ Payload
- .from_file(path, filename: nil) ⇒ Payload
- .from_json(object, filename: nil) ⇒ Payload
- .from_string(string, filename: nil) ⇒ Payload
Instance Method Summary collapse
-
#initialize(data, path, filename) ⇒ Payload
constructor
A new instance of Payload.
- #to_binary(offset = 0, length = nil) ⇒ String
- #to_file(path) ⇒ void
- #to_json ⇒ Hash
- #to_s ⇒ String (also: #to_string)
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
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
5 6 7 |
# File 'lib/appwrite/payload.rb', line 5 def filename @filename end |
#size ⇒ Object (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
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
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
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
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
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.
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_json ⇒ Hash
44 45 46 |
# File 'lib/appwrite/payload.rb', line 44 def to_json() JSON.parse(to_s()) end |
#to_s ⇒ String Also known as: to_string
37 38 39 |
# File 'lib/appwrite/payload.rb', line 37 def to_s() to_binary().force_encoding('UTF-8') end |