Class: Opium::File

Inherits:
Object show all
Includes:
Model::Connectable
Defined in:
lib/opium/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#nameObject

Returns the value of attribute name.



84
85
86
# File 'lib/opium/file.rb', line 84

def name
  @name
end

#urlObject

Returns the value of attribute url.



84
85
86
# File 'lib/opium/file.rb', line 84

def url
  @url
end

Class Method Details

.model_nameObject



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, 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

Instance Method Details

#delete(options = {}) ⇒ Object



77
78
79
80
81
82
# 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

#inspectObject



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_typeObject



86
87
88
# File 'lib/opium/file.rb', line 86

def mime_type
  @mime_type ||= MimeMagic.by_path( url ) if url
end

#to_parseObject



94
95
96
# File 'lib/opium/file.rb', line 94

def to_parse
  self.class.to_parse( self )
end