Class: Range::Client

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

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rangeclient.rb', line 17

def initialize(options = {})
  @host = 'range'
  @host = ENV['RANGE_HOST'] if ENV.has_key?('RANGE_HOST')
  @host = options[:host] if options.member?(:host)

  @port = '80'
  @port = ENV['RANGE_PORT'] if ENV.has_key?('RANGE_PORT')
  @port = options[:port] if options.member?(:port)

  @ssl = ENV['RANGE_SSL'] =~ (/(1|true)/i)
  @ssl = options[:ssl] if options.member?(:ssl)

  @timeout = 60
  @timeout = options[:timeout] if options.member?(:timeout)
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/rangeclient.rb', line 8

def host
  @host
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/rangeclient.rb', line 8

def port
  @port
end

#rangeexceptionObject

Returns the value of attribute rangeexception.



8
9
10
# File 'lib/rangeclient.rb', line 8

def rangeexception
  @rangeexception
end

#timeoutObject

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



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rangeclient.rb', line 89

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



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/rangeclient.rb', line 176

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



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/rangeclient.rb', line 188

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



103
104
105
106
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
# File 'lib/rangeclient.rb', line 103

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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rangeclient.rb', line 157

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



54
55
56
57
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
# File 'lib/rangeclient.rb', line 54

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



33
34
35
36
37
38
39
40
41
42
# File 'lib/rangeclient.rb', line 33

def expand(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)
  @rangeexception = resp['rangeexception']
  return resp.body.split "\n"
end