Class: Dina::File

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/dina/models/object_store/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

custom_headers, endpoint_path, find_by_group, properties, site

Constructor Details

#initialize(attributes = {}) ⇒ File

Returns a new instance of File.



30
31
32
33
34
35
36
# File 'lib/dina/models/object_store/file.rb', line 30

def initialize(attributes = {})
  @id = attributes[:id] || SecureRandom.uuid
  @group = attributes[:group] || nil
  @is_derivative = attributes[:is_derivative] || false
  @file_path = attributes[:file_path] || nil
  @filename = attributes[:filename] || File.basename(@file_path) rescue nil
end

Instance Attribute Details

#file_pathObject

Returns the value of attribute file_path.



3
4
5
# File 'lib/dina/models/object_store/file.rb', line 3

def file_path
  @file_path
end

#filenameObject

Returns the value of attribute filename.



3
4
5
# File 'lib/dina/models/object_store/file.rb', line 3

def filename
  @filename
end

#groupObject

Returns the value of attribute group.



3
4
5
# File 'lib/dina/models/object_store/file.rb', line 3

def group
  @group
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/dina/models/object_store/file.rb', line 3

def id
  @id
end

#is_derivativeObject

Returns the value of attribute is_derivative.



3
4
5
# File 'lib/dina/models/object_store/file.rb', line 3

def is_derivative
  @is_derivative
end

Class Method Details

.create(attributes = {}) ⇒ Object



24
25
26
27
28
# File 'lib/dina/models/object_store/file.rb', line 24

def self.create(attributes = {})
  new(attributes).tap do |resource|
    resource.save
  end
end

.find(group:, id:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/dina/models/object_store/file.rb', line 13

def self.find(group:, id:)
  obj = self.new
  obj.group = group
  RestClient::Request.execute(
    method: :get,
    headers: { authorization: Dina.header },
    url: obj.url + "/#{id}",
    verify_ssl: verify_ssl
  )
end

.verify_sslObject



5
6
7
8
9
10
11
# File 'lib/dina/models/object_store/file.rb', line 5

def self.verify_ssl
  begin
    connection_options[:ssl][:verify]
  rescue
    true
  end
end

Instance Method Details

#endpointObject



38
39
40
# File 'lib/dina/models/object_store/file.rb', line 38

def endpoint
  Dina.config.endpoint_url
end

#endpoint_pathObject



42
43
44
# File 'lib/dina/models/object_store/file.rb', line 42

def endpoint_path
  "objectstore-api/"
end

#fileObject



54
55
56
57
58
59
60
61
# File 'lib/dina/models/object_store/file.rb', line 54

def file
  new_file = ::File.new(file_path)
  bound = filename.dup
  new_file.define_singleton_method(:original_filename) do
    bound
  end
  new_file
end

#saveObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dina/models/object_store/file.rb', line 63

def save
  validate_params
  response = RestClient::Request.execute(
    method: :post,
    headers: { authorization: Dina.header },
    url: (!is_derivative) ? url : url + "/derivative",
    payload: {
      multipart: true,
      file: file
    },
    verify_ssl: self.class.verify_ssl
  )
  json = JSON.parse(response, symbolize_names: true)
  self.id = json[:uuid]
  json.each{ |k,v| define_singleton_method(k.to_sym) { v } }
  json
end

#table_nameObject



46
47
48
# File 'lib/dina/models/object_store/file.rb', line 46

def table_name
  "file/#{group.downcase}"
end

#urlObject



50
51
52
# File 'lib/dina/models/object_store/file.rb', line 50

def url
  endpoint + "/" + endpoint_path + table_name
end