stash-sword 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: 3e0a9bb5934ab1313698dde7e68177a63ffb0597
4
- data.tar.gz: 2ad46f630eb85d4349e24c8bf866607358c5ae4c
3
+ metadata.gz: 0037ff658b5959ae0dca3bdfed3af072d81a7755
4
+ data.tar.gz: 6677bb48ecbe7d07e25d1bf27c1737250f4c1ae9
5
5
  SHA512:
6
- metadata.gz: 66c005b599f1359ad1fe9884a065d4a96619af9764b791c034f8c5f197f413ef50a2d08c0f654ad662496aae6c387cee9f2d9ebed96ae9503a996930dfcebaae
7
- data.tar.gz: 124321ab9db2a6d31f43f16fb717f595337f92778219f45521adccb74d63af3e8ee26d5afbe65c078164f9175374c4aaa1435522a9809e124934d995addadc57
6
+ metadata.gz: 6a0e6a7532fb0e39ae48dd242e9b20f03cae6870f43addbbb20c6d137f49cddcec1510798c9e33fbb516c8a6b71906afafc2c8c647cfdb9bc6cdd97417d28d57
7
+ data.tar.gz: 0265b3b0cfc3fd3d0ea4eac70bafd83f528632259e9bbc4943cd869b0aa5cd490e1a7e880a749bb73c13e9a400eb7f2c246fbe87a543d8092b8f1ff3517e4866
data/.gitignore CHANGED
@@ -15,6 +15,8 @@
15
15
  *.a
16
16
  mkmf.log
17
17
 
18
+ /*.gem
19
+
18
20
  # Database
19
21
 
20
22
  db/*.sqlite3
data/CHANGES.md ADDED
@@ -0,0 +1,7 @@
1
+ ## 0.1.1 (23 June 2016)
2
+
3
+ - `logger` is now a parameter passed to `Stash::Sword::Client` instead of a global singleton.
4
+
5
+ ## 0.1.0 (8 June 2016)
6
+
7
+ - Initial release.
@@ -4,23 +4,23 @@ require 'stash/sword'
4
4
 
5
5
  include Stash::Sword
6
6
 
7
- username, password, collection = ARGV
7
+ password = ARGV[0]
8
+ username = 'ucop_dash_submitter'
9
+ collection = 'dash_cdl'
10
+ collection_uri = "http://uc3-mrtsword-dev.cdlib.org:39001/mrtsword/collection/#{collection}"
11
+ zipfile = File.expand_path('../uploads/example.zip', __FILE__)
12
+
13
+ doi = "doi:10.5072/FK#{Time.now.to_i}"
8
14
 
9
15
  client = Client.new(
10
16
  username: username,
11
17
  password: password,
12
- collection_uri: URI("http://uc3-mrtsword-dev.cdlib.org:39001/mrtsword/collection/#{collection}")
18
+ collection_uri: URI(collection_uri)
13
19
  )
14
20
 
15
- doi = "doi:10.5072/FK#{Time.now.to_i}"
16
- zipfile = File.expand_path('../uploads/example.zip', __FILE__)
17
-
18
21
  receipt = client.create(doi: doi, zipfile: zipfile)
19
22
  em_iri = receipt.em_iri
20
- se_iri = receipt.se_iri
23
+ edit_iri = receipt.edit_iri
21
24
 
22
25
  puts "em_iri: #{em_iri}"
23
- puts "se_iri: #{se_iri}"
24
-
25
- code = client.update(se_iri: se_iri, zipfile: zipfile)
26
- puts "update response code: #{code}"
26
+ puts "edit_iri: #{edit_iri}"
@@ -0,0 +1,22 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'stash/sword'
4
+
5
+ include Stash::Sword
6
+
7
+ password = ARGV[0]
8
+ username = 'ucop_dash_submitter'
9
+ collection = 'dash_cdl'
10
+ collection_uri = "http://uc3-mrtsword-dev.cdlib.org:39001/mrtsword/collection/#{collection}"
11
+ zipfile = File.expand_path('../uploads/example.zip', __FILE__)
12
+
13
+ edit_iri = 'http://sword-aws-dev.cdlib.org:39001/mrtsword/edit/dash_cdl/doi%3A10.5072%2FFK1465424720'
14
+
15
+ client = Client.new(
16
+ username: username,
17
+ password: password,
18
+ collection_uri: URI(collection_uri)
19
+ )
20
+
21
+ code = client.update(edit_iri: edit_iri, zipfile: zipfile)
22
+ puts "update response code: #{code}"
data/lib/stash/sword.rb CHANGED
@@ -1,39 +1,5 @@
1
- require 'uri'
2
- require 'logger'
3
-
4
1
  module Stash
5
-
6
- # TODO: Make this configurable
7
- LOG_LEVEL = case ENV['RAILS_ENV'].to_s.downcase
8
- when 'test'
9
- Logger::DEBUG
10
- when 'development'
11
- Logger::INFO
12
- else
13
- Logger::WARN
14
- end
15
-
16
2
  module Sword
17
-
18
3
  Dir.glob(File.expand_path('../sword/*.rb', __FILE__)).sort.each(&method(:require))
19
-
20
- def self.log
21
- @log ||= new_logger(logdev: $stdout)
22
- end
23
-
24
- def self.log_device=(value)
25
- @log = new_logger(logdev: value)
26
- end
27
-
28
- def self.new_logger(logdev:, level: Stash::LOG_LEVEL, shift_age: 10, shift_size: 1024 * 1024)
29
- logger = Logger.new(logdev, shift_age, shift_size)
30
- logger.level = level
31
- logger.formatter = proc do |severity, datetime, progname, msg|
32
- "#{datetime.to_time.utc} #{severity} -#{progname}- #{msg}\n"
33
- end
34
- logger
35
- end
36
-
37
- private_class_method :new_logger
38
4
  end
39
5
  end
@@ -4,6 +4,7 @@ require 'stash/sword/header_utils'
4
4
  require 'stash/sword/log_utils'
5
5
  require 'stash/sword/http_helper'
6
6
  require 'stash/sword/sequence_io'
7
+ require 'logger'
7
8
 
8
9
  module Stash
9
10
  module Sword
@@ -24,7 +25,8 @@ module Stash
24
25
  # @param password [String] the password
25
26
  # @param on_behalf_of [String, nil] the user for whom the original sword package was deposited on behalf of.
26
27
  # Defaults to `username`.
27
- def initialize(collection_uri:, username:, password:, on_behalf_of: nil, helper: nil)
28
+ # @param logger [Logger, nil] the logger to use, or nil to use a default logger
29
+ def initialize(collection_uri:, username:, password:, on_behalf_of: nil, logger: nil, helper: nil)
28
30
  raise 'no collection URI provided' unless collection_uri
29
31
  raise 'no username provided' unless username
30
32
  raise 'no password provided' unless password
@@ -32,7 +34,8 @@ module Stash
32
34
  @username = username
33
35
  @password = password
34
36
  @on_behalf_of = on_behalf_of || username
35
- @helper = helper || HTTPHelper.new(username: username, password: password, user_agent: "stash-sword #{VERSION}")
37
+ @helper = helper || HTTPHelper.new(username: username, password: password, user_agent: "stash-sword #{VERSION}", logger: logger)
38
+ @log = logger || default_logger
36
39
  end
37
40
 
38
41
  # Creates a new resource for the specified DOI with the specified zipfile
@@ -118,7 +121,11 @@ module Stash
118
121
  update_mime_headers(zipfile).each { |k, v| content << "#{k}: #{v}#{EOL}" }
119
122
  content << EOL
120
123
  content << zipfile
124
+ content << EOL
121
125
  content << "--#{boundary}--#{EOL}"
126
+
127
+ log.debug("Payload:\n\t#{content.map(&:to_s).join("\t")}")
128
+
122
129
  SequenceIO.new(content).binmode
123
130
  end
124
131
 
@@ -5,13 +5,15 @@ module Stash
5
5
  SIMPLE_ZIP = 'http://purl.org/net/sword/package/SimpleZip'.freeze
6
6
  APPLICATION_ZIP = 'application/zip'.freeze
7
7
  MULTIPART_RELATED_ATOM_XML = 'multipart/related; type="application/atom+xml"'.freeze
8
+ # CONTENT_DISPOSITION = 'attachment'.freeze
9
+ CONTENT_DISPOSITION = 'form-data'.freeze
8
10
 
9
11
  attr_reader :on_behalf_of
10
12
 
11
13
  def create_request_headers(zipfile, slug)
12
14
  {
13
15
  'Content-Type' => APPLICATION_ZIP,
14
- 'Content-Disposition' => "attachment; filename=#{File.basename(zipfile)}",
16
+ 'Content-Disposition' => "#{CONTENT_DISPOSITION}; filename=#{File.basename(zipfile)}",
15
17
  'Packaging' => SIMPLE_ZIP,
16
18
  'Content-MD5' => Digest::MD5.file(zipfile).to_s,
17
19
  'On-Behalf-Of' => on_behalf_of,
@@ -31,7 +33,7 @@ module Stash
31
33
  def update_mime_headers(zipfile)
32
34
  {
33
35
  'Content-Type' => APPLICATION_ZIP,
34
- 'Content-Disposition' => "attachment; name=payload; filename=\"#{File.basename(zipfile)}\"",
36
+ 'Content-Disposition' => "#{CONTENT_DISPOSITION}; name=\"payload\"; filename=\"#{File.basename(zipfile)}\"",
35
37
  'Packaging' => SIMPLE_ZIP,
36
38
  'Content-MD5' => Digest::MD5.file(zipfile).to_s,
37
39
  'MIME-Version' => '1.0'
@@ -30,11 +30,13 @@ module Stash
30
30
  # @param user_agent [String] the User-Agent string to send when making requests
31
31
  # @param redirect_limit [Integer] the number of redirects to follow before erroring out
32
32
  # (defaults to {DEFAULT_MAX_REDIRECTS})
33
- def initialize(user_agent:, username: nil, password: nil, redirect_limit: DEFAULT_MAX_REDIRECTS)
33
+ # @param logger [Logger, nil] the logger to use, or nil to use a default logger
34
+ def initialize(user_agent:, username: nil, password: nil, redirect_limit: DEFAULT_MAX_REDIRECTS, logger: nil)
34
35
  @user_agent = user_agent
35
36
  @redirect_limit = redirect_limit
36
37
  @username = username
37
38
  @password = password
39
+ @log = logger || default_logger
38
40
  end
39
41
 
40
42
  # Gets the content of the specified URI as a string.
@@ -1,8 +1,9 @@
1
1
  module Stash
2
2
  module Sword
3
3
  module LogUtils
4
+
4
5
  def log
5
- ::Stash::Sword.log
6
+ @log ||= default_logger
6
7
  end
7
8
 
8
9
  def log_error(e)
@@ -34,6 +35,28 @@ module Stash
34
35
  "#{k}: #{value}"
35
36
  end.join("\n")
36
37
  end
38
+
39
+ def level
40
+ # TODO: make this configurable
41
+ @level ||= case ENV['RAILS_ENV'].to_s.downcase
42
+ when 'test'
43
+ Logger::DEBUG
44
+ when 'development'
45
+ Logger::INFO
46
+ else
47
+ Logger::WARN
48
+ end
49
+ end
50
+
51
+ def default_logger
52
+ logger = Logger.new($stdout, 10, 1024 * 1024)
53
+ logger.level = level
54
+ logger.formatter = proc do |severity, datetime, progname, msg|
55
+ "#{datetime.to_time.utc} #{severity} -#{progname}- #{msg}\n"
56
+ end
57
+ logger
58
+ end
59
+
37
60
  end
38
61
  end
39
62
  end
@@ -4,7 +4,7 @@ module Stash
4
4
  NAME = 'stash-sword'.freeze
5
5
 
6
6
  # The version of this gem
7
- VERSION = '0.1.0'.freeze
7
+ VERSION = '0.1.1'.freeze
8
8
 
9
9
  # The copyright notice for this gem
10
10
  COPYRIGHT = 'Copyright (c) 2016 The Regents of the University of California'.freeze
@@ -113,6 +113,6 @@ RSpec::Matchers.define :include_header do |k, v|
113
113
  end
114
114
 
115
115
  failure_message do |actual|
116
- "expected #{k}: #{v} but found #{value_for(key: k, in_hash: actual) || 'nil'}"
116
+ "expected #{k} to be '#{v}' but found '#{value_for(key: k, in_hash: actual) || 'nil'}'"
117
117
  end
118
118
  end
@@ -33,12 +33,14 @@ module Stash
33
33
  actual_headers = req.headers
34
34
  end).to have_been_made
35
35
 
36
+ expected_disposition = 'attachment'
37
+
36
38
  aggregate_failures('request headers') do
37
39
  {
38
40
  'On-Behalf-Of' => on_behalf_of,
39
41
  'Packaging' => 'http://purl.org/net/sword/package/SimpleZip',
40
42
  'Slug' => doi,
41
- 'Content-Disposition' => 'attachment; filename=example.zip',
43
+ 'Content-Disposition' => "#{expected_disposition}; filename=example.zip",
42
44
  'Content-MD5' => md5,
43
45
  'Content-Length' => /[0-9]+/,
44
46
  'Content-Type' => 'application/zip'
@@ -84,16 +86,19 @@ module Stash
84
86
  end
85
87
  end
86
88
 
89
+ expected_disposition = 'attachment'
90
+
87
91
  mime_headers = {
88
92
  'Packaging' => 'http://purl.org/net/sword/package/SimpleZip',
89
- 'Content-Disposition' => 'attachment; name=payload; filename="example.zip"',
93
+ 'Content-Disposition' => "#{expected_disposition}; name=payload; filename=\"example.zip\"",
90
94
  'Content-Type' => 'application/zip',
91
95
  'Content-MD5' => md5
92
96
  }
93
97
 
94
98
  aggregate_failures('MIME headers') do
95
99
  mime_headers.each do |k, v|
96
- expect(actual_body).to include("#{k}: #{v}"), "expected #{k}: #{v}, closest match was #{actual_body[/#{k}[^\n]+/m]}"
100
+ closest_match = actual_body[/#{k}[^\n]+/m].strip
101
+ expect(actual_body).to include("#{k}: #{v}"), "expected '#{k}: #{v}'; closest match was '#{closest_match}'"
97
102
  end
98
103
  end
99
104
  end
@@ -67,7 +67,7 @@ module Stash
67
67
 
68
68
  after(:each) do
69
69
  sqio.close if sqio
70
- tempfiles.each { |f| File.delete(f) } if tempfiles
70
+ tempfiles.each { |f| File.delete(f) if File.exist?(f) } if tempfiles
71
71
  end
72
72
 
73
73
  def make_alphanumeric_string(chars)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stash-sword
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Moles
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-08 00:00:00.000000000 Z
11
+ date: 2016-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -202,11 +202,13 @@ files:
202
202
  - ".ruby-version"
203
203
  - ".travis.yml"
204
204
  - ".yardopts"
205
+ - CHANGES.md
205
206
  - Gemfile
206
207
  - LICENSE.md
207
208
  - README.md
208
209
  - Rakefile
209
- - examples/example.rb
210
+ - examples/create.rb
211
+ - examples/update.rb
210
212
  - examples/uploads/example.zip
211
213
  - examples/uploads/example/lorem-ipsum.txt
212
214
  - examples/uploads/example/mrt-datacite.xml
@@ -234,7 +236,6 @@ files:
234
236
  - spec/unit/stash/sword2/http_helper_get_spec.rb
235
237
  - spec/unit/stash/sword2/http_helper_post_spec.rb
236
238
  - spec/unit/stash/sword2/http_helper_put_spec.rb
237
- - spec/unit/stash/sword2/log_spec.rb
238
239
  - spec/unit/stash/sword2/namespaces_spec.rb
239
240
  - spec/unit/stash/sword2/sequence_io_spec.rb
240
241
  - stash-sword.gemspec
@@ -273,7 +274,6 @@ test_files:
273
274
  - spec/unit/stash/sword2/http_helper_get_spec.rb
274
275
  - spec/unit/stash/sword2/http_helper_post_spec.rb
275
276
  - spec/unit/stash/sword2/http_helper_put_spec.rb
276
- - spec/unit/stash/sword2/log_spec.rb
277
277
  - spec/unit/stash/sword2/namespaces_spec.rb
278
278
  - spec/unit/stash/sword2/sequence_io_spec.rb
279
279
  has_rdoc:
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Stash
4
- module Sword
5
- describe 'log' do
6
- it 'logs to stdout in a timestamp-first format' do
7
- out = StringIO.new
8
- Sword.log_device = out
9
- begin
10
- msg = 'I am a log message'
11
- Sword.log.warn(msg)
12
- logged = out.string
13
- expect(logged).to include(msg)
14
- timestamp_str = logged.split[0]
15
- timestamp = DateTime.parse(timestamp_str)
16
- expect(timestamp.to_date).to eq(Time.now.utc.to_date)
17
- ensure
18
- Sword.log_device = $stdout
19
- end
20
- end
21
- end
22
- end
23
- end