Class: Parse::File
Overview
The default MIME type for all files is image/jpeg. This can be default can be changed by setting a value to `Parse::File.default_mime_type`.
This class represents a Parse file pointer. `Parse::File` has helper methods to upload Parse files directly to Parse and manage file associations with your classes.
Constant Summary collapse
- LEGACY_FILE_RX =
Regular expression that matches the old legacy Parse hosted file name
/^tfss-[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}-/
- ATTRIBUTES =
The default attributes in a Parse File hash.
{ __type: :string, name: :string, url: :string }.freeze
Constants inherited from Model
Model::CLASS_INSTALLATION, Model::CLASS_PRODUCT, Model::CLASS_ROLE, Model::CLASS_SESSION, Model::CLASS_USER, Model::ID, Model::KEY_CLASS_NAME, Model::KEY_CREATED_AT, Model::KEY_OBJECT_ID, Model::KEY_UPDATED_AT, Model::OBJECT_ID, Model::TYPE_ACL, Model::TYPE_BYTES, Model::TYPE_DATE, Model::TYPE_FIELD, Model::TYPE_FILE, Model::TYPE_GEOPOINT, Model::TYPE_NUMBER, Model::TYPE_OBJECT, Model::TYPE_POINTER, Model::TYPE_RELATION
Class Attribute Summary collapse
- .default_mime_type ⇒ String
The default mime type for created instances.
- .force_ssl ⇒ Boolean
When set to true, it will make all calls to File#url.
Instance Attribute Summary collapse
- #contents ⇒ Object
The contents of the file.
- #mime_type ⇒ String
The mime-type of the file whe.
- #name ⇒ String
The name of the file including extension (if any).
- #url ⇒ String
Returns the url string for this Parse::File pointer.
Class Method Summary collapse
- .basename(file_name, suffix = nil) ⇒ String
A proxy method for ::File.basename.
- .create(url) ⇒ Parse::File
This creates a new Parse File Object with from a URL, saves it and returns it.
- .parse_class ⇒ Model::TYPE_FILE
Instance Method Summary collapse
- #==(u) ⇒ Boolean
Two files are equal if they have the same url.
- #attributes ⇒ Hash
- #attributes=(h) ⇒ Object
Allows mass assignment from a Parse JSON hash.
- #initialize(name, contents = nil, mime_type = nil) ⇒ File constructor
The initializer to create a new file supports different inputs.
- #parse_class ⇒ Model::TYPE_FILE (also: #__type)
- #parse_hosted_file? ⇒ Boolean
True if this file is hosted by Parse's servers.
- #save ⇒ Boolean
Save the file by uploading it to Parse and creating a file pointer.
- #saved? ⇒ Boolean
A File object is considered saved if the basename of the URL and the name parameters are equal.
- #to_s ⇒ String
The url.
Methods inherited from Model
Methods included from Client::Connectable
Constructor Details
#initialize(name, contents = nil, mime_type = nil) ⇒ File
The initializer to create a new file supports different inputs. If the first paramter is a string which starts with 'http', we then download the content of the file (and use the detected mime-type) to set the content and mime_type fields. If the first parameter is a hash, we assume it might be the Parse File hash format which contains url and name fields only. If the first paramter is a Parse::File, then we copy fields over Otherwise, creating a new file requires a name, the actual contents (usually from a File.open(“local.jpg”).read ) and the mime-type
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | # File 'lib/parse/model/file.rb', line 80 def initialize(name, contents = nil, mime_type = nil) mime_type ||= Parse::File.default_mime_type if name.is_a?(String) && name.start_with?("http") #could be url string file = open(name) @contents = file.read @name = File.basename file.base_uri.to_s @mime_type = file.content_type elsif name.is_a?(Hash) self.attributes = name elsif name.is_a?(::File) @contents = contents || name.read @name = File.basename name.to_path elsif name.is_a?(Parse::File) @name = name.name @url = name.url else @name = name @contents = contents end if @name.blank? raise ArgumentError, "Invalid Parse::File initialization with name '#{@name}'" end @mime_type ||= mime_type end |
Class Attribute Details
.default_mime_type ⇒ String
Returns The default mime type for created instances. Default: _'image/jpeg'_.
56 57 58 | # File 'lib/parse/model/file.rb', line 56 def default_mime_type @default_mime_type end |
.force_ssl ⇒ Boolean
Returns When set to true, it will make all calls to File#url.
59 60 61 | # File 'lib/parse/model/file.rb', line 59 def force_ssl @force_ssl end |
Instance Attribute Details
#contents ⇒ Object
Returns the contents of the file.
39 40 41 | # File 'lib/parse/model/file.rb', line 39 def contents @contents end |
#mime_type ⇒ String
Returns the mime-type of the file whe.
42 43 44 | # File 'lib/parse/model/file.rb', line 42 def mime_type @mime_type end |
#name ⇒ String
Returns the name of the file including extension (if any).
34 35 36 | # File 'lib/parse/model/file.rb', line 34 def name @name end |
#url ⇒ String
Returns the url string for this Parse::File pointer. If the force_ssl option is set to true, it will make sure it returns a secure url.
36 37 38 | # File 'lib/parse/model/file.rb', line 36 def url @url end |
Class Method Details
.basename(file_name, suffix = nil) ⇒ String
A proxy method for ::File.basename
160 161 162 163 164 165 166 | # File 'lib/parse/model/file.rb', line 160 def self.basename(file_name, suffix = nil) if suffix.nil? ::File.basename(file_name) else ::File.basename(file_name, suffix) end end |
.create(url) ⇒ Parse::File
This creates a new Parse File Object with from a URL, saves it and returns it
110 111 112 113 114 115 | # File 'lib/parse/model/file.rb', line 110 def self.create(url) url = url.url if url.is_a?(Parse::File) file = self.new(url) file.save file end |
.parse_class ⇒ Model::TYPE_FILE
44 | # File 'lib/parse/model/file.rb', line 44 def self.parse_class; TYPE_FILE; end |
Instance Method Details
#==(u) ⇒ Boolean
Returns Two files are equal if they have the same url.
139 140 141 142 | # File 'lib/parse/model/file.rb', line 139 def ==(u) return false unless u.is_a?(self.class) @url == u.url end |
#attributes ⇒ Hash
134 135 136 | # File 'lib/parse/model/file.rb', line 134 def attributes ATTRIBUTES end |
#attributes=(h) ⇒ Object
Allows mass assignment from a Parse JSON hash.
145 146 147 148 149 150 151 152 153 | # File 'lib/parse/model/file.rb', line 145 def attributes=(h) if h.is_a?(String) @url = h @name = File.basename(h) elsif h.is_a?(Hash) @url = h[FIELD_URL] || h[:url] || @url @name = h[FIELD_NAME] || h[:name] || @name end end |
#parse_class ⇒ Model::TYPE_FILE Also known as: __type
46 | # File 'lib/parse/model/file.rb', line 46 def parse_class; self.class.parse_class; end |
#parse_hosted_file? ⇒ Boolean
Returns true if this file is hosted by Parse's servers.
183 184 185 186 | # File 'lib/parse/model/file.rb', line 183 def parse_hosted_file? return false if @url.blank? ::File.basename(@url).starts_with?("tfss-") || @url.starts_with?("http://files.parsetfss.com") end |
#save ⇒ Boolean
Save the file by uploading it to Parse and creating a file pointer.
170 171 172 173 174 175 176 177 178 179 180 | # File 'lib/parse/model/file.rb', line 170 def save unless saved? || @contents.nil? || @name.nil? response = client.create_file(@name, @contents, @mime_type) unless response.error? result = response.result @name = result[FIELD_NAME] || File.basename(result[FIELD_URL]) @url = result[FIELD_URL] end end saved? end |
#saved? ⇒ Boolean
A File object is considered saved if the basename of the URL and the name parameters are equal
119 120 121 | # File 'lib/parse/model/file.rb', line 119 def saved? @url.present? && @name.present? && @name == File.basename(@url) end |
#to_s ⇒ String
Returns the url.
195 196 197 | # File 'lib/parse/model/file.rb', line 195 def to_s @url end |