Class: Zold::Remotes::Remote

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

Overview

One remote.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, score, idx, log: Log::Quiet.new, network: 'test') ⇒ Remote

Returns a new instance of Remote.


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

def initialize(host, port, score, idx, log: Log::Quiet.new, network: 'test')
  @host = host
  raise 'Post must be Integer' unless port.is_a?(Integer)
  @port = port
  raise 'Score must be of type Score' unless score.is_a?(Score)
  @score = score
  raise 'Idx must be of type Integer' unless idx.is_a?(Integer)
  @idx = idx
  raise 'Network can\'t be nil' if network.nil?
  @network = network
  @log = log
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host


59
60
61
# File 'lib/zold/remotes.rb', line 59

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port


59
60
61
# File 'lib/zold/remotes.rb', line 59

def port
  @port
end

Instance Method Details

#assert_code(code, response) ⇒ Object


81
82
83
84
85
86
87
# File 'lib/zold/remotes.rb', line 81

def assert_code(code, response)
  msg = response.message.strip
  return if response.code.to_i == code
  @log.debug("#{response.code} \"#{response.message}\" at \"#{response.body}\"")
  raise "Unexpected HTTP code #{response.code}, instead of #{code}" if msg.empty?
  raise "#{msg} (HTTP code #{response.code}, instead of #{code})"
end

#assert_score_ownership(score) ⇒ Object


94
95
96
97
# File 'lib/zold/remotes.rb', line 94

def assert_score_ownership(score)
  raise "Masqueraded host #{@host} as #{score.host}: #{score}" if @host != score.host
  raise "Masqueraded port #{@port} as #{score.port}: #{score}" if @port != score.port
end

#assert_score_strength(score) ⇒ Object


99
100
101
# File 'lib/zold/remotes.rb', line 99

def assert_score_strength(score)
  raise "Score #{score.strength} is too weak (<#{Score::STRENGTH}): #{score}" if score.strength < Score::STRENGTH
end

#assert_score_value(score, min) ⇒ Object


103
104
105
# File 'lib/zold/remotes.rb', line 103

def assert_score_value(score, min)
  raise "Score is too small (<#{min}): #{score}" if score.value < min
end

#assert_valid_score(score) ⇒ Object


89
90
91
92
# File 'lib/zold/remotes.rb', line 89

def assert_valid_score(score)
  raise "Invalid score #{score}" unless score.valid?
  raise "Expired score #{score}" if score.expired?
end

#http(path = '/') ⇒ Object


73
74
75
# File 'lib/zold/remotes.rb', line 73

def http(path = '/')
  Http.new("http://#{@host}:#{@port}#{path}", @score, network: @network)
end

#to_sObject


77
78
79
# File 'lib/zold/remotes.rb', line 77

def to_s
  "#{@host}:#{@port}/#{@idx}"
end