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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dina/models/object_store/file_connection.rb', line 19

def run(request_method, path, params: nil, headers: {}, body: nil)
  if request_method == :get && path == "file/download"
    path = "file" + "/#{params[:group]}/#{params[:fileId]}"
    if params[:isDerivative]
      path = "file" + "/#{params[:group]}/derivative/#{params[:fileId]}"
    end
    headers[:content_type] = "application/octet-stream"


    response = @faraday.run_request(request_method, path, body, headers) do |request|
    end
    response.body["meta"] = {}
    response.body["errors"] = []
    response.body["data"] = { 
      "id" => params[:fileId],
      "type" => "file",
      "relationships" => {},
      "attributes" => response.headers
    }
    response
  else
    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
end