Class: Range::Client
- Inherits:
-
Object
- Object
- Range::Client
- Defined in:
- lib/rangeclient.rb
Defined Under Namespace
Classes: QueryException
Constant Summary collapse
- @@NodeRegx =
used to split hostnames into component parts for compression
/ ([-\w.]*?) # $1 - prefix (\d+) # $2 - start of range (\.[-A-Za-z\d.]*[-A-Za-z]+[-A-Za-z\d.]*)? # optional domain /x
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#rangeexception ⇒ Object
Returns the value of attribute rangeexception.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #_extra_compress(nodes) ⇒ Object
- #_get_group(prefix, digits, count, suffix) ⇒ Object
- #_ignore_common_prefix(start_pos, end_pos) ⇒ Object
- #_simple_compress(nodes) ⇒ Object
- #_sort_nodes(nodes) ⇒ Object
-
#compress(nodes) ⇒ Object
Take a page from the Perl Seco::Data::Range and perform this locally – more efficient in both speed/size This was ported over from the Perl version, so it’s not quite idiomatic ruby.
- #expand(arg) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rangeclient.rb', line 20 def initialize( = {}) @host = 'range' @host = ENV['RANGE_HOST'] if ENV.has_key?('RANGE_HOST') @host = [:host] if .member?(:host) @port = '80' @port = ENV['RANGE_PORT'] if ENV.has_key?('RANGE_PORT') @port = [:port] if .member?(:port) @ssl = ENV['RANGE_SSL'] =~ (/(1|true)/i) @ssl = [:ssl] if .member?(:ssl) @timeout = 60 @timeout = [:timeout] if .member?(:timeout) end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/rangeclient.rb', line 8 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
8 9 10 |
# File 'lib/rangeclient.rb', line 8 def port @port end |
#rangeexception ⇒ Object
Returns the value of attribute rangeexception.
8 9 10 |
# File 'lib/rangeclient.rb', line 8 def rangeexception @rangeexception end |
#timeout ⇒ Object
Returns the value of attribute timeout.
8 9 10 |
# File 'lib/rangeclient.rb', line 8 def timeout @timeout end |
Instance Method Details
#_extra_compress(nodes) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rangeclient.rb', line 93 def _extra_compress(nodes) domains = {} nodes = nodes.dup nodes.each do |node| node.gsub!(/^([a-z]+)(\d+)([a-z]\w+)\./) { "#{$1}#{$2}.UNDOXXX#{$3}." } end result = _simple_compress(nodes) result.each_char do |r| r.gsub!(/(\d+\.\.\d+)\.UNDOXXX/) {"{#{$1}}"} r.gsub!(/(\d+)\.UNDOXXX/) {"#{$1}"} end return result end |
#_get_group(prefix, digits, count, suffix) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/rangeclient.rb', line 180 def _get_group(prefix, digits, count, suffix) prefix = "" if prefix.nil? group = sprintf("%s%0*d..%s", prefix, digits.to_s.length, digits.to_i, # sometimes has leading zeroes _ignore_common_prefix(digits, (digits.to_i + count).to_s) ) suffix = "" if suffix.nil? return group + suffix end |
#_ignore_common_prefix(start_pos, end_pos) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/rangeclient.rb', line 192 def _ignore_common_prefix(start_pos, end_pos) len_start = start_pos.to_s.length return end_pos if len_start < end_pos.to_s.length pick = 0 len_start.times do |i| pick = i # find the point at which the two strings deviate break if (start_pos[0..i] != end_pos[0..i]) end # and return that substring prior to deviation return end_pos[pick..-1] end |
#_simple_compress(nodes) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/rangeclient.rb', line 107 def _simple_compress(nodes) # dedup nodes set = {} nodes.each do |node| set[node] = true end nodes = set.keys nodes = _sort_nodes(nodes) result = [] prev_prefix, prev_digits, prev_suffix = "", nil, "" prev_n = nil count = 0 nodes.each do |n| if n =~ /\A#{@@NodeRegx}\z/ # foo100abc => foo 100 abc prefix, digits, suffix = $1, $2, $3 prefix = "" if prefix.nil? suffix = "" if suffix.nil? else prefix, digits, suffix = n, nil, nil end if (not digits.to_i.zero?) and (prefix == prev_prefix) and (suffix == prev_suffix) and (not prev_digits.nil?) and (digits.to_i == prev_digits.to_i + count + 1) count += 1 next end if prev_n if count > 0 result << _get_group(prev_prefix, prev_digits, count, prev_suffix) else result << prev_n end end prev_n = n prev_prefix = prefix prev_digits = digits prev_suffix = suffix count = 0 end #nodes.each if count > 0 result << _get_group(prev_prefix, prev_digits, count, prev_suffix) else result << prev_n end return result.join "," end |
#_sort_nodes(nodes) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/rangeclient.rb', line 161 def _sort_nodes(nodes) sorted = nodes.map { |n| # decorate-sort-undecorate # FIXME can this all be pushed into sort_by? n =~ /\A#{@@NodeRegx}\z/ [ n, $1.nil? ? "" : $1, $2.nil? ? 0 : $2.to_i, $3.nil? ? "" : $3, ] }.sort_by { |e| [ e[1], e[3], e[2], e[0] ] }.map { |n| n[0] } return sorted end |
#compress(nodes) ⇒ Object
Take a page from the Perl Seco::Data::Range and perform this locally – more efficient in both speed/size This was ported over from the Perl version, so it’s not quite idiomatic ruby
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rangeclient.rb', line 58 def compress(nodes) domain_tbl = {} no_domain_list = [] nodes.each do |n| # If this is a quoted range, just compress it without collapsing return _simple_compress(nodes) if n =~ /^(?:"|q\()/ # Break out host and key by domain, to enable {foo1,foo3}.bar.com grouping host, domain = n.split('.', 2) if domain domain_tbl[domain] ||= [] domain_tbl[domain] << host else no_domain_list << host end end result = [] # Range elements with no domain component do not group # just return if not no_domain_list.empty? result << _simple_compress(no_domain_list) end domain_tbl.keys.sort.each do |domain| r = _extra_compress(domain_tbl[domain]) r.gsub!(/\.#{domain},/) {","} r.gsub!(/\.#{domain}$/) {""} if r=~ /,/ r = "{#{r}}" end result << "#{r}.#{domain}" end return result.join "," end |
#expand(arg) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rangeclient.rb', line 36 def (arg) escaped_arg = URI.escape arg http = Net::HTTP.new(@host, @port) http.read_timeout = @timeout http.use_ssl = @ssl req = Net::HTTP::Get.new('/range/list?' + escaped_arg) resp = http.request(req) raise QueryException.new(resp.body) unless resp.is_a?(Net::HTTPSuccess) @rangeexception = resp['rangeexception'] return resp.body.split "\n" end |