Class: Dina::FileConnection

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ FileConnection

Returns a new instance of FileConnection.

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dina/models/object_store/file_connection.rb', line 6

def initialize(options = {})
  site = options.fetch(:site)
  connection_options = options.slice(:proxy, :ssl, :request, :headers, :params)
  adapter_options = Array(options.fetch(:adapter, Faraday.default_adapter))

  @faraday = Faraday.new(site, connection_options) do |builder|
    builder.request :multipart
    builder.response :json
    builder.adapter(*adapter_options)
  end
  yield(self) if block_given?
end

Instance Method Details

#run(request_method, path, params: nil, headers: {}, body: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dina/models/object_store/file_connection.rb', line 19

def run(request_method, path, params: nil, headers: {}, body: nil)
  path = path + "/#{body[:data]["attributes"]["group"].downcase}"
  if body[:data]["attributes"].key?("isDerivative")
    path = path + "/derivative"
  end
  file_path = body[:data]["attributes"]["filePath"]
  mime_type = body[:data]["attributes"]["dcFormat"]
  file_name = body[:data]["attributes"]["fileName"]
  
  body[:file] = Faraday::Multipart::FilePart.new(
    file_path,
    mime_type,
    file_name
  )

  response = @faraday.run_request(request_method, path, body, headers) do |request|
    request.params.update(params) if params
  end
  attributes = response.body.dup
  response.body["meta"] = {}
  response.body["errors"] = []
  response.body["data"] = { 
    "id" => attributes["uuid"],
    "type" => "file",
    "relationships" => {},
    "attributes" => attributes
  }
  response
end