Class: NSWTopo::ArcGIS::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/nswtopo/gis/arcgis/connection.rb

Constant Summary collapse

ERRORS =
[Timeout::Error, Errno::ENETUNREACH, Errno::ETIMEDOUT, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, SocketError]
Error =
Class.new RuntimeError

Instance Method Summary collapse

Constructor Details

#initialize(uri, prefix_path = nil) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
11
12
13
# File 'lib/nswtopo/gis/arcgis/connection.rb', line 7

def initialize(uri, prefix_path = nil)
  @http = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", read_timeout: 600)
  @prefix, @headers = Pathname(prefix_path.to_s), { "User-Agent" => "Ruby/#{RUBY_VERSION}", "Referer" => "%s://%s" % [@http.use_ssl? ? "https" : "http", @http.address] }
  @http.max_retries = 0
rescue *ERRORS => error
  raise Error, error.message
end

Instance Method Details

#get(relative_path, **query, &block) ⇒ Object



23
24
25
26
27
28
# File 'lib/nswtopo/gis/arcgis/connection.rb', line 23

def get(relative_path, **query, &block)
  path = @prefix.join(relative_path.to_s).to_s
  path << ?? << URI.encode_www_form(query) unless query.empty?
  request = Net::HTTP::Get.new(path, @headers)
  repeatedly_request(request, &block)
end

#get_json(relative_path = "", **query) ⇒ Object



47
48
49
# File 'lib/nswtopo/gis/arcgis/connection.rb', line 47

def get_json(relative_path = "", **query)
  get relative_path, **query, f: "json", &method(:process_json)
end

#post(relative_path, **query, &block) ⇒ Object



30
31
32
33
34
35
# File 'lib/nswtopo/gis/arcgis/connection.rb', line 30

def post(relative_path, **query, &block)
  path = @prefix.join(relative_path.to_s).to_s
  request = Net::HTTP::Post.new(path, @headers)
  request.body = URI.encode_www_form(query)
  repeatedly_request(request, &block)
end

#post_json(relative_path = "", **query) ⇒ Object



51
52
53
# File 'lib/nswtopo/gis/arcgis/connection.rb', line 51

def post_json(relative_path = "", **query)
  post relative_path, **query, f: "json", &method(:process_json)
end

#process_json(response) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/nswtopo/gis/arcgis/connection.rb', line 37

def process_json(response)
  JSON.parse(response.body).tap do |result|
    next unless error = result["error"]
    # raise Error, error.values_at("message", "details").compact.join(?\n)
    raise Error, error.values_at("message", "code").map(&:to_s).reject(&:empty?).first
  end
rescue JSON::ParserError
  raise Error, "unexpected ArcGIS response format"
end

#repeatedly_request(request, &block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/nswtopo/gis/arcgis/connection.rb', line 15

def repeatedly_request(request, &block)
  intervals ||= 4.times.map(&1.4142.method(:**))
  @http.request(request).tap(&:value).then(&block)
rescue *ERRORS, Error => error
  intervals.any? ? sleep(intervals.shift) : raise(Error, error.message)
  retry
end