Class: Zold::Remotes
- Inherits:
-
Object
- Object
- Zold::Remotes
- Defined in:
- lib/zold/remotes.rb
Overview
All remotes
Defined Under Namespace
Classes: Empty
Constant Summary collapse
- PORT =
4096
Instance Method Summary collapse
- #add(host, port = Remotes::PORT) ⇒ Object
- #all ⇒ Object
- #clean ⇒ Object
- #error(host, port = Remotes::PORT) ⇒ Object
- #errors(host, port = Remotes::PORT) ⇒ Object
- #exists?(host, port = Remotes::PORT) ⇒ Boolean
-
#initialize(file) ⇒ Remotes
constructor
A new instance of Remotes.
- #remove(host, port = Remotes::PORT) ⇒ Object
- #rescore(host, port, score) ⇒ Object
- #reset ⇒ Object
- #score(host, port = Remotes::PORT) ⇒ Object
Constructor Details
#initialize(file) ⇒ Remotes
Returns a new instance of Remotes.
41 42 43 |
# File 'lib/zold/remotes.rb', line 41 def initialize(file) @file = file end |
Instance Method Details
#add(host, port = Remotes::PORT) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/zold/remotes.rb', line 73 def add(host, port = Remotes::PORT) raise 'Port has to be of type Integer' unless port.is_a?(Integer) raise 'Port can\'t be negative' if port < 0 raise 'Port can\'t be over 65536' if port > 0xffff raise "#{host}:#{port} alread exists" if exists?(host, port) list = load list << { host: host.downcase, port: port, score: 0 } list.uniq! { |r| "#{r[:host]}:#{r[:port]}" } save(list) end |
#all ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/zold/remotes.rb', line 45 def all list = load max_score = list.map { |r| r[:score] }.max || 0 max_score = 1 if max_score.zero? max_errors = list.map { |r| r[:errors] }.max || 0 max_errors = 1 if max_errors.zero? list.sort_by do |r| (1 - r[:errors] / max_errors) * 5 + (r[:score] / max_score) end.reverse end |
#clean ⇒ Object
56 57 58 |
# File 'lib/zold/remotes.rb', line 56 def clean save([]) end |
#error(host, port = Remotes::PORT) ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/zold/remotes.rb', line 104 def error(host, port = Remotes::PORT) raise 'Port has to be of type Integer' unless port.is_a?(Integer) raise "#{host}:#{port} is absent" unless exists?(host, port) list = load list.find do |r| r[:host] == host.downcase && r[:port] == port end[:errors] += 1 save(list) end |
#errors(host, port = Remotes::PORT) ⇒ Object
98 99 100 101 102 |
# File 'lib/zold/remotes.rb', line 98 def errors(host, port = Remotes::PORT) raise 'Port has to be of type Integer' unless port.is_a?(Integer) raise "#{host}:#{port} is absent" unless exists?(host, port) load.find { |r| r[:host] == host.downcase && r[:port] == port }[:errors] end |
#exists?(host, port = Remotes::PORT) ⇒ Boolean
68 69 70 71 |
# File 'lib/zold/remotes.rb', line 68 def exists?(host, port = Remotes::PORT) raise 'Port has to be of type Integer' unless port.is_a?(Integer) !load.find { |r| r[:host] == host.downcase && r[:port] == port }.nil? end |
#remove(host, port = Remotes::PORT) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/zold/remotes.rb', line 84 def remove(host, port = Remotes::PORT) raise 'Port has to be of type Integer' unless port.is_a?(Integer) raise "#{host}:#{port} is absent" unless exists?(host, port) list = load list.reject! { |r| r[:host] == host.downcase && r[:port] == port } save(list) end |
#rescore(host, port, score) ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/zold/remotes.rb', line 114 def rescore(host, port, score) raise 'Port has to be of type Integer' unless port.is_a?(Integer) raise "#{host}:#{port} is absent" unless exists?(host, port) list = load list.find do |r| r[:host] == host.downcase && r[:port] == port end[:score] = score save(list) end |
#reset ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/zold/remotes.rb', line 60 def reset FileUtils.mkdir_p(File.dirname(@file)) FileUtils.copy( File.join(File.dirname(__FILE__), '../../resources/remotes'), @file ) end |
#score(host, port = Remotes::PORT) ⇒ Object
92 93 94 95 96 |
# File 'lib/zold/remotes.rb', line 92 def score(host, port = Remotes::PORT) raise 'Port has to be of type Integer' unless port.is_a?(Integer) raise "#{host}:#{port} is absent" unless exists?(host, port) load.find { |r| r[:host] == host.downcase && r[:port] == port }[:score] end |