Class: RGeo::CoordSys::SRSDatabase::UrlReader

Inherits:
Object
  • Object
show all
Defined in:
lib/rgeo/coord_sys/srs_database/url_reader.rb

Overview

A spatial reference database implementation that fetches data from internet URLs.

Instance Method Summary collapse

Constructor Details

#initialize(opts_ = {}) ⇒ UrlReader

Create a URL-based spatial reference database.

Options:

:cache

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.



25
26
27
# File 'lib/rgeo/coord_sys/srs_database/url_reader.rb', line 25

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

Instance Method Details

#clear_cacheObject

Clear the cache if one is present.



57
58
59
# File 'lib/rgeo/coord_sys/srs_database/url_reader.rb', line 57

def clear_cache
  @cache.clear if @cache
end

#get(ident_) ⇒ Object

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rgeo/coord_sys/srs_database/url_reader.rb', line 33

def get(ident_)
  ident_ = ident_.to_s
  return @cache[ident_] if @cache && @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_.request_get(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