Class: PushmiPullyu::AIP::FedoraFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/pushmi_pullyu/aip/fedora_fetcher.rb

Defined Under Namespace

Classes: FedoraFetchError

Constant Summary collapse

RDF_FORMAT =
'text/rdf+n3'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(noid) ⇒ FedoraFetcher

Returns a new instance of FedoraFetcher.


9
10
11
# File 'lib/pushmi_pullyu/aip/fedora_fetcher.rb', line 9

def initialize(noid)
  @noid = noid
end

Instance Method Details

#download_object(download_path, url_extra: nil, optional: false, is_rdf: false, should_add_user_email: false) ⇒ Object

Return true on success, raise an error otherwise (or use 'optional' to return false on 404)


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
# File 'lib/pushmi_pullyu/aip/fedora_fetcher.rb', line 21

def download_object(download_path, url_extra: nil,
                    optional: false, is_rdf: false,
                    should_add_user_email: false)

  uri = URI(object_url(url_extra))

  request = Net::HTTP::Get.new(uri)
  request.basic_auth(PushmiPullyu.options[:fedora][:user],
                     PushmiPullyu.options[:fedora][:password])

  request['Accept'] = RDF_FORMAT if is_rdf

  response = Net::HTTP.start(uri.hostname, uri.port) do |http|
    http.request(request)
  end

  if response.is_a?(Net::HTTPSuccess)
    body = if should_add_user_email
             PushmiPullyu::AIP::OwnerEmailEditor.new(response.body).run
           else
             response.body
           end
    file = File.open(download_path, 'wb')
    file.write(body)
    file.close
    return true
  elsif response.is_a?(Net::HTTPNotFound)
    raise FedoraFetchError unless optional

    return false
  else
    raise FedoraFetchError
  end
end

#object_url(url_extra = nil) ⇒ Object


13
14
15
16
17
# File 'lib/pushmi_pullyu/aip/fedora_fetcher.rb', line 13

def object_url(url_extra = nil)
  url = "#{PushmiPullyu.options[:fedora][:url]}#{base_path}/#{pairtree}"
  url += url_extra if url_extra
  url
end