Class: Zold::Http

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

Overview

Http page

Defined Under Namespace

Classes: Error, Response

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'
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'
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'
PROTOCOL_HEADER =

HTTP header we add, in order to inform the node about our protocol.

'X-Zold-Protocol'

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Http.



64
65
66
67
68
# File 'lib/zold/http.rb', line 64

def initialize(uri:, score: Score::ZERO, network: 'test')
  @uri = uri.is_a?(URI) ? uri : URI(uri)
  @score = score
  @network = network
end

Instance Method Details

#get(timeout: READ_TIMEOUT) ⇒ Object



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

def get(timeout: READ_TIMEOUT)
  Response.new(
    Typhoeus::Request.get(
      @uri,
      accept_encoding: 'gzip',
      headers: headers,
      connecttimeout: CONNECT_TIMEOUT,
      timeout: timeout
    )
  )
rescue StandardError => e
  Error.new(e)
end

#put(body, timeout: READ_TIMEOUT) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/zold/http.rb', line 84

def put(body, timeout: READ_TIMEOUT)
  Response.new(
    Typhoeus::Request.put(
      @uri,
      accept_encoding: 'gzip',
      body: body,
      headers: headers.merge('Content-Type': 'text/plain'),
      connecttimeout: CONNECT_TIMEOUT,
      timeout: timeout
    )
  )
rescue StandardError => e
  Error.new(e)
end