Class: Fiveruns::Dash::Pinger

Inherits:
Object
  • Object
show all
Defined in:
lib/fiveruns/dash/update.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ Pinger

Returns a new instance of Pinger.



11
12
13
# File 'lib/fiveruns/dash/update.rb', line 11

def initialize(payload)
  @payload = payload
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



10
11
12
# File 'lib/fiveruns/dash/update.rb', line 10

def payload
  @payload
end

Instance Method Details

#ping(*urls) ⇒ Object



15
16
17
18
19
# File 'lib/fiveruns/dash/update.rb', line 15

def ping(*urls)
  try_urls(urls) do |url|
    send_ping(url, payload)
  end
end

#send_ping(url, payload) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fiveruns/dash/update.rb', line 21

def send_ping(url, payload)
  begin
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true if url.scheme == 'https'
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    multipart = Fiveruns::Dash::Store::HTTP::Multipart.new(payload.io, payload.params)
    response = http.post("/apps/#{token}/ping", multipart.to_s, "Content-Type" => multipart.content_type)
    case response.code.to_i
    when 201
      data = ::Fiveruns::JSON.load(response.body)
      [:success, "Found application '#{data['name']}'"]
    else
      # Error message
      [:failed, response.body.to_s]
    end
  rescue => e
    [:error, e.message]
  end
end

#tokenObject



41
42
43
# File 'lib/fiveruns/dash/update.rb', line 41

def token
  ::Fiveruns::Dash.configuration.options[:app]
end

#try_urls(urls) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fiveruns/dash/update.rb', line 45

def try_urls(urls)
  results = urls.map do |u|
    result = yield(URI.parse(u))
    case result[0]
    when :success
      puts "OK: #{result[1]}"
      true
    when :failed
      puts "Failed talking to #{u}: #{result[1]}"
      false
    when :error
      puts "Error contacting #{u}: #{result[1]}"
      false
    end
  end
  results.all?
end