Class: IPGeolocationAPI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apiKey) ⇒ IPGeolocationAPI

Returns a new instance of IPGeolocationAPI.



8
9
10
11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#apiKeyObject (readonly)

Returns the value of attribute apiKey.



7
8
9
# File 'lib/ipgeolocation_io/IPGeolocationAPI.rb', line 7

def apiKey
  @apiKey
end

Instance Method Details

#buildGeolocationUrlParams(geolocationParams) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# 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) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# 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) ⇒ Object



20
21
22
23
24
25
26
27
28
# 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) ⇒ Object



30
31
32
33
# File 'lib/ipgeolocation_io/IPGeolocationAPI.rb', line 30

def getGeolocationResponse(geolocationParams)
  urlParams = buildGeolocationUrlParams(geolocationParams);
  return callAPIEndpoint("ipgeo", urlParams);
end

#getTimezone(timezoneParams = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
# 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) ⇒ Object



64
65
66
67
# File 'lib/ipgeolocation_io/IPGeolocationAPI.rb', line 64

def getTimezoneResponse(timezoneParams)
  urlParams = buildTimezoneUrlParams(timezoneParams);
  return callAPIEndpoint("timezone", urlParams);
end