class IPGeolocationAPI

Attributes

apiKey[R]

Public Class Methods

new(apiKey) click to toggle source
# File lib/ipgeolocation_io/IPGeolocationAPI.rb, line 8
def initialize(apiKey)
  begin
    if Strings.isNullOrEmpty(apiKey)
      raise ArgumentError, 'API key must not be null or empty'
    else
      @apiKey = apiKey;
    end
  rescue ArgumentError => e
    puts e.message
  end
end

Public Instance Methods

buildGeolocationUrlParams(geolocationParams) click to toggle source
# File lib/ipgeolocation_io/IPGeolocationAPI.rb, line 35
def buildGeolocationUrlParams(geolocationParams)
  urlParams = "apiKey="+@apiKey;
  if geolocationParams != nil
    if !Strings.isNullOrEmpty(geolocationParams.ip)
      urlParams = urlParams + "&ip=";
      urlParams = urlParams + geolocationParams.ip;
    end
    if !Strings.isNullOrEmpty(geolocationParams.fields)
      urlParams = urlParams + "&fields=";
      urlParams = urlParams + geolocationParams.fields;
    end
    if !Strings.isNullOrEmpty(geolocationParams.lang)
      urlParams = urlParams + "&lang=";
      urlParams = urlParams + geolocationParams.lang;
    end
  end
  return urlParams;
end
buildTimezoneUrlParams(timezoneParams) click to toggle source
# File lib/ipgeolocation_io/IPGeolocationAPI.rb, line 69
def buildTimezoneUrlParams(timezoneParams)
  urlParams = "apiKey="+@apiKey
  if timezoneParams != nil
    if !Strings.isNullOrEmpty(timezoneParams.ip)
      urlParams = urlParams+"&ip="
      urlParams = urlParams+timezoneParams.ip
    end

    if !Strings.isNullOrEmpty(timezoneParams.timezone)
      urlParams = urlParams+"&tz="
      urlParams = urlParams+timezoneParams.timezone
    end

    latitude = timezoneParams.latitude;
    longitude = timezoneParams.longitude;

    if latitude != 1000.0 && longitude != 1000.0
      urlParams = urlParams + "&lat="
      urlParams = urlParams + latitude.to_s
      urlParams = urlParams + "&long="
      urlParams = urlParams + longitude.to_s
    end
  end
  return urlParams
end
callAPIEndpoint(endpoint, urlParams) click to toggle source
# File lib/ipgeolocation_io/IPGeolocationAPI.rb, line 95
def callAPIEndpoint(endpoint, urlParams)
  url = "https://api.ipgeolocation.io/" + endpoint + "?" + urlParams;
  responseMap = Hash.new
  begin
    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    resp = http.get("/" + endpoint + "?" + urlParams)
    responseCode = resp.code
    jsonString = resp.body
    if Strings.isNullOrEmpty(responseCode) || Strings.isNullOrEmpty(jsonString)
      responseCode = "422";
      jsonString = "{\"message\":\"Something went wrong while parsing IP Geolocation API response\"}";
    end
  rescue StandardError => e
    responseCode = "422";
    jsonString = "{\"message\":\"Something went wrong while connecting to IP Geolocation API\"}";
  rescue ArgumentError => e
    responseCode = "422";
    jsonString = "{\"message\":\"Something went wrong while parsing IP Geolocation API response\"}";
  end
  responseMap = JSON.parse(jsonString)
  responseMap['status'] = responseCode
  return responseMap
end
callBulkGeolocationAPIEndpoint(ipAddresses, urlParams) click to toggle source
# File lib/ipgeolocation_io/IPGeolocationAPI.rb, line 133
def callBulkGeolocationAPIEndpoint(ipAddresses, urlParams)
  uri = URI.parse("https://api.ipgeolocation.io/ipgeo-bulk" + "?" + urlParams);
  begin
    header = {'Content-Type': 'text/json'}
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    request = Net::HTTP::Post.new(uri.request_uri, header)
    request.body = ipAddresses.to_json
    response = http.request(request)
    responseCode = response.code
    jsonString = response.body
    if Strings.isNullOrEmpty(responseCode) || Strings.isNullOrEmpty(jsonString)
      responseCode = "422";
      jsonString = "{\"message\":\"Something went wrong while parsing IP Geolocation API response\"}";
    end
  rescue StandardError => e
    responseCode = "422";
    jsonString = "{\"message\":\"Something went wrong while connecting to IP Geolocation API\"}";
  rescue ArgumentError => e
    responseCode = "422";
    jsonString = "{\"message\":\"Something went wrong while parsing IP Geolocation API response\"}";
  end
  mapList = Array.new
  list = JSON.parse(jsonString)
  list.each do |item|
    if !item['status']
      item['status'] = '200'
    end
    mapList.push(item)
  end
  return mapList
end
getBulkGeolocation(geolocationParams) click to toggle source
# File lib/ipgeolocation_io/IPGeolocationAPI.rb, line 121
def getBulkGeolocation(geolocationParams)
  data = Hash.new
  data["ips"] =  geolocationParams.ips;
  urlParams = buildGeolocationUrlParams(geolocationParams);
  apiResponse = callBulkGeolocationAPIEndpoint(data, urlParams);
  geolocations = Array.new
  for response in apiResponse
    geolocations.push(Geolocation.new(response));
  end
  return geolocations;
end
getGeolocation(geolocationParams = nil) click to toggle source
# File lib/ipgeolocation_io/IPGeolocationAPI.rb, line 20
def getGeolocation(geolocationParams = nil)
  if geolocationParams==nil
    apiResponse = getGeolocationResponse(nil);
    return Geolocation.new(apiResponse);
  else
    apiResponse = getGeolocationResponse(geolocationParams);
    return Geolocation.new(apiResponse);
  end
end
getGeolocationResponse(geolocationParams) click to toggle source
# File lib/ipgeolocation_io/IPGeolocationAPI.rb, line 30
def getGeolocationResponse(geolocationParams)
  urlParams = buildGeolocationUrlParams(geolocationParams);
  return callAPIEndpoint("ipgeo", urlParams);
end
getTimezone(timezoneParams = nil) click to toggle source
# File lib/ipgeolocation_io/IPGeolocationAPI.rb, line 54
def getTimezone(timezoneParams = nil)
  if timezoneParams==nil
    apiResponse = getTimezoneResponse(nil);
    return Timezone.new(apiResponse);
  else
    apiResponse = getTimezoneResponse(timezoneParams)
    return Timezone.new(apiResponse);
  end
end
getTimezoneResponse(timezoneParams) click to toggle source
# File lib/ipgeolocation_io/IPGeolocationAPI.rb, line 64
def getTimezoneResponse(timezoneParams)
  urlParams = buildTimezoneUrlParams(timezoneParams);
  return callAPIEndpoint("timezone", urlParams);
end