class Opium::File
Attributes
name[RW]
url[RW]
Public Class Methods
model_name()
click to toggle source
# File lib/opium/file.rb, line 11 def model_name @model_name ||= ActiveModel::Name.new( self ) end
new( attributes = {} )
click to toggle source
# File lib/opium/file.rb, line 71 def initialize( attributes = {} ) attributes.with_indifferent_access.each do |k, v| send( :"#{k}=", v ) unless k == '__type' end end
to_parse( object )
click to toggle source
# File lib/opium/file.rb, line 34 def to_parse( object ) return unless object fail ArgumentError, "could not convert #{ object.inspect } to a Parse File object" unless object.is_a?( self ) && object.name { __type: 'File', name: object.name }.with_indifferent_access end
to_ruby( object )
click to toggle source
# File lib/opium/file.rb, line 23 def to_ruby( object ) return if object.nil? || object == '' return object if object.is_a?( self ) object = ::JSON.parse( object ) if object.is_a?( String ) if object.is_a?( Hash ) && (has_key_of_value( object, :__type, 'File' ) || has_keys( object, :url, :name )) new( object ) else fail ArgumentError, "could not convert #{ object.inspect } to an Opium::File" end end
upload( file, options = {} )
click to toggle source
# File lib/opium/file.rb, line 15 def upload( file, options = {} ) request_options = build_request_options( file, options ) attributes = send( :http, :post, request_options ) do |request| request.body = Faraday::UploadIO.new( file, request_options[:headers][:content_type] ) end options[:sent_headers] ? attributes : new(attributes) end
Private Class Methods
build_request_options( file, options )
click to toggle source
Note that MimeMagic returns application/zip for the more recent MS Office file types, hence the extra check.
# File lib/opium/file.rb, line 47 def build_request_options( file, options ) {}.tap do |result| mime_type = options.fetch( :content_type, MimeMagic.by_magic(file) ) mime_type = MimeMagic.by_path(file) if mime_type == 'application/zip' result[:id] = parameterize_name( options[:original_filename] || ::File.basename( file ) ) result[:headers] = { content_type: mime_type.to_s, content_length: file.size.to_s } result[:sent_headers] = options[:sent_headers] if options.key? :sent_headers end end
has_key_of_value( object, key, value )
click to toggle source
# File lib/opium/file.rb, line 57 def has_key_of_value( object, key, value ) (object[key] || object[key.to_s]) == value end
has_keys( object, *keys )
click to toggle source
# File lib/opium/file.rb, line 61 def has_keys( object, *keys ) object.keys.all? {|key| keys.include?( key.to_s ) || keys.include?( key.to_sym )} end
parameterize_name( name )
click to toggle source
# File lib/opium/file.rb, line 65 def parameterize_name( name ) without_extension, extension = ::File.basename( name, '.*' ), ::File.extname( name ) without_extension.parameterize + extension end
Public Instance Methods
delete( options = {} )
click to toggle source
# File lib/opium/file.rb, line 77 def delete( options = {} ) fail "cannot delete #{ self.inspect }, as there is no name" unless self.name self.class.with_heightened_privileges do self.class.http_delete self.name, options end.tap { self.freeze } end
inspect()
click to toggle source
# File lib/opium/file.rb, line 90 def inspect "#<#{ self.class.model_name.name } name=#{ name.inspect } url=#{ url.inspect } mime_type=#{ (mime_type ? mime_type.type : nil).inspect }>" end
mime_type()
click to toggle source
# File lib/opium/file.rb, line 86 def mime_type @mime_type ||= MimeMagic.by_path( url ) if url end
to_parse()
click to toggle source
# File lib/opium/file.rb, line 94 def to_parse self.class.to_parse( self ) end