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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Http.



43
44
45
46
# File 'lib/zold/http.rb', line 43

def initialize(uri, score = Score::ZERO)
  @uri = uri
  @score = score
end

Instance Method Details

#getObject



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

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

#put(body) ⇒ Object



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

def put(body)
  http = Net::HTTP.new(@uri.host, @uri.port)
  http.read_timeout = 60
  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