class Proj::NetworkApiImpl

Proj allows its network api to be replaced by a custom implementation. This can be done by calling Context#set_network_api with a user defined Class that includes the NetworkApiCallbacks module and implements its required methods.

@see proj.org/usage/network.html

The NetworkApiImpl class is a simple example of a network api implementation.

Public Class Methods

new(context) click to toggle source
# File lib/proj/network_api.rb, line 57
def initialize(context)
  install_callbacks(context)
end

Public Instance Methods

close() click to toggle source
# File lib/proj/network_api.rb, line 73
def close
  @http.finish
end
header_value(name) click to toggle source
# File lib/proj/network_api.rb, line 77
def header_value(name)
  @response[name]
end
open(uri, offset, size_to_read) click to toggle source
# File lib/proj/network_api.rb, line 61
def open(uri, offset, size_to_read)
  @uri = uri
  @http = Net::HTTP.new(@uri.host, @uri.port)
  if uri.scheme == "https"
    @http.use_ssl = true
    @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end
  @http.start

  read_data(offset, size_to_read)
end
read_data(offset, size_to_read) click to toggle source
# File lib/proj/network_api.rb, line 85
def read_data(offset, size_to_read)
  headers = {"Range": "bytes=#{offset}-#{offset + size_to_read - 1}"}
  request = Net::HTTP::Get.new(@uri.request_uri, headers)
  @response = @http.request(request)
  @response.body.force_encoding("ASCII-8BIT")
end
read_range(offset, size_to_read) click to toggle source
# File lib/proj/network_api.rb, line 81
def read_range(offset, size_to_read)
  read_data(offset, size_to_read)
end