Class: Opium::File
- Includes:
- Model::Connectable
- Defined in:
- lib/opium/file.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
- .model_name ⇒ Object
- .to_parse(object) ⇒ Object
- .to_ruby(object) ⇒ Object
- .upload(file, options = {}) ⇒ Object
Instance Method Summary collapse
- #delete(options = {}) ⇒ Object
-
#initialize(attributes = {}) ⇒ File
constructor
A new instance of File.
- #inspect ⇒ Object
- #mime_type ⇒ Object
- #to_parse ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ File
Returns a new instance of File.
71 72 73 74 75 |
# 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 |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
84 85 86 |
# File 'lib/opium/file.rb', line 84 def name @name end |
#url ⇒ Object
Returns the value of attribute url.
84 85 86 |
# File 'lib/opium/file.rb', line 84 def url @url end |
Class Method Details
.model_name ⇒ Object
11 12 13 |
# File 'lib/opium/file.rb', line 11 def model_name @model_name ||= ActiveModel::Name.new( self ) end |
.to_parse(object) ⇒ Object
34 35 36 37 38 39 40 41 |
# 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) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# 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 = {}) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/opium/file.rb', line 15 def upload( file, = {} ) = ( file, ) attributes = send( :http, :post, ) do |request| request.body = Faraday::UploadIO.new( file, [:headers][:content_type] ) end [:sent_headers] ? attributes : new(attributes) end |
Instance Method Details
#delete(options = {}) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/opium/file.rb', line 77 def delete( = {} ) 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, end.tap { self.freeze } end |
#inspect ⇒ Object
90 91 92 |
# 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 ⇒ Object
86 87 88 |
# File 'lib/opium/file.rb', line 86 def mime_type @mime_type ||= MimeMagic.by_path( url ) if url end |
#to_parse ⇒ Object
94 95 96 |
# File 'lib/opium/file.rb', line 94 def to_parse self.class.to_parse( self ) end |