Class: LC::File
- Inherits:
-
Object
- Object
- LC::File
- Defined in:
- lib/leancloud/datatypes.rb
Overview
File
tf = LC::File.new(:body => “Hello World!”, :local_filename => “hello.txt”) tf.save
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#content_type ⇒ Object
Returns the value of attribute content_type.
-
#id ⇒ Object
Returns the value of attribute id.
-
#local_filename ⇒ Object
‘{“__type”:“File”, “name”:“profile.png”, “url”=>“”}’.
-
#parse_filename ⇒ Object
eg “12-4-532d-d-g3-3-hello.text”.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#initialize(data) ⇒ File
constructor
A new instance of File.
- #save ⇒ Object
- #to_h(*a) ⇒ Object (also: #as_json)
- #to_json(*a) ⇒ Object
Constructor Details
#initialize(data) ⇒ File
Returns a new instance of File.
309 310 311 312 313 314 315 316 317 318 |
# File 'lib/leancloud/datatypes.rb', line 309 def initialize(data) data = Hash[data.map{ |k, v| [k.to_s, v] }] # convert hash keys to strings @local_filename = data["local_filename"] if data["local_filename"] @parse_filename = data["name"] if data["name"] @parse_filename = data["parse_filename"] if data["parse_filename"] @content_type = data["content_type"] if data["content_type"] @url = data["url"] if data["url"] @body = data["body"] if data["body"] @id = data["id"] if data["id"] end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
305 306 307 |
# File 'lib/leancloud/datatypes.rb', line 305 def body @body end |
#content_type ⇒ Object
Returns the value of attribute content_type.
304 305 306 |
# File 'lib/leancloud/datatypes.rb', line 304 def content_type @content_type end |
#id ⇒ Object
Returns the value of attribute id.
307 308 309 |
# File 'lib/leancloud/datatypes.rb', line 307 def id @id end |
#local_filename ⇒ Object
‘{“__type”:“File”, “name”:“profile.png”, “url”=>“”}’
302 303 304 |
# File 'lib/leancloud/datatypes.rb', line 302 def local_filename @local_filename end |
#parse_filename ⇒ Object
eg “12-4-532d-d-g3-3-hello.text”
303 304 305 |
# File 'lib/leancloud/datatypes.rb', line 303 def parse_filename @parse_filename end |
#url ⇒ Object
Returns the value of attribute url.
306 307 308 |
# File 'lib/leancloud/datatypes.rb', line 306 def url @url end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
320 321 322 323 |
# File 'lib/leancloud/datatypes.rb', line 320 def eql?(other) self.class.equal?(other.class) && url == other.url end |
#hash ⇒ Object
327 328 329 |
# File 'lib/leancloud/datatypes.rb', line 327 def hash url.hash end |
#save ⇒ Object
331 332 333 334 335 336 337 338 |
# File 'lib/leancloud/datatypes.rb', line 331 def save uri = LC::Protocol.file_uri(@local_filename) resp = LC.client.request(uri, :post, @body, nil, @content_type) @parse_filename = resp["name"] @url = resp["url"] @id = resp["id"] resp end |
#to_h(*a) ⇒ Object Also known as: as_json
340 341 342 343 344 345 346 347 |
# File 'lib/leancloud/datatypes.rb', line 340 def to_h(*a) { Protocol::KEY_TYPE => Protocol::TYPE_FILE, "name" => @parse_filename, "url" => @url, "id" => @id } end |
#to_json(*a) ⇒ Object
350 351 352 |
# File 'lib/leancloud/datatypes.rb', line 350 def to_json(*a) to_h.to_json(*a) end |