Class: RequestBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/request_builder.rb

Overview

Given a hash of parameters, this builds a request of an appropriate type. It does the following:

Contacts third-party services to ensure metadata is present and accurate. Provisions a location for the file to be uploaded Populates the upload_link field of the request

This process is synchronous.

Instance Method Summary collapse

Instance Method Details

#create(bag_id:, content_type:, external_id:, user:) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/request_builder.rb', line 12

def create(bag_id:, content_type:, external_id:, user:)
  duplicate = Package.find_by_bag_id(bag_id)
  duplicate ||= Package.find_by_external_id(external_id)
  unless duplicate.nil?
    return :duplicate, duplicate
  end

  request = Package.new(
    bag_id: bag_id,
    external_id: external_id,
    content_type: content_type,
    user: user
    )
  if request.valid?
    request.save!
    return :created, request
  else
    return :invalid, request
  end
end