Sha256: 5825444e8fa8e9d1e74e414c8710a1dd0739cdeee36beefd10b5ffaf6019ca0e

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

require 'melonsmasher/pokemon-go-status/version'
require 'net/http'

module PokemonGoStatus
  class Status

    def interpret_response_average avg_ms
      if avg_ms === -1
        {
            :exit_code => 3,
            :description => 'Error! Probably not a good sign, but try again.',
            :avg_ms => avg_ms,
            :available => false
        }
      elsif avg_ms < 800
        {
            :exit_code => 0,
            :description => 'All good! Go catch\'em all!',
            :avg_ms => avg_ms,
            :available => true
        }
      elsif avg_ms >= 800 && avg_ms < 3000
        {
            :exit_code => 1,
            :description => 'Servers are slow. Your mileage may vary.',
            :avg_ms => avg_ms,
            :available => true
        }
      elsif avg_ms > 3000
        {
            :exit_code => 2,
            :description => 'Servers don\'t seem to be up.',
            :avg_ms => avg_ms,
            :available => false
        }
      else
        {
            :exit_code => 3,
            :description => 'Error! Probably not a good sign, but try again.',
            :avg_ms => avg_ms,
            :available => false
        }
      end
    end

    def get_server_response_average url
      times = []
      total_time = 0

      (0..9).each do |i|
        start_time = Time.now
        begin
          Net::HTTP.get_response(URI(url))
        rescue
          next
        end
        response_time = (Time.now - start_time) * 1000
        times[i] = response_time.round
        total_time = total_time + response_time.round
      end

      begin
        return total_time / times.count
      rescue
        return -1
      end

    end

    def get_server_status url = 'https://pgorelease.nianticlabs.com/plfe'
      self.interpret_response_average self.get_server_response_average url
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pokemon-go-status-0.0.8 lib/melonsmasher/pokemon-go-status.rb