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: [], map_layers: [], image_size: [1200, 900]) ⇒ Object
Constructor Details
#initialize(port: 0) ⇒ MapGeoAdmin
Returns a new instance of MapGeoAdmin.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/natour/map_geo_admin.rb', line 10 def initialize(port: 0) @doc_root = Pathname(Dir.mktmpdir) FileUtils.cp_r("#{__dir__}/data/js", @doc_root) event = Concurrent::Event.new @server = WEBrick::HTTPServer.new( StartCallback: -> { event.set }, Logger: WEBrick::Log.new(File.open(File::NULL, 'w')), AccessLog: [], DocumentRoot: @doc_root, BindAddress: 'localhost', Port: port ) @server.mount('/map', MapServlet) @thread = Thread.new { @server.start } event.wait @browser = Ferrum::Browser.new( slowmo: 2.0, window_size: [5000, 5000], browser_options: { 'no-sandbox': nil } ) end |
Class Method Details
.open(*args) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/natour/map_geo_admin.rb', line 62 def self.open(*args) map = MapGeoAdmin.new(*args) return map unless block_given? yield(map) ensure map&.close end |
Instance Method Details
#close ⇒ Object
32 33 34 35 36 37 |
# File 'lib/natour/map_geo_admin.rb', line 32 def close @browser.quit @server.shutdown @thread.join FileUtils.remove_entry(@doc_root) end |
#save_image(filename, overwrite: false, gps_files: [], map_layers: [], image_size: [1200, 900]) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/natour/map_geo_admin.rb', line 39 def save_image(filename, overwrite: false, gps_files: [], map_layers: [], image_size: [1200, 900]) FileUtils.cp(gps_files, @doc_root) uri = URI("http://#{@server[:BindAddress]}:#{@server[:Port]}/map") uri.query = URI.encode_www_form( 'gps-files': gps_files.map { |gps_file| Pathname(gps_file).basename }.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 |