Class: NSWTopo::ArcGISServer::Connection
- Inherits:
-
Object
- Object
- NSWTopo::ArcGISServer::Connection
- Defined in:
- lib/nswtopo/gis/arcgis_server/connection.rb
Instance Method Summary collapse
- #get(relative_path, **query, &block) ⇒ Object
- #get_json(relative_path = "", **query) ⇒ Object
-
#initialize(http, service_path) ⇒ Connection
constructor
A new instance of Connection.
- #post(relative_path, **query, &block) ⇒ Object
- #post_json(relative_path = "", **query) ⇒ Object
- #process_json(response) ⇒ Object
- #repeatedly_request(request) ⇒ Object
Constructor Details
#initialize(http, service_path) ⇒ Connection
Returns a new instance of Connection.
4 5 6 7 |
# File 'lib/nswtopo/gis/arcgis_server/connection.rb', line 4 def initialize(http, service_path) @http, @service_path, @headers = http, service_path, { "User-Agent" => "Ruby/#{RUBY_VERSION}", "Referer" => "%s://%s" % [http.use_ssl? ? "https" : "http", http.address] } http.max_retries = 0 end |
Instance Method Details
#get(relative_path, **query, &block) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/nswtopo/gis/arcgis_server/connection.rb', line 20 def get(relative_path, **query, &block) path = Pathname(@service_path).join(relative_path).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
43 44 45 |
# File 'lib/nswtopo/gis/arcgis_server/connection.rb', line 43 def get_json(relative_path = "", **query) get relative_path, query.merge(f: "json"), &method(:process_json) end |
#post(relative_path, **query, &block) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/nswtopo/gis/arcgis_server/connection.rb', line 27 def post(relative_path, **query, &block) path = Pathname(@service_path).join(relative_path).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
47 48 49 |
# File 'lib/nswtopo/gis/arcgis_server/connection.rb', line 47 def post_json(relative_path = "", **query) post relative_path, query.merge(f: "json"), &method(:process_json) end |
#process_json(response) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/nswtopo/gis/arcgis_server/connection.rb', line 34 def process_json(response) JSON.parse(response.body).tap do |result| # raise Error, result["error"].values_at("message", "details").compact.join(?\n) if result["error"] raise Error, result["error"]["message"] if result["error"] end rescue JSON::ParserError raise Error, "unexpected ArcGIS response format" end |
#repeatedly_request(request) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/nswtopo/gis/arcgis_server/connection.rb', line 9 def repeatedly_request(request) intervals ||= 5.times.map(&1.4142.method(:**)) response = @http.request(request) response.error! unless Net::HTTPSuccess === response yield response rescue *ERRORS, Error => error interval = intervals.shift interval ? sleep(interval) : raise(error) retry end |