Class: Ip2locationWebService

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, package, use_ssl) ⇒ Ip2locationWebService

Returns a new instance of Ip2locationWebService.



764
765
766
767
768
769
770
771
772
773
774
775
776
777
# File 'lib/ip2location_ruby.rb', line 764

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 IP2Location web service API key."
  end
  if !package.match(/^WS[0-9]+$/)
    package = 'WS1'
  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.



762
763
764
# File 'lib/ip2location_ruby.rb', line 762

def ws_api_key
  @ws_api_key
end

#ws_packageObject

Returns the value of attribute ws_package.



762
763
764
# File 'lib/ip2location_ruby.rb', line 762

def ws_package
  @ws_package
end

#ws_use_sslObject

Returns the value of attribute ws_use_ssl.



762
763
764
# File 'lib/ip2location_ruby.rb', line 762

def ws_use_ssl
  @ws_use_ssl
end

Instance Method Details

#get_creditObject



795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
# File 'lib/ip2location_ruby.rb', line 795

def get_credit()
  if self.ws_use_ssl
    response =  Net::HTTP.get(URI("https://api.ip2location.com/v2/?key=" + self.ws_api_key + "&check=true"))
  else
    response =  Net::HTTP.get(URI("http://api.ip2location.com/v2/?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, add_ons, language) ⇒ Object



779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
# File 'lib/ip2location_ruby.rb', line 779

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