Class: Ip2proxyWebService

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, package, use_ssl) ⇒ Ip2proxyWebService

Returns a new instance of Ip2proxyWebService.



481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/ip2proxy_ruby.rb', line 481

def initialize(api_key, package, use_ssl)
  if !api_key.match(/^[0-9A-Z]{10}$/) && api_key != 'demo'
    raise Exception.new "Please provide a valid IP2Proxy web service API key."
  end
  if !package.match(/^PX[0-9]+$/)
    package = 'PX1'
  end
  if use_ssl == ''
    use_ssl = true
  end
  self.ws_api_key = api_key
  self.ws_package = package
  self.ws_use_ssl = use_ssl
end

Instance Attribute Details

#ws_api_keyObject

Returns the value of attribute ws_api_key.



479
480
481
# File 'lib/ip2proxy_ruby.rb', line 479

def ws_api_key
  @ws_api_key
end

#ws_packageObject

Returns the value of attribute ws_package.



479
480
481
# File 'lib/ip2proxy_ruby.rb', line 479

def ws_package
  @ws_package
end

#ws_use_sslObject

Returns the value of attribute ws_use_ssl.



479
480
481
# File 'lib/ip2proxy_ruby.rb', line 479

def ws_use_ssl
  @ws_use_ssl
end

Instance Method Details

#get_creditObject



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/ip2proxy_ruby.rb', line 512

def get_credit()
  if self.ws_use_ssl
    response =  Net::HTTP.get(URI("https://api.ip2proxy.com/?key=" + self.ws_api_key + "&check=true"))
  else
    response =  Net::HTTP.get(URI("http://api.ip2proxy.com/?key=" + self.ws_api_key + "&check=true"))
  end
  parsed_response = JSON.parse(response)
  if parsed_response.nil?
    return 0
  end
  if parsed_response["response"].nil?
    return 0
  end
  return parsed_response["response"]
end

#lookup(ip) ⇒ Object



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/ip2proxy_ruby.rb', line 496

def lookup(ip)
  if self.ws_use_ssl
    response =  Net::HTTP.get(URI("https://api.ip2proxy.com/?key=" + self.ws_api_key + "&ip=" + ip + "&package=" + self.ws_package + "&format=json"))
  else
    response =  Net::HTTP.get(URI("http://api.ip2proxy.com/?key=" + self.ws_api_key + "&ip=" + ip + "&package=" + self.ws_package + "&format=json"))
  end
  parsed_response = JSON.parse(response)
  if parsed_response.nil?
    return false
  end
  if parsed_response["response"] != "OK"
    raise Exception.new "Error: " + parsed_response["response"]
  end
  return parsed_response
end