sdr-client 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5c716a01e8ec4c6fc081a8faa3be231bcd39c3da6481efaa43f7f04248aa717c
4
- data.tar.gz: b69d8f2c2942abb198764eab8025250c0dbd018580e8a535ebafc4b2fc2f2634
3
+ metadata.gz: 80a6d11a266d546eb1e0763b99b13b18d9f64c558ef21db50f3f509623a4f4bf
4
+ data.tar.gz: 5ec2344617583af3322f0426e39328aee355ee4c12d7338d8734f529110e8da6
5
5
  SHA512:
6
- metadata.gz: 44884b0decb3af139e287b932110aa614bde3892f692d22528adceee0b02bdf4a966c30abbca6c2a81e755a32fc3189b66de89c8899bae77f64ea30b356cdb81
7
- data.tar.gz: f38600d5355545f23acac4e89ee546f46d90c80cedf30b7becb4b8eac2bc1e3ffead361d2aa343fb67fc01eb9845f1bbd0da9a58bbdee7afd3fb4c5aa8cf23ba
6
+ metadata.gz: 3a145ef20a1c42abaed33ab79746778844e922b530a6c48d1b7f6d6f50a0e5a2c3f23e15a804fc5b75b3baeaac6baa4e67b35391965b5bb5da34892b345c9805
7
+ data.tar.gz: 2600c209c4f35f2ab7a8d7209189c690ec8f06de819666fd7bdd38f63290b997aa87355d54c7a5b5b2e4633ecfd02f75e908815923c2f44116373a999789bf68
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'dry/monads'
4
+ require 'faraday'
5
+
3
6
  require 'sdr_client/version'
4
7
  require 'sdr_client/deposit'
5
8
  require 'sdr_client/credentials'
6
9
  require 'sdr_client/login'
7
10
  require 'sdr_client/login_prompt'
8
11
  require 'sdr_client/cli'
9
- require 'faraday'
10
12
 
11
13
  module SdrClient
12
14
  class Error < StandardError; end
@@ -8,7 +8,8 @@ module SdrClient
8
8
  when 'deposit'
9
9
  SdrClient::Deposit.run(options)
10
10
  when 'login'
11
- SdrClient::Login.run(options)
11
+ status = SdrClient::Login.run(options)
12
+ puts status.value if status.failure?
12
13
  else
13
14
  raise "Unknown command #{command}"
14
15
  end
@@ -14,7 +14,7 @@ module SdrClient
14
14
  end
15
15
 
16
16
  def self.read
17
- return IO.readlines(credentials_file, chomp: true).first if File.exist?(credentials_file)
17
+ return IO.readlines(credentials_file, chomp: true).first if ::File.exist?(credentials_file)
18
18
 
19
19
  puts 'Log in first'
20
20
  exit(1)
@@ -25,5 +25,7 @@ end
25
25
  require 'json'
26
26
  require 'sdr_client/deposit/files/direct_upload_request'
27
27
  require 'sdr_client/deposit/files/direct_upload_response'
28
+ require 'sdr_client/deposit/file'
29
+ require 'sdr_client/deposit/file_set'
28
30
  require 'sdr_client/deposit/request'
29
31
  require 'sdr_client/deposit/process'
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrClient
4
+ module Deposit
5
+ # This represents the File metadata that we send to the server for doing a deposit
6
+ class File
7
+ def initialize(external_identifier:, label:, filename:, access: 'dark', preserve: false, shelve: false)
8
+ @external_identifier = external_identifier
9
+ @label = label
10
+ @filename = filename
11
+ @access = access
12
+ @preserve = preserve
13
+ @shelve = shelve
14
+ end
15
+
16
+ def as_json
17
+ {
18
+ "type": 'http://cocina.sul.stanford.edu/models/file.jsonld',
19
+ label: @label,
20
+ filename: @filename,
21
+ externalIdentifier: @external_identifier,
22
+ access: {
23
+ access: @access
24
+ },
25
+ administrative: {
26
+ sdrPreserve: @preserve,
27
+ shelve: @shelve
28
+ }
29
+ }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrClient
4
+ module Deposit
5
+ # This represents the FileSet metadata that we send to the server for doing a deposit
6
+ class FileSet
7
+ def initialize(uploads: [], files: [])
8
+ @files = if !uploads.empty?
9
+ uploads.map do |upload|
10
+ File.new(external_identifier: upload.signed_id, label: upload.filename, filename: upload.filename)
11
+ end
12
+ else
13
+ files
14
+ end
15
+ end
16
+
17
+ def as_json
18
+ {
19
+ "type": 'http://cocina.sul.stanford.edu/models/fileset.jsonld',
20
+ structural: {
21
+ hasMember: files.map(&:as_json)
22
+ }
23
+ }
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :files
29
+ end
30
+ end
31
+ end
@@ -9,9 +9,9 @@ module SdrClient
9
9
  def self.from_file(filename)
10
10
  checksum = Digest::MD5.file(filename).base64digest
11
11
  new(checksum: checksum,
12
- byte_size: File.size(filename),
12
+ byte_size: ::File.size(filename),
13
13
  content_type: 'text/html',
14
- filename: File.basename(filename))
14
+ filename: ::File.basename(filename))
15
15
  end
16
16
 
17
17
  def as_json
@@ -37,7 +37,7 @@ module SdrClient
37
37
  def check_files_exist
38
38
  logger.info('checking to see if files exist')
39
39
  files.each do |file_name|
40
- raise Errno::ENOENT, file_name unless File.exist?(file_name)
40
+ raise Errno::ENOENT, file_name unless ::File.exist?(file_name)
41
41
  end
42
42
  end
43
43
 
@@ -78,7 +78,7 @@ module SdrClient
78
78
  logger.info("Uploading `#{filename}' to #{url}")
79
79
 
80
80
  upload_response = connection.put(url) do |req|
81
- req.body = File.open(filename)
81
+ req.body = ::File.open(filename)
82
82
  req.headers['Content-Type'] = content_type
83
83
  req.headers['Content-Length'] = content_length.to_s
84
84
  end
@@ -6,21 +6,21 @@ module SdrClient
6
6
  class Request
7
7
  # @param [String] label the required object label
8
8
  # @param [String] type (http://cocina.sul.stanford.edu/models/object.jsonld) the required object type.
9
- # @param [Array<SdrClient::Deposit::Files::DirectUploadResponse>] uploads the uploaded files to attach.
9
+ # @param [Array<FileSet>] file_sets the file sets to attach.
10
10
  def initialize(label: nil,
11
11
  apo:,
12
12
  collection:,
13
13
  source_id:,
14
14
  catkey: nil,
15
15
  type: 'http://cocina.sul.stanford.edu/models/object.jsonld',
16
- uploads: [])
16
+ file_sets: [])
17
17
  @label = label
18
18
  @type = type
19
19
  @source_id = source_id
20
20
  @collection = collection
21
21
  @catkey = catkey
22
22
  @apo = apo
23
- @uploads = uploads
23
+ @file_sets = file_sets
24
24
  end
25
25
 
26
26
  def as_json
@@ -35,20 +35,28 @@ module SdrClient
35
35
  end
36
36
  end
37
37
 
38
+ # @param [Array<SdrClient::Deposit::Files::DirectUploadResponse>] uploads the uploaded files to attach.
38
39
  # @return [Request] a clone of this request with the uploads added
39
40
  def with_uploads(uploads)
41
+ file_sets = uploads.map { |upload| FileSet.new(uploads: [upload]) }
42
+
40
43
  Request.new(label: label,
41
44
  apo: apo,
42
45
  collection: collection,
43
46
  source_id: source_id,
44
47
  catkey: catkey,
45
48
  type: type,
46
- uploads: uploads)
49
+ file_sets: file_sets)
47
50
  end
48
51
 
52
+ # In this case there is a 1-1 mapping between Files and FileSets,
53
+ # but this doesn't always have to be the case. We could change this in the
54
+ # future so that we have one FileSet that has an Image and its OCR file.
55
+ def add_uploads_each_as_resource(uploads); end
56
+
49
57
  private
50
58
 
51
- attr_reader :label, :uploads, :source_id, :catkey, :apo, :collection, :type
59
+ attr_reader :label, :file_sets, :source_id, :catkey, :apo, :collection, :type
52
60
 
53
61
  def administrative
54
62
  {
@@ -65,24 +73,9 @@ module SdrClient
65
73
  def structural
66
74
  {
67
75
  isMemberOf: collection,
68
- hasMember: file_sets_as_json
76
+ hasMember: file_sets.map(&:as_json)
69
77
  }
70
78
  end
71
-
72
- # In this case there is a 1-1 mapping between Files and FileSets,
73
- # but this doesn't always have to be the case. We could change this in the
74
- # future so that we have one FileSet that has an Image and its OCR file.
75
- def file_sets_as_json
76
- uploads.map do |upload|
77
- {
78
- "type": 'http://cocina.sul.stanford.edu/models/fileset.jsonld',
79
- label: upload.filename,
80
- structural: {
81
- hasMember: [upload.signed_id]
82
- }
83
- }
84
- end
85
- end
86
79
  end
87
80
  end
88
81
  end
@@ -4,19 +4,22 @@ module SdrClient
4
4
  # The namespace for the "login" command
5
5
  module Login
6
6
  LOGIN_PATH = '/v1/auth/login'
7
+ extend Dry::Monads[:result]
8
+
9
+ # @return [Result] the status of the call
7
10
  def self.run(url:, login_service: LoginPrompt)
8
11
  request_json = JSON.generate(login_service.run)
9
12
  response = Faraday.post(url + LOGIN_PATH, request_json, 'Content-Type' => 'application/json')
10
13
  case response.status
11
14
  when 200
12
15
  Credentials.write(response.body)
16
+ Success()
13
17
  when 400
14
- puts 'Email address is not a valid email'
18
+ Failure('Email address is not a valid email')
15
19
  when 401
16
- puts 'Invalid username or password'
20
+ Failure('Invalid username or password')
17
21
  else
18
- puts "Status: #{response.status}"
19
- puts response.body
22
+ Failure("Status: #{response.status}\n#{response.body}")
20
23
  end
21
24
  end
22
25
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SdrClient
4
- VERSION = '0.2.1'
4
+ VERSION = '0.3.0'
5
5
  end
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ['lib']
29
29
 
30
+ spec.add_dependency 'dry-monads'
30
31
  spec.add_dependency 'faraday', '>= 0.16'
31
32
 
32
33
  spec.add_development_dependency 'bundler', '~> 2.0'
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sdr-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Coyne
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-13 00:00:00.000000000 Z
11
+ date: 2020-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-monads
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: faraday
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -148,6 +162,8 @@ files:
148
162
  - lib/sdr_client/cli.rb
149
163
  - lib/sdr_client/credentials.rb
150
164
  - lib/sdr_client/deposit.rb
165
+ - lib/sdr_client/deposit/file.rb
166
+ - lib/sdr_client/deposit/file_set.rb
151
167
  - lib/sdr_client/deposit/files/direct_upload_request.rb
152
168
  - lib/sdr_client/deposit/files/direct_upload_response.rb
153
169
  - lib/sdr_client/deposit/process.rb