Class: Proxy

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/proxy_rotater/proxy.rb

Constant Summary collapse

ROUND_NUM =
2
TEST_REQUEST_URL =
"http://www.google.co.jp"
BENCHMARK_TIMES =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Proxy

Returns a new instance of Proxy.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/proxy_rotater/proxy.rb', line 11

def initialize(options)
  options.merge!(
    requested_at:       [],
    response_time:      nil,
    last_response_time: nil,
    checked_at:         nil,
    timeout:            false,
    return_at:          nil
  )
  options.keys.each do |key|
    instance_variable_set("@#{key}", options[key])
  end

  self.class.http_proxy(@ip_address, @port)
  benchmark
end

Instance Attribute Details

#last_response_timeObject (readonly)

Returns the value of attribute last_response_time.



9
10
11
# File 'lib/proxy_rotater/proxy.rb', line 9

def last_response_time
  @last_response_time
end

#requested_atObject (readonly)

Returns the value of attribute requested_at.



9
10
11
# File 'lib/proxy_rotater/proxy.rb', line 9

def requested_at
  @requested_at
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



9
10
11
# File 'lib/proxy_rotater/proxy.rb', line 9

def timeout
  @timeout
end

Instance Method Details

#benchmarkObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/proxy_rotater/proxy.rb', line 46

def benchmark
  urls = BENCHMARK_TIMES.times.map{|i| TEST_REQUEST_URL}
  response_times = Parallel.map(urls, in_threads: BENCHMARK_TIMES) do |url|
    start = Time.now.to_f
    res = get_url(TEST_REQUEST_URL)
    finish = Time.now.to_f
    res.nil? ? nil : (finish - start).round(ROUND_NUM)
  end

  # TODO: 微妙なのでリファクタリングする
  # sliceとかまとめられそう
  # ifの中はresponse_timeどうにかしたほうがいいのでは?
  if response_times.include?(nil)
    reset_size = @requested_at.size
    @requested_at.slice!(-1 * reset_size, reset_size)
    return
  end

  @response_time = (response_times.inject(0){|v,s|v+s}/response_times.size).round(ROUND_NUM)
  @checked_at = Time.now

  # ベンチマークの結果はused_rateとrequested_atに影響しないようにする
  @requested_at.slice!(-1 * BENCHMARK_TIMES, BENCHMARK_TIMES)
end

#get_request_intervalsObject



71
72
73
74
75
76
77
78
79
# File 'lib/proxy_rotater/proxy.rb', line 71

def get_request_intervals
  diff = []
  @requested_at.each_with_index do |num, i|
    next if i == 0
    diff << @requested_at[i] - @requested_at[i-1]
  end

  return diff
end

#get_url(url) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/proxy_rotater/proxy.rb', line 28

def get_url(url)
  start = Time.now
  # TODO: redirect too deepの時にどうにかする
  begin
    res = self.class.get(url)
  rescue Errno::ETIMEDOUT, Errno::ECONNRESET, EOFError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e
    @timeout = true
    return nil
  rescue Net::ReadTimeout, Net::OpenTimeout => e
    @timeout = true
    return nil
  end
  finish = Time.now
  @requested_at << finish.to_f
  @last_response_time = (finish.to_f - start.to_f).round(ROUND_NUM)
  return res
end