Class: Zold::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/http.rb

Overview

Http page

Defined Under Namespace

Classes: Error

Constant Summary collapse

SCORE_HEADER =

HTTP header we add to each HTTP request, in order to inform the other node about the score. If the score is big enough, the remote node will add us to its list of remote nodes.

'X-Zold-Score'.freeze
VERSION_HEADER =

HTTP header we add, in order to inform the node about our version. This is done mostly in order to let the other node reboot itself, if the version is higher.

'X-Zold-Version'.freeze
NETWORK_HEADER =

HTTP header we add, in order to inform the node about our network. This is done in order to isolate test networks from production one.

'X-Zold-Network'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(uri, score = Score::ZERO, network: 'test') ⇒ Http

Returns a new instance of Http.



50
51
52
53
54
55
56
57
# File 'lib/zold/http.rb', line 50

def initialize(uri, score = Score::ZERO, network: 'test')
  raise 'URI can\'t be nil' if uri.nil?
  @uri = uri.is_a?(String) ? URI(uri) : uri
  raise 'Score can\'t be nil' if score.nil?
  @score = score
  raise 'Network can\'t be nil' if network.nil?
  @network = network
end

Instance Method Details

#getObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/zold/http.rb', line 59

def get
  http = Net::HTTP.new(@uri.host, @uri.port)
  http.read_timeout = 8
  http.open_timeout = 4
  path = @uri.path
  path += '?' + @uri.query if @uri.query
  http.request_get(path, headers)
rescue StandardError => e
  Error.new(e)
end

#put(body) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/zold/http.rb', line 70

def put(body)
  http = Net::HTTP.new(@uri.host, @uri.port)
  http.read_timeout = 16
  http.open_timeout = 4
  path = @uri.path
  path += '?' + @uri.query if @uri.query
  http.request_put(
    path, body,
    headers.merge(
      'Content-Type': 'text/plain',
      'Content-Length': body.length.to_s
    )
  )
rescue StandardError => e
  Error.new(e)
end