Sha256: 2ac66714512ef61b5d0df3f2b992ddba596a1c8660bdcf37054d36ba5df83b6f

Contents?: true

Size: 1.84 KB

Versions: 9

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

# -----------------------------------------------------------------------------
#
# SRS database interface
#
# -----------------------------------------------------------------------------

require "net/http"

module RGeo
  module CoordSys
    module SRSDatabase
      # A spatial reference database implementation that fetches data from
      # internet URLs.

      class UrlReader
        # Create a URL-based spatial reference database.
        #
        # Options:
        #
        # [<tt>:cache</tt>]
        #   If set to true, lookup results are cached so if the same URL
        #   is requested again, the result is served from cache rather
        #   than issuing another HTTP request. Default is false.

        def initialize(opts = {})
          @cache = opts[:cache] ? {} : nil
        end

        # Retrieve the given URL and return an Entry.
        # Returns nil if the URL cannot be read as an OGC WKT or Proj4
        # coordinate system

        def get(ident)
          ident = ident.to_s
          return @cache[ident] if @cache&.include?(ident)
          uri = URI.parse(ident)
          result = nil
          Net::HTTP.start(uri.host, uri.port) do |http|
            request = uri.path
            request = "#{request}?#{uri.query}" if uri.query
            response = http.requestget(request)
            if response.is_a?(Net::HTTPSuccess)
              response = response.body.strip
              if response[0, 1] == "+"
                result = Entry.new(ident, proj4: response)
              else
                result = Entry.new(ident, coord_sys: response)
              end
            end
          end
          @cache[ident] = result if @cache
          result
        end

        # Clear the cache if one is present.

        def clear_cache
          @cache&.clear
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rgeo-3.0.0.pre.rc.1 lib/rgeo/coord_sys/srs_database/url_reader.rb
rgeo-2.4.0 lib/rgeo/coord_sys/srs_database/url_reader.rb
rgeo-2.3.1 lib/rgeo/coord_sys/srs_database/url_reader.rb
rgeo-2.3.0 lib/rgeo/coord_sys/srs_database/url_reader.rb
rgeo-2.2.0 lib/rgeo/coord_sys/srs_database/url_reader.rb
rgeo-2.1.1 lib/rgeo/coord_sys/srs_database/url_reader.rb
rgeo-2.1.0 lib/rgeo/coord_sys/srs_database/url_reader.rb
rgeo-2.0.1 lib/rgeo/coord_sys/srs_database/url_reader.rb
rgeo-2.0.0 lib/rgeo/coord_sys/srs_database/url_reader.rb