Skip to content

Commit

Permalink
Merge branch 'release-1.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
dchandekstark committed Feb 28, 2017
2 parents ab7cf04 + 154a53d commit 519762b
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 27 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
sudo: false
language: ruby
rvm:
- 2.3.1
- 2.2
- 2.1
- 2.0.0
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.0
1.6.0
52 changes: 31 additions & 21 deletions lib/ezid/batch_download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class BatchDownload < Hashie::Dash

# Parameters
property :format, required: true # {anvl|csv|xml}
property :compression # {gzip|zip}
property :column # repeatable
property :notify # repeatable
property :convertTimestamps # {yes|no}
Expand Down Expand Up @@ -87,36 +88,45 @@ def download_file(path: nil)
path ||= Dir.getwd
fullpath = File.directory?(path) ? File.join(path, download_filename) : path
tries = 0
begin
tries += 1
download = Net::HTTP.get_response(download_uri)
download.value
rescue Net::HTTPServerException => e
if download.is_a?(Net::HTTPNotFound)
if tries < MAX_DOWNLOAD_TRIES
print "Download file not yet available (attempt #{tries} of #{MAX_DOWNLOAD_TRIES})."
puts " Trying again in #{DOWNLOAD_RETRY_INTERVAL} second(s) ..."
sleep DOWNLOAD_RETRY_INTERVAL
retry
else
raise BatchDownloadError,
"Maximum download attempts (#{MAX_DOWNLOAD_TRIES}) reached unsuccessfully."
ready = false

print "Checking for download "
Net::HTTP.start(download_uri.host, download_uri.port) do |http|
while tries < MAX_DOWNLOAD_TRIES
tries += 1
sleep DOWNLOAD_RETRY_INTERVAL
print "."
response = http.head(download_uri.path)
if response.code == '200'
ready = true
break
end
else
raise
end
else
File.open(fullpath, "wb") do |f|
f.write(download.body)
end
puts

unless ready
raise BatchDownloadError,
"Download not ready after checking #{MAX_DOWNLOAD_TRIES} times."
end

File.open(fullpath, "wb") do |f|
Net::HTTP.start(download_uri.host, download_uri.port) do |http|
http.request_get(download_uri.path) do |response|
response.read_body do |chunk|
f.write(chunk)
end
end
end
puts "File successfully download to #{fullpath}."
end

fullpath
end

private

def download_uri
URI(download_url)
@download_uri ||= URI(download_url)
end

def download_filename
Expand Down
42 changes: 42 additions & 0 deletions lib/ezid/batch_enumerator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Ezid
class BatchEnumerator
include Enumerable

attr_reader :format, :batch_file

def initialize(format, batch_file)
@format = format
@batch_file = batch_file
end

def each(&block)
case format
when :anvl
each_anvl(&block)
when :xml
each_xml(&block)
when :csv
each_csv(&block)
end
end

def each_anvl(&block)
File.open(batch_file, "rb") do |f|
while record = f.gets("")
head, metadata = record.split(/\n/, 2)
id = head.sub(/\A::/, "").strip
yield Ezid::Identifier.new(id, metadata: metadata)
end
end
end

def each_xml
raise NotImplementedError
end

def each_csv
raise NotImplementedError
end

end
end
39 changes: 39 additions & 0 deletions spec/fixtures/anvl_batch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
:: ark:/99999/fk4086hs23
_updated: 1488227717
_target: http://example.com
_profile: erc
_ownergroup: apitest
_owner: apitest
_export: yes
_created: 1488227717
_status: public

:: ark:/99999/fk4086hs23/123
_updated: 1488227718
_target: http://ezid.cdlib.org/id/ark:/99999/fk4086hs23/123
_profile: erc
_ownergroup: apitest
_owner: apitest
_export: yes
_created: 1488227718
_status: public

:: ark:/99999/fk40p1bb85
_updated: 1488303565
_target: http://ezid.cdlib.org/id/ark:/99999/fk40p1bb85
_profile: erc
_ownergroup: apitest
_owner: apitest
_export: yes
_created: 1488303565
_status: public

:: ark:/99999/fk40z7fh7x
_updated: 1488232856
_target: http://ezid.cdlib.org/id/ark:/99999/fk40z7fh7x
_profile: erc
_ownergroup: apitest
_owner: apitest
_export: yes
_created: 1488232856
_status: public
21 changes: 21 additions & 0 deletions spec/integration/batch_download_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'tempfile'

module Ezid
RSpec.describe BatchDownload do

subject do
a_week_ago = (Time.now - (7*24*60*60)).to_i
described_class.new(:anvl, compression: "zip", permanence: "test", status: "public", createdAfter: a_week_ago)
end

its(:download_url) { is_expected.to match(/\Ahttp:\/\/ezid\.cdlib\.org\/download\/\w+\.zip\z/) }

specify {
Dir.mktmpdir do |tmpdir|
expect(subject.download_file(path: tmpdir))
.to match(/\A#{tmpdir}\/\w+\.zip\z/)
end
}

end
end
5 changes: 0 additions & 5 deletions spec/unit/batch_download_spec.rb

This file was deleted.

34 changes: 34 additions & 0 deletions spec/unit/batch_enumerator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'ezid/batch_enumerator'

module Ezid
RSpec.describe BatchEnumerator do

let(:batch_file) { File.expand_path("../../fixtures/anvl_batch.txt", __FILE__) }

subject { described_class.new(:anvl, batch_file) }

its(:count) { is_expected.to eq 4 }

specify {
subject.each do |id|
expect(id).to be_a(Identifier)
end
}

specify {
batch_array = subject.to_a
expect(batch_array.length).to eq 4
}

specify {
ids = subject.map(&:id)
expect(ids).to eq ["ark:/99999/fk4086hs23", "ark:/99999/fk4086hs23/123", "ark:/99999/fk40p1bb85", "ark:/99999/fk40z7fh7x"]
}

specify {
id = subject.first
expect(id.target).to eq "http://example.com"
}

end
end

0 comments on commit 519762b

Please sign in to comment.