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.



45
46
47
48
49
50
# File 'lib/zold/http.rb', line 45

def initialize(uri, score = Score::ZERO)
  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
end

Instance Method Details

#getObject



52
53
54
55
56
57
58
59
60
# File 'lib/zold/http.rb', line 52

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/zold/http.rb', line 62

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