Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 0e1eb1a

Browse files
committed
Merge pull request #20 from MITLibraries/5_sword_submission
5 sword submission
2 parents 2765171 + da267de commit 0e1eb1a

25 files changed

+4440
-36
lines changed

Gemfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ gem 'less-rails'
1212
gem 'nokogiri'
1313
gem 'omniauth-mit-oauth2'
1414
gem 'omniauth-oauth2', '~> 1.3.1'
15-
gem 'passenger'
15+
gem 'rest-client'
1616
gem 'rubyzip', require: 'zip'
1717
gem 'skylight'
1818
gem 'therubyracer', platforms: :ruby
@@ -24,6 +24,10 @@ group :production do
2424
gem 'rails_12factor'
2525
end
2626

27+
group :production, :development do
28+
gem 'passenger'
29+
end
30+
2731
group :development, :test do
2832
gem 'byebug'
2933
gem 'sqlite3'
@@ -40,4 +44,6 @@ group :test do
4044
gem 'minitest-reporters'
4145
gem 'minitest-rails'
4246
gem 'minitest-rails-capybara'
47+
gem 'vcr'
48+
gem 'webmock'
4349
end

Gemfile.lock

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ GEM
4646
minitest (~> 5.1)
4747
thread_safe (~> 0.3, >= 0.3.4)
4848
tzinfo (~> 1.1)
49+
addressable (2.4.0)
4950
annotate (2.6.10)
5051
activerecord (>= 3.2, <= 4.3)
5152
rake (~> 10.4)
@@ -76,8 +77,10 @@ GEM
7677
term-ansicolor (~> 1.3)
7778
thor (~> 0.19.1)
7879
tins (~> 1.6.0)
80+
crack (0.4.3)
81+
safe_yaml (~> 1.0.0)
7982
debug_inspector (0.0.2)
80-
devise (3.5.2)
83+
devise (3.5.3)
8184
bcrypt (~> 3.0)
8285
orm_adapter (~> 0.1)
8386
railties (>= 3.2.6, < 5)
@@ -93,6 +96,7 @@ GEM
9396
multipart-post (>= 1.2, < 3)
9497
globalid (0.3.6)
9598
activesupport (>= 4.1.0)
99+
hashdiff (0.2.3)
96100
hashie (3.4.3)
97101
http-cookie (1.0.2)
98102
domain_name (~> 0.5)
@@ -161,7 +165,7 @@ GEM
161165
orm_adapter (0.5.0)
162166
parser (2.2.3.0)
163167
ast (>= 1.1, < 3.0)
164-
passenger (5.0.21)
168+
passenger (5.0.22)
165169
rack
166170
rake (>= 0.8.1)
167171
pg (0.18.4)
@@ -216,14 +220,15 @@ GEM
216220
tins (<= 1.6.0)
217221
ruby-progressbar (1.7.5)
218222
rubyzip (1.1.7)
223+
safe_yaml (1.0.4)
219224
simplecov (0.11.1)
220225
docile (~> 1.1.0)
221226
json (~> 1.8)
222227
simplecov-html (~> 0.10.0)
223228
simplecov-html (0.10.0)
224229
skylight (0.10.0)
225230
activesupport (>= 3.0.0)
226-
sprockets (3.5.1)
231+
sprockets (3.5.2)
227232
concurrent-ruby (~> 1.0)
228233
rack (> 1, < 3)
229234
sprockets-rails (2.3.3)
@@ -253,13 +258,18 @@ GEM
253258
unf (0.1.4)
254259
unf_ext
255260
unf_ext (0.0.7.1)
256-
warden (1.2.3)
261+
vcr (3.0.0)
262+
warden (1.2.4)
257263
rack (>= 1.0)
258264
web-console (2.2.1)
259265
activemodel (>= 4.0)
260266
binding_of_caller (>= 0.7.2)
261267
railties (>= 4.0)
262268
sprockets-rails (>= 2.0, < 4.0)
269+
webmock (1.22.3)
270+
addressable (>= 2.3.6)
271+
crack (>= 0.3.2)
272+
hashdiff
263273
xpath (2.0.0)
264274
nokogiri (~> 1.3)
265275

@@ -287,14 +297,17 @@ DEPENDENCIES
287297
pg
288298
rails (= 4.2.5)
289299
rails_12factor
300+
rest-client
290301
rubocop
291302
rubyzip
292303
skylight
293304
sqlite3
294305
therubyracer
295306
twitter-bootstrap-rails
296307
uglifier (>= 1.3.0)
308+
vcr
297309
web-console (~> 2.0)
310+
webmock
298311

299312
BUNDLED WITH
300313
1.10.6

app/controllers/submissions_controller.rb

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ def create
1919
@submission = Submission.new(submission_params)
2020
@submission.user = current_user
2121
if @submission.save
22-
process_submission(@submission)
23-
flash.notice = 'Your Submission is now in progress.'
22+
process_submission
2423
redirect_to submissions_path
2524
else
2625
render 'new'
@@ -33,10 +32,34 @@ def package
3332

3433
private
3534

36-
def process_submission(submission)
37-
# this will likely want to be an asynch job queue
38-
# to create the package and handle the sword submission
39-
submission.to_sword_package
35+
def process_submission
36+
@submission.to_sword_package
37+
sword = Sword.new(@submission)
38+
begin
39+
sword.deposit
40+
read_sword_response(sword)
41+
rescue RestClient::Unauthorized
42+
@submission.status = 'failed'
43+
flash.notice = 'There was a problem with your submission.'
44+
end
45+
@submission.save
46+
end
47+
48+
def read_sword_response(sword)
49+
deposited(sword) if sword.response.code == 201
50+
queued if sword.response.code == 202
51+
end
52+
53+
def deposited(sword)
54+
@submission.status = 'deposited'
55+
@submission.handle = sword.handle
56+
flash.notice =
57+
"Your Submission succeeded. Permanent URL: #{@submission.handle}"
58+
end
59+
60+
def queued
61+
@submission.status = 'in review queue'
62+
flash.notice = 'Your Submission is now in progress.'
4063
end
4164

4265
def submission_params

app/models/mets.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ def initialize(submission)
55
xml['mets'].mets(
66
'xmlns:mets' => 'http://www.loc.gov/METS/',
77
'xmlns:xlink' => 'http://www.w3.org/1999/xlink',
8-
'xmlns:epdcx' => 'http://purl.org/eprint/epdcx/2006-11-16/') do
8+
'xmlns:epdcx' => 'http://purl.org/eprint/epdcx/2006-11-16/',
9+
'PROFILE' => 'DSpace METS SIP Profile 1.0') do
910
overall_structure(xml)
1011
end
1112
end

app/models/submission.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# created_at :datetime not null
1515
# updated_at :datetime not null
1616
# documents :string
17+
# status :string
18+
# handle :string
1719
#
1820

1921
class Submission < ActiveRecord::Base

app/models/sword.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Sword
2+
attr_reader :response
3+
4+
def initialize(submission)
5+
@submission = submission
6+
@sword_server = RestClient::Resource.new(
7+
Rails.application.secrets.sword_endpoint,
8+
user: Rails.application.secrets.sword_username,
9+
password: Rails.application.secrets.sword_password)
10+
end
11+
12+
def deposit
13+
@response = @sword_server.post(
14+
File.read(@submission.sword_path),
15+
content_type: 'application/zip',
16+
x_packaging: 'http://purl.org/net/sword-types/METSDSpaceSIP')
17+
end
18+
19+
def handle
20+
return unless @response.code == 201
21+
xml = Nokogiri::XML(@response)
22+
xml.xpath('//atom:id').text
23+
end
24+
end

app/models/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# uid :string not null
88
# created_at :datetime not null
99
# updated_at :datetime not null
10-
# admin :boolean
10+
# admin :boolean default(FALSE)
1111
#
1212

1313
class User < ActiveRecord::Base
Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
<h1>MIT DSpace Quick Submit Submissions</h1>
22

33
<% if @submissions %>
4-
<ul>
4+
55
<% @submissions.each do |sub| %>
6-
<li>
7-
<strong><%= sub.title %></strong>
8-
submitted: <%= time_ago_in_words(sub.created_at) %> ago
9-
<br />
6+
<% if sub.status == 'deposited' %>
7+
<div class="panel panel-success">
8+
<% elsif sub.status == 'in review queue' %>
9+
<div class="panel panel-info">
10+
<% elsif sub.status == 'failed' %>
11+
<div class="panel panel-danger">
12+
<% else %>
13+
<div class="panel panel-warning">
14+
<% end %>
15+
<div class="panel-heading">
16+
<%= sub.title %>
17+
</div>
18+
<div class="panel-body">
19+
Submitted: <%= time_ago_in_words(sub.created_at) %> ago<br />
20+
Status: <%= sub.status || 'unknown' %><br />
21+
Permanent URL: <%= sub.handle || 'pending submission processing' %><br />
22+
</div>
1023
<% if current_user.admin? %>
11-
<%= link_to('Sword Package', submission_package_path(sub)) %>
24+
<div class="panel-footer">
25+
<%= link_to('Sword Package', submission_package_path(sub)) %>
26+
</div>
1227
<% end %>
13-
</li>
28+
</div>
1429
<% end %>
15-
</ul>
30+
1631
<% else %>
1732
<p>You have no submissions in progress.</p>
1833
<% end %>

config/environments/production.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
4343

4444
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
45-
# config.force_ssl = true
45+
config.force_ssl = true
4646

4747
# Use the lowest log level to ensure availability of diagnostic information
4848
# when problems arise.

config/secrets.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,22 @@ common: &common
1818
development:
1919
<<: *common
2020
secret_key_base: 85c11866fb99f4aa04e3b6a44ea14ab2e8594395c16b90797ea3b57402737be909f7d3a198486ad3e0deb448dbc022c5b4d5b2fd60bcfda7739d36d4b64d42d7
21+
sword_endpoint: <%= ENV['SWORD_ENDPOINT'] %>
22+
sword_username: <%= ENV['SWORD_USERNAME'] %>
23+
sword_password: <%= ENV['SWORD_PASSWORD'] %>
2124

2225
test:
2326
<<: *common
2427
secret_key_base: a42ad0e17b7f5a1e4aca95c930ee3d497f6ffe9a96a85818fe8544028b8f96a2641141da61bcdb39f67af684ffd096836f7c43b09417fd017c7f6126051b90bb
28+
sword_endpoint: http://example.com
29+
sword_username: username
30+
sword_password: password
2531

2632
# Do not keep production secrets in the repository,
2733
# instead read values from the environment.
2834
production:
2935
<<: *common
3036
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
37+
sword_endpoint: <%= ENV['SWORD_ENDPOINT'] %>
38+
sword_username: <%= ENV['SWORD_USERNAME'] %>
39+
sword_password: <%= ENV['SWORD_PASSWORD'] %>

0 commit comments

Comments
 (0)