Class: Uploadcare::Api::File

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/uploadcare/resources/file.rb

Instance Method Summary collapse

Constructor Details

#initialize(api, uuid_or_cdn_url, data = nil) ⇒ File

Returns a new instance of File.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/uploadcare/resources/file.rb', line 6

def initialize api, uuid_or_cdn_url, data=nil
  result = Uploadcare::Parser.parse_file_string uuid_or_cdn_url
  
  file = {uuid: result["uuid"], operations: result["operations"]}

  @api = api

  super file

  set_data(data) if data
end

Instance Method Details

#cdn_url(add_operations = false) ⇒ Object Also known as: public_url



18
19
20
21
22
23
24
# File 'lib/uploadcare/resources/file.rb', line 18

def cdn_url add_operations=false
  if add_operations
    cdn_url_with_operations
  else
    cdn_url_without_operations
  end
end

#cdn_url_with_operationsObject Also known as: public_url_with_operations



34
35
36
37
38
39
40
41
# File 'lib/uploadcare/resources/file.rb', line 34

def cdn_url_with_operations
  url = cdn_url_without_operations
  unless operations.empty?
    ops = operations.join("/-/")
    url = url + "-/#{ops}/"
  end
  url
end

#cdn_url_without_operationsObject Also known as: public_url_without_operations



28
29
30
# File 'lib/uploadcare/resources/file.rb', line 28

def cdn_url_without_operations
  @api.options[:static_url_base] + "/#{@table[:uuid]}/"
end

#copy(with_operations = true, target = nil) ⇒ Object

copy file to target location note what file copied with operations



97
98
99
100
101
102
103
104
# File 'lib/uploadcare/resources/file.rb', line 97

def copy with_operations=true, target=nil
  data = Hash.new
  data[:target] = target if target
  data[:source] = self.cdn_url_with_operations if with_operations
  data[:source] = self.cdn_url_without_operations unless with_operations

  @api.post "/files/", data
end

#deleteObject



79
80
81
82
83
# File 'lib/uploadcare/resources/file.rb', line 79

def delete
  data = @api.delete "/files/#{uuid}/storage/"
  set_data data
  self
end

#is_deleted?Boolean Also known as: deleted?, removed?, is_removed?

nil is returning if there is no way to say for sure

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/uploadcare/resources/file.rb', line 86

def is_deleted?
  return nil unless is_loaded?
  !send(:datetime_removed).nil?
end

#is_loaded?Boolean Also known as: loaded?

Returns:

  • (Boolean)


59
60
61
# File 'lib/uploadcare/resources/file.rb', line 59

def is_loaded?
  !send(:datetime_uploaded).nil?
end

#is_stored?Boolean Also known as: stored?

nil is returning if there is no way to say for sure

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/uploadcare/resources/file.rb', line 72

def is_stored?
  return nil unless is_loaded?
  !send(:datetime_stored).nil?
end

#load_dataObject Also known as: load



45
46
47
48
# File 'lib/uploadcare/resources/file.rb', line 45

def load_data
  load_data! unless is_loaded?
  self
end

#load_data!Object Also known as: load!



51
52
53
54
55
56
# File 'lib/uploadcare/resources/file.rb', line 51

def load_data!
  data = @api.get "/files/#{uuid}/"
  set_data(data)

  self
end

#storeObject



65
66
67
68
69
# File 'lib/uploadcare/resources/file.rb', line 65

def store
  data = @api.put "/files/#{uuid}/storage/"
  set_data data
  self 
end