class Parse::ParseFile

Attributes

content[RW]
name[RW]
type[RW]
url[RW]

Public Class Methods

new(hash) click to toggle source
# 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

Public Instance Methods

delete!() click to toggle source
# 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() click to toggle source
Calls superclass method
# File lib/parse/file.rb, line 61
def inspect
  content, @content = @content, '..snip..'
  ret = super
  @content = content
  ret
end
load(&block) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/parse/file.rb, line 68
def to_h
  {
    "__type" => "File",
    "name" => @name
  }
end
to_json(*args) click to toggle source
# File lib/parse/file.rb, line 75
def to_json *args
  to_h.to_json
end