Class: Proj::NetworkApiImpl

Inherits:
Object
  • Object
show all
Includes:
NetworkApiCallbacks
Defined in:
lib/proj/network_api.rb

Overview

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.

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

Instance Method Summary collapse

Methods included from NetworkApiCallbacks

#close_callback, #header_value_callback, #install_callbacks, #open_callback, #read_range_callback

Constructor Details

#initialize(context) ⇒ NetworkApiImpl

Returns a new instance of NetworkApiImpl.



57
58
59
# File 'lib/proj/network_api.rb', line 57

def initialize(context)
  install_callbacks(context)
end

Instance Method Details

#closeObject



73
74
75
# File 'lib/proj/network_api.rb', line 73

def close
  @http.finish
end

#header_value(name) ⇒ Object



77
78
79
# File 'lib/proj/network_api.rb', line 77

def header_value(name)
  @response[name]
end

#open(uri, offset, size_to_read) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Object



85
86
87
88
89
90
# 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) ⇒ Object



81
82
83
# File 'lib/proj/network_api.rb', line 81

def read_range(offset, size_to_read)
  read_data(offset, size_to_read)
end