geoblacklight 0.7.1 → 0.8.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
  SHA1:
3
- metadata.gz: 336d18ec6cafab94ffffefb1bab4ea094d00a236
4
- data.tar.gz: 5c2897ee61afa85bc2cad7cc76c5c44e2fc2b1ee
3
+ metadata.gz: a76208f38c8360b0a7ac9d53f6bea129aa127c7f
4
+ data.tar.gz: cab96dd5acfb95ebb2313061eedb342be66fc086
5
5
  SHA512:
6
- metadata.gz: 2520695818063a834b764469d3874269ab69e27cb640093c233f35af57982b62f99fd08818178ca27ebf6ac8d43d12c42ad6b3ca3e2cde0db155162b74a50ee5
7
- data.tar.gz: 09a53748f413c5f1a77c0852eae201a758ddd67194a0452671b67bd1878468c68cf4e6b85920f9ef251e9feb5b5bb29e929d1d9ad75ec030d61aafc36371e295
6
+ metadata.gz: 52d058b189f45f3009b8071bf2ea5db8811b1d2965d6995432ebe25897fabe86c79c68068245158388fbd4efc9a6b5658ab8d68fcd3bd1e7a72318b1800e5d88
7
+ data.tar.gz: 1deb25f8b97b1954559efa2105d364dd69be7ad8ad69d9c736ddbb0cc9fef2c16c0a882fef063a3b7b8ff507d740f42e75126b0a2ee3c163a10fb72a07ab7dcc
@@ -1,6 +1,22 @@
1
1
  class DownloadController < ApplicationController
2
2
  include Blacklight::SolrHelper
3
3
 
4
+ rescue_from Geoblacklight::Exceptions::ExternalDownloadFailed do |exception|
5
+ Geoblacklight.logger.error exception.message + ' ' + exception.url
6
+ flash[:danger] = view_context
7
+ .content_tag(:span,
8
+ flash_error_message(exception),
9
+ data: {
10
+ download: 'error',
11
+ download_id: params[:id],
12
+ download_type: "generated-#{params[:type]}"
13
+ })
14
+ respond_to do |format|
15
+ format.json { render json: flash, response: response }
16
+ format.html { render json: flash, response: response }
17
+ end
18
+ end
19
+
4
20
  def show
5
21
  @response, @document = get_solr_response_for_doc_id params[:id]
6
22
  restricted_should_authenticate
@@ -37,28 +53,43 @@ class DownloadController < ApplicationController
37
53
  end
38
54
  end
39
55
 
56
+ protected
57
+
58
+ ##
59
+ # Creates an error flash message with failed download url
60
+ # @param [Geoblacklight::Exceptions::ExternalDownloadFailed] Download failed
61
+ # exception
62
+ # @return [String] error message to display in flash
63
+ def flash_error_message(exception)
64
+ if exception.url
65
+ message = t('geoblacklight.download.error_with_url',
66
+ link: view_context
67
+ .link_to(exception.url,
68
+ exception.url,
69
+ target: 'blank'))
70
+ .html_safe
71
+ else
72
+ message = t('geoblacklight.download.error')
73
+ end
74
+ end
75
+
40
76
  private
41
77
 
42
78
  def check_type
43
- case params[:type]
79
+ response = case params[:type]
44
80
  when 'shapefile'
45
- response = Geoblacklight::ShapefileDownload.new(@document).get
81
+ Geoblacklight::ShapefileDownload.new(@document).get
46
82
  when 'kmz'
47
- response = Geoblacklight::KmzDownload.new(@document).get
83
+ Geoblacklight::KmzDownload.new(@document).get
48
84
  when 'geojson'
49
- response = Geoblacklight::GeojsonDownload.new(@document).get
85
+ Geoblacklight::GeojsonDownload.new(@document).get
50
86
  when 'geotiff'
51
- response = Geoblacklight::GeotiffDownload.new(@document).get
87
+ Geoblacklight::GeotiffDownload.new(@document).get
52
88
  end
53
- response
54
89
  end
55
90
 
56
91
  def validate(response)
57
- if response.nil?
58
- flash[:danger] = view_context.content_tag(:span, t('geoblacklight.download.error'), data: { download: 'error', download_id: params[:id], download_type: "generated-#{params[:type]}"})
59
- else
60
- flash[:success] = view_context.link_to(t('geoblacklight.download.success', title: response), download_file_path(response), data: { download: 'trigger', download_id: params[:id], download_type: "generated-#{params[:type]}"})
61
- end
92
+ flash[:success] = view_context.link_to(t('geoblacklight.download.success', title: response), download_file_path(response), data: { download: 'trigger', download_id: params[:id], download_type: "generated-#{params[:type]}"})
62
93
  end
63
94
 
64
95
  # Checks whether a document is public, if not require user to authenticate
@@ -5,6 +5,7 @@ en:
5
5
  success: 'Your file %{title} is ready for download'
6
6
  hgl_success: 'You should receive an email when your download is ready'
7
7
  error: 'Sorry, the requested file could not be downloaded'
8
+ error_with_url: 'Sorry, the requested file could not be downloaded, try downloading it directly from: %{link}'
8
9
  home:
9
10
  headline: 'Explore and discover...'
10
11
  search_heading: 'Find the maps and data you need'
@@ -207,9 +207,9 @@ class CatalogController < ApplicationController
207
207
  config.spell_max = 5
208
208
 
209
209
  # Custom tools for GeoBlacklight
210
- config.add_show_tools_partial :web_services, if: proc { |_context, _config, options| (Settings.WEBSERVICES_SHOWN & options[:document].references.refs.map(&:type).map(&:to_s)).any? }
211
- config.add_show_tools_partial :metadata, if: proc { |_context, _config, options| (Settings.METADATA_SHOWN & options[:document].references.refs.map(&:type).map(&:to_s)).any? }
212
- config.add_show_tools_partial :downloads, partial: 'downloads'
210
+ config.add_show_tools_partial :web_services, if: proc { |_context, _config, options| options[:document] && (Settings.WEBSERVICES_SHOWN & options[:document].references.refs.map(&:type).map(&:to_s)).any? }
211
+ config.add_show_tools_partial :metadata, if: proc { |_context, _config, options| options[:document] && (Settings.METADATA_SHOWN & options[:document].references.refs.map(&:type).map(&:to_s)).any? }
212
+ config.add_show_tools_partial :downloads, partial: 'downloads', if: proc { |_context, _config, options| options[:document] }
213
213
  end
214
214
 
215
215
 
@@ -29,11 +29,12 @@ module Geoblacklight
29
29
  end
30
30
  end
31
31
 
32
+ ##
33
+ # Creates temporary file on file system and renames it if download completes
34
+ # successfully. Will raise and rescue if the wrong file format is downloaded
35
+ # @return [String] filename of the completed download
32
36
  def create_download_file
33
37
  download = initiate_download
34
- unless download.present?
35
- raise Geoblacklight::Exceptions::ExternalDownloadFailed
36
- end
37
38
  File.open("#{file_path}.tmp", 'wb') do |file|
38
39
  if download.headers['content-type'] == @options[:content_type]
39
40
  file.write download.body
@@ -43,18 +44,18 @@ module Geoblacklight
43
44
  end
44
45
  File.rename("#{file_path}.tmp", file_path)
45
46
  file_name
46
- rescue Geoblacklight::Exceptions::ExternalDownloadFailed
47
- Geoblacklight.logger.error 'Download from external server failed'
48
- nil
49
47
  rescue Geoblacklight::Exceptions::WrongDownloadFormat => error
50
48
  Geoblacklight.logger.error "#{error} expected #{@options[:content_type]} received #{download.headers['content-type']}"
51
49
  File.delete("#{file_path}.tmp")
52
- nil
50
+ raise Geoblacklight::Exceptions::ExternalDownloadFailed, message: 'Wrong download type'
53
51
  end
54
52
 
53
+ ##
54
+ # Initiates download from a remote source url using the `request_params`.
55
+ # Will catch Faraday::Error::ConnectionFailed and
56
+ # Faraday::Error::TimeoutError
57
+ # @return [Faraday::Request] returns a Faraday::Request object
55
58
  def initiate_download
56
- url = @document.references.send(@options[:service_type]).endpoint
57
- url += '/reflect' if @options[:reflect]
58
59
  conn = Faraday.new(url: url)
59
60
  conn.get do |request|
60
61
  request.params = @options[:request_params]
@@ -64,11 +65,9 @@ module Geoblacklight
64
65
  }
65
66
  end
66
67
  rescue Faraday::Error::ConnectionFailed => error
67
- Geoblacklight.logger.error error.inspect
68
- nil
68
+ raise Geoblacklight::Exceptions::ExternalDownloadFailed, message: 'Download connection failed', url: conn.url_prefix.to_s
69
69
  rescue Faraday::Error::TimeoutError => error
70
- Geoblacklight.logger.error error.inspect
71
- nil
70
+ raise Geoblacklight::Exceptions::ExternalDownloadFailed, message: 'Download timed out', url: conn.url_prefix.to_s
72
71
  end
73
72
 
74
73
  private
@@ -76,9 +75,18 @@ module Geoblacklight
76
75
  ##
77
76
  # Returns timeout for the download request. `timeout` passed as an option to
78
77
  # the Geoblacklight::Download class
79
- # @returns [Fixnum] download timeout in seconds
78
+ # @return [Fixnum] download timeout in seconds
80
79
  def timeout
81
80
  @options[:timeout] || Settings.TIMEOUT_DOWNLOAD || 20
82
81
  end
82
+
83
+ ##
84
+ # URL for download
85
+ # @return [String] URL that is checked in download
86
+ def url
87
+ url = @document.references.send(@options[:service_type]).endpoint
88
+ url += '/reflect' if @options[:reflect]
89
+ url
90
+ end
83
91
  end
84
92
  end
@@ -1,6 +1,23 @@
1
1
  module Geoblacklight
2
2
  module Exceptions
3
3
  class ExternalDownloadFailed < StandardError
4
+ def initialize(options = {})
5
+ @options = options
6
+ end
7
+
8
+ ##
9
+ # URL tried from failed download
10
+ # @return [String]
11
+ def url
12
+ @options[:url].to_s
13
+ end
14
+
15
+ ##
16
+ # Message passed from a failed download
17
+ # @return [String]
18
+ def message
19
+ @options[:message].to_s
20
+ end
4
21
  end
5
22
  class WrongDownloadFormat < StandardError
6
23
  end
@@ -1,3 +1,3 @@
1
1
  module Geoblacklight
2
- VERSION = '0.7.1'
2
+ VERSION = '0.8.0'
3
3
  end
@@ -26,7 +26,7 @@ describe Geoblacklight::DownloadController, type: :controller do
26
26
  end
27
27
  describe 'public file' do
28
28
  it 'should initiate download creation' do
29
- get 'show', id: 'mit-us-ma-e25zcta5dct-2000'
29
+ get 'show', id: 'mit-us-ma-e25zcta5dct-2000', type: 'shapefile'
30
30
  expect(response.status).to eq 200
31
31
  end
32
32
  end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ feature 'Blacklight Bookmarks' do
4
+ scenario 'index has created bookmarks' do
5
+ visit catalog_path 'columbia-columbia-landinfo-global-aet'
6
+ click_button 'Bookmark'
7
+ visit bookmarks_path
8
+ expect(page).to have_css '.document', count: 1
9
+ end
10
+ end
@@ -7,6 +7,13 @@ feature 'Download layer' do
7
7
  find('a', text: 'Download Shapefile').click
8
8
  expect(page).to have_css('a', text: 'Your file mit-us-ma-e25zcta5dct-2000-shapefile.zip is ready for download')
9
9
  end
10
+ scenario 'failed download should return message with link to layer', js: true do
11
+ expect_any_instance_of(Geoblacklight::ShapefileDownload).to receive(:get).and_raise(Geoblacklight::Exceptions::ExternalDownloadFailed.new(message: 'Failed', url: 'http://www.example.com/failed'))
12
+ visit catalog_path('mit-us-ma-e25zcta5dct-2000')
13
+ find('a', text: 'Download Shapefile', match: :first).click
14
+ expect(page).to have_css 'div.alert.alert-danger', text: 'Sorry, the requested file could not be downloaded, try downloading it directly from:'
15
+ expect(page).to have_css 'a', text: 'http://www.example.com/failed'
16
+ end
10
17
  scenario 'clicking kmz download button should trigger download', js: true do
11
18
  expect_any_instance_of(Geoblacklight::KmzDownload).to receive(:get).and_return('mit-us-ma-e25zcta5dct-2000-kmz.kmz')
12
19
  visit catalog_path('mit-us-ma-e25zcta5dct-2000')
@@ -51,7 +51,7 @@ describe Geoblacklight::Download do
51
51
  bad_file = OpenStruct.new(headers: { 'content-type' => 'bad/file' })
52
52
  expect(download).to receive(:initiate_download).and_return(bad_file)
53
53
  expect(File).to receive(:delete).with("#{download.file_path}.tmp").and_return(nil)
54
- expect(download.create_download_file).to eq nil
54
+ expect { download.create_download_file }.to raise_error(Geoblacklight::Exceptions::ExternalDownloadFailed, 'Wrong download type')
55
55
  end
56
56
  it 'should create the file, write it, and then rename from tmp if everything is ok' do
57
57
  shapefile = OpenStruct.new(headers: {'content-type' => 'application/zip'})
@@ -67,10 +67,11 @@ describe Geoblacklight::Download do
67
67
  expect(Faraday).to receive(:new).with(url: 'http://www.example.com/wms').and_return(response)
68
68
  download.initiate_download
69
69
  end
70
- it 'should return nil with a connection failure' do
71
- expect(response).to receive(:get).and_return(Faraday::Error::ConnectionFailed)
70
+ it 'should raise Geoblacklight::Exceptions::ExternalDownloadFailed with a connection failure' do
71
+ expect(response).to receive(:url_prefix).and_return 'http://www.example.com/wms'
72
+ expect(response).to receive(:get).and_raise(Faraday::Error::ConnectionFailed.new('Failed'))
72
73
  expect(Faraday).to receive(:new).with(url: 'http://www.example.com/wms').and_return(response)
73
- expect(download.initiate_download).to be Faraday::ConnectionFailed
74
+ expect { download.initiate_download }.to raise_error(Geoblacklight::Exceptions::ExternalDownloadFailed)
74
75
  end
75
76
  end
76
77
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geoblacklight
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Graves
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2015-03-03 00:00:00.000000000 Z
14
+ date: 2015-03-04 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: blacklight
@@ -360,6 +360,7 @@ files:
360
360
  - spec/controllers/catalog_controller_spec.rb
361
361
  - spec/controllers/download_controller_spec.rb
362
362
  - spec/factories/user.rb
363
+ - spec/features/bookmarks_spec.rb
363
364
  - spec/features/download_layer_spec.rb
364
365
  - spec/features/home_page_spec.rb
365
366
  - spec/features/iiif_viewer_spec.rb
@@ -433,6 +434,7 @@ test_files:
433
434
  - spec/controllers/catalog_controller_spec.rb
434
435
  - spec/controllers/download_controller_spec.rb
435
436
  - spec/factories/user.rb
437
+ - spec/features/bookmarks_spec.rb
436
438
  - spec/features/download_layer_spec.rb
437
439
  - spec/features/home_page_spec.rb
438
440
  - spec/features/iiif_viewer_spec.rb