Class: Stash::Sword::Client

Inherits:
Object
  • Object
show all
Includes:
HeaderUtils, LogUtils
Defined in:
lib/stash/sword/client.rb

Constant Summary collapse

EOL =
"\r\n".freeze

Constants included from HeaderUtils

HeaderUtils::APPLICATION_ZIP, HeaderUtils::CONTENT_DISPOSITION, HeaderUtils::MULTIPART_RELATED_ATOM_XML, HeaderUtils::SIMPLE_ZIP

Instance Attribute Summary collapse

Attributes included from HeaderUtils

#on_behalf_of

Instance Method Summary collapse

Methods included from HeaderUtils

#create_request_headers, #update_mime_headers, #update_request_headers

Methods included from LogUtils

create_default_logger, #default_logger, #hash_to_log_msg, #level, #log, #log_error, #log_hash, #to_log_msg

Constructor Details

#initialize(collection_uri:, username:, password:, on_behalf_of: nil, logger: nil, helper: nil) ⇒ Client

Creates a new Stash::Sword::Client for the specified collection URI, with the specified credentials.

Parameters:

  • collection_uri (URI, String)

    The collection URI

  • username (String)

    the username

  • password (String)

    the password

  • on_behalf_of (String, nil) (defaults to: nil)

    the user for whom the original sword package was deposited on behalf of. Defaults to username.

  • logger (Logger, nil) (defaults to: nil)

    the logger to use, or nil to use a default logger



29
30
31
32
33
34
35
36
37
# File 'lib/stash/sword/client.rb', line 29

def initialize(collection_uri:, username:, password:, on_behalf_of: nil, logger: nil, helper: nil) # rubocop:disable Metrics/ParameterLists
  validate(collection_uri, password, username)
  @collection_uri = to_uri(collection_uri)
  @username     = username
  @password     = password
  @on_behalf_of = on_behalf_of || username
  @helper       = helper || HTTPHelper.new(username: username, password: password, user_agent: "stash-sword #{VERSION}", logger: logger)
  @log          = logger || default_logger
end

Instance Attribute Details

#collection_uri (readonly)

Returns the value of attribute collection_uri.



16
17
18
# File 'lib/stash/sword/client.rb', line 16

def collection_uri
  @collection_uri
end

#helper (readonly)

Returns the value of attribute helper.



19
20
21
# File 'lib/stash/sword/client.rb', line 19

def helper
  @helper
end

#password (readonly)

Returns the value of attribute password.



18
19
20
# File 'lib/stash/sword/client.rb', line 18

def password
  @password
end

#username (readonly)

Returns the value of attribute username.



17
18
19
# File 'lib/stash/sword/client.rb', line 17

def username
  @username
end

Instance Method Details

#create(doi:, zipfile:) ⇒ DepositReceipt

Creates a new resource for the specified DOI with the specified zipfile

Parameters:

  • doi (String)

    the DOI

  • zipfile (String)

    the zipfile path

Returns:



44
45
46
47
48
49
50
51
52
# File 'lib/stash/sword/client.rb', line 44

def create(doi:, zipfile:)
  log.debug("Stash::Sword::Client.create(doi: #{doi}, zipfile: #{zipfile})")
  uri = collection_uri.to_s
  response = do_post(uri, zipfile, create_request_headers(zipfile, doi))
  receipt_from(response)
rescue => e
  log_error(e)
  raise
end

#update(edit_iri:, zipfile:) ⇒ Integer

Updates a resource with a new zipfile

Parameters:

  • edit_iri (URI, String)

    the Atom Edit-IRI

  • zipfile (String)

    the zipfile path

Returns:

  • (Integer)

    the response code (if the request succeeds)



59
60
61
62
63
64
65
66
67
68
# File 'lib/stash/sword/client.rb', line 59

def update(edit_iri:, zipfile:)
  log.debug("Stash::Sword::Client.update(edit_iri: #{edit_iri}, zipfile: #{zipfile})")
  uri = to_uri(edit_iri).to_s
  response = do_put(uri, zipfile)
  log.debug(to_log_msg(response))
  response.code # TODO: what if anything should we return here?
rescue => e
  log_error(e)
  raise
end