Class: Parse::ParseFile
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #delete! ⇒ Object
-
#initialize(hash) ⇒ ParseFile
constructor
A new instance of ParseFile.
- #inspect ⇒ Object
- #load(&block) ⇒ Object
- #save ⇒ Object
- #store(filepath = nil) ⇒ Object
- #to_h ⇒ Object
- #to_json(*args) ⇒ Object
Methods included from Util
Constructor Details
#initialize(hash) ⇒ ParseFile
Returns a new instance of ParseFile.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/parse/file.rb', line 8 def initialize hash hash = string_keyed_hash hash @name = hash['name'] raise 'name is mandatory' unless @name @url = hash['url'] @content = hash['content'] @type = hash['type'] || { '.txt' => 'text/plain', '.html' => 'text/html', '.jpg' => 'image/jpeg', '.jpeg' => 'image/jpeg', '.png' => 'image/png', '.gif' => 'image/gif' }[File.extname(@name).downcase] @client = hash['parce_client'] || Parse::Client.default end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
6 7 8 |
# File 'lib/parse/file.rb', line 6 def content @content end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/parse/file.rb', line 6 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
6 7 8 |
# File 'lib/parse/file.rb', line 6 def type @type end |
#url ⇒ Object
Returns the value of attribute url.
6 7 8 |
# File 'lib/parse/file.rb', line 6 def url @url end |
Instance Method Details
#delete! ⇒ Object
36 37 38 39 40 41 |
# File 'lib/parse/file.rb', line 36 def delete! raise "File should be fetched" unless @url @client.use_master_key do @client.call_api :delete, "files/#{@name}", nil, 'Content-Type' => nil, 'Accept' => nil end end |
#inspect ⇒ Object
61 62 63 64 65 66 |
# File 'lib/parse/file.rb', line 61 def inspect content, @content = @content, '..snip..' ret = super @content = content ret end |
#load(&block) ⇒ Object
43 44 45 46 47 |
# File 'lib/parse/file.rb', line 43 def load &block open @url do |content| @content = content.read end unless @content block.call @content if block @content end |
#save ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/parse/file.rb', line 25 def save raise "Files cannot be updated." if @url if @type =~ %r|^image/| @content = @content.respond_to?(:read) ? @content.read : File.read(@content) end @client.call_api :post, "files/#{@name}", @content, 'Content-Type' => @type, 'Accept' => nil do |resp_body| @name = resp_body['name'] @url = resp_body['url'] end end |
#store(filepath = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/parse/file.rb', line 49 def store filepath=nil filepath ||= @name raise 'filepath is mandatory' unless filepath FileUtils.mkdir_p File.dirname(filepath) load do |content| open filepath, 'wb' do |file| file.write content end end end |
#to_h ⇒ Object
68 69 70 71 72 73 |
# File 'lib/parse/file.rb', line 68 def to_h { "__type" => "File", "name" => @name } end |
#to_json(*args) ⇒ Object
75 76 77 |
# File 'lib/parse/file.rb', line 75 def to_json *args to_h.to_json end |