Class: Natour::MapGeoAdmin
- Inherits:
-
Object
- Object
- Natour::MapGeoAdmin
- Defined in:
- lib/natour/map_geo_admin.rb
Defined Under Namespace
Classes: MapServlet
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(port: 0) ⇒ MapGeoAdmin
constructor
A new instance of MapGeoAdmin.
- #save_image(filename, overwrite: false, gps_files: [], gps_colors: [], map_layers: [], image_size: [1200, 900]) ⇒ Object
Constructor Details
#initialize(port: 0) ⇒ MapGeoAdmin
Returns a new instance of MapGeoAdmin.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/natour/map_geo_admin.rb', line 11 def initialize(port: 0) @doc_root = Pathname(Dir.mktmpdir) FileUtils.cp_r("#{__dir__}/data/js", @doc_root) event = Concurrent::Event.new @server = StdoutUtils.suppress_output do WEBrick::HTTPServer.new( StartCallback: -> { event.set }, Logger: WEBrick::Log.new(File.open(File::NULL, 'w')), AccessLog: [], DocumentRoot: @doc_root, BindAddress: 'localhost', Port: port, SSLEnable: true, SSLCertName: [%w[CN localhost]] ) end @server.mount('/map', MapServlet) @thread = Thread.new { @server.start } event.wait @browser = Ferrum::Browser.new( slowmo: 2.0, timout: 30, window_size: [5000, 5000], browser_options: { 'no-sandbox': nil, 'ignore-certificate-errors': nil } ) end |
Class Method Details
.open(*args) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/natour/map_geo_admin.rb', line 72 def self.open(*args) map = MapGeoAdmin.new(*args) return map unless block_given? yield(map) ensure map&.close end |
Instance Method Details
#close ⇒ Object
41 42 43 44 45 46 |
# File 'lib/natour/map_geo_admin.rb', line 41 def close @browser.quit @server.shutdown @thread.join FileUtils.remove_entry(@doc_root) end |
#save_image(filename, overwrite: false, gps_files: [], gps_colors: [], map_layers: [], image_size: [1200, 900]) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/natour/map_geo_admin.rb', line 48 def save_image(filename, overwrite: false, gps_files: [], gps_colors: [], map_layers: [], image_size: [1200, 900]) FileUtils.cp(gps_files, @doc_root) uri = URI("https://#{@server[:BindAddress]}:#{@server[:Port]}/map") uri.query = URI.encode_www_form( 'gps-files': gps_files.map { |gps_file| Pathname(gps_file).basename }.join(','), 'gps-colors': gps_colors.join(','), 'map-layers': map_layers.join(','), 'map-size': image_size.map { |dim| dim.is_a?(String) ? dim : "#{dim}px" }.join(',') ) @browser.goto(uri) tmp_filename = @doc_root.join(Pathname(filename).basename) @browser.screenshot( path: tmp_filename, quality: 100, selector: '.map' ) FileUtils.mkdir_p(Pathname(filename).dirname) mode = File::WRONLY | File::CREAT | File::TRUNC | File::BINARY mode |= File::EXCL unless overwrite File.open(filename, mode) do |file| file.write(File.read(tmp_filename, mode: 'rb')) end end |