ipgeolocation_io 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f940be534e7ca3bdcdfc7185b1d14f5ecab038b2
4
- data.tar.gz: d458bc31a51f613cdb44a9a3fe5182d14c658c00
3
+ metadata.gz: 9996be4bdc452ebe11bc89a2fbcc5467d146fb25
4
+ data.tar.gz: 20473f2d14eeccd8ad21860a1ce64b0e85359eee
5
5
  SHA512:
6
- metadata.gz: ce97cbeefc633ae4246b3c1b19a4229f5270fb0a413a0d58e1c098eee7e24024fe11a72915985b9c516a24a84c2a6cdf9963619b78046bf421c9a5ec49b56cec
7
- data.tar.gz: ecc0ea4ab8f3f139d8ca90c8ac806bea86004620bd7cddac8985533dc0c4fb6a1f8f03eabbf8768a0922bc0de15d639bbde0da58788a738e8188ae33daf58046
6
+ metadata.gz: c24508aa89db806387788de7eea878046bc34c321f0b0a7f2b97f4269c46417a34d358dfdbd4d1a09bb10e8c284efeebfa2703fab4ea00e64f2614521f77e0fd
7
+ data.tar.gz: bf6f9724dca89499f28441aeab22fb2f7c8551da6edc07bc83f9a597279ac356fc177a3cd73d4af949c3216a2e6d902ae77f40b87f17f21221fbce52c3f5b129
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 ahsan nawaz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # IpgeolocationIo
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ipgeolocation_io`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ipgeolocation_io'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ipgeolocation_io
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ipgeolocation_io. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the IpgeolocationIo project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ipgeolocation_io/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "ipgeolocation_io/version"
2
+
3
+ module IpgeolocationIo
4
+ class Error < StandardError; end
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,67 @@
1
+ require_relative "GeolocationCurrency"
2
+ require_relative "GeolocationTimezone"
3
+ require 'json'
4
+ class Geolocation
5
+ attr_reader :status;
6
+ attr_reader :message;
7
+ attr_reader :ip;
8
+ attr_reader :continentCode;
9
+ attr_reader :continentName;
10
+ attr_reader :countryCode2;
11
+ attr_reader :countryCode3;
12
+ attr_reader :countryName;
13
+ attr_reader :countryCapital;
14
+ attr_reader :stateProvince;
15
+ attr_reader :district;
16
+ attr_reader :city;
17
+ attr_reader :zipCode;
18
+ attr_reader :latitude;
19
+ attr_reader :longitude;
20
+ attr_reader :isEU;
21
+ attr_reader :callingCode;
22
+ attr_reader :countryTLD;
23
+ attr_reader :languages;
24
+ attr_reader :countryFlag;
25
+ attr_reader :isp;
26
+ attr_reader :connectionType;
27
+ attr_reader :organization;
28
+ attr_reader :geonameID;
29
+ attr_reader :currency;
30
+ attr_reader :timezone;
31
+
32
+ def initialize(json)
33
+ @status = json["status"].to_i;
34
+ if @status != 200 || json["message"] != nil
35
+ @message = json["message"].to_s;
36
+ else
37
+ @ip = json["ip"];
38
+ @continentCode = json["continent_code"];
39
+ @continentName = json["continent_name"];
40
+ @countryCode2 = json["country_code2"];
41
+ @countryCode3 = json["country_code3"];
42
+ @countryName = json["country_name"];
43
+ @countryCapital = json["country_capital"];
44
+ @stateProvince = json["state_prov"];
45
+ @district = json["district"];
46
+ @city = json["city"];
47
+ @zipcode = json["zipcode"];
48
+ @latitude = json["latitude"];
49
+ @longitude = json["longitude"];
50
+ @isEU = json["is_eu"];
51
+ @callingCode = json["calling_code"];
52
+ @countryTLD = json["country_tld"];
53
+ @languages = json["languages"];
54
+ @countryFlag = json["country_flag"];
55
+ @isp = json["isp"];
56
+ @connectionType = json["connection_type"];
57
+ @organization = json["organization"];
58
+ @geonameID = json["geoname_id"];
59
+ if(json["currency"] != nil)
60
+ @currency = GeolocationCurrency.new(json["currency"]);
61
+ end
62
+ if(json["time_zone"] != nil)
63
+ @timezone = GeolocationTimezone.new(json["time_zone"]);
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,10 @@
1
+ class GeolocationCurrency
2
+
3
+ attr_reader :name
4
+ attr_reader :code
5
+
6
+ def initialize(json)
7
+ @name = json["name"];
8
+ @code = json["code"];
9
+ end
10
+ end
@@ -0,0 +1,27 @@
1
+
2
+ class GeolocationParams
3
+ attr_accessor :ip
4
+ attr_accessor :fields
5
+ attr_accessor :lang
6
+ attr_accessor :ips;
7
+
8
+ def initialize
9
+ @ip = "";
10
+ @fields = "";
11
+ @lang = "en";
12
+ @ips = Array.new(0);
13
+ end
14
+
15
+ def ips=(ips)
16
+ if @ips.length > 50
17
+ begin
18
+ raise ArgumentError, 'Max. number of IP addresses cannot be more than 50.'
19
+ rescue ArgumentError => e
20
+ puts e.message
21
+ end
22
+ else
23
+ @ips = ips;
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,16 @@
1
+ class GeolocationTimezone
2
+
3
+ attr_reader :name
4
+ attr_reader :offset
5
+ attr_reader :currentTime
6
+ attr_reader :isDST
7
+ attr_reader :dstSavings
8
+
9
+ def initialize(json)
10
+ @name = json["name"];
11
+ @offset = json["offset"];
12
+ @currentTime = json["current_time"];
13
+ @isDST = json["is_dst"];
14
+ @dstSavings = json["dst_savings"];
15
+ end
16
+ end
@@ -0,0 +1,165 @@
1
+ require_relative "Strings"
2
+ require_relative "Geolocation"
3
+ require_relative "Timezone"
4
+ require 'json'
5
+ require 'net/https'
6
+ class IPGeolocationAPI
7
+ attr_reader :apiKey;
8
+ def initialize(apiKey)
9
+ begin
10
+ if Strings.isNullOrEmpty(apiKey)
11
+ raise ArgumentError, 'API key must not be null or empty'
12
+ else
13
+ @apiKey = apiKey;
14
+ end
15
+ rescue ArgumentError => e
16
+ puts e.message
17
+ end
18
+ end
19
+
20
+ def getGeolocation(geolocationParams = nil)
21
+ if geolocationParams==nil
22
+ apiResponse = getGeolocationResponse(nil);
23
+ return Geolocation.new(apiResponse);
24
+ else
25
+ apiResponse = getGeolocationResponse(geolocationParams);
26
+ return Geolocation.new(apiResponse);
27
+ end
28
+ end
29
+
30
+ def getGeolocationResponse(geolocationParams)
31
+ urlParams = buildGeolocationUrlParams(geolocationParams);
32
+ return callAPIEndpoint("ipgeo", urlParams);
33
+ end
34
+
35
+ def buildGeolocationUrlParams(geolocationParams)
36
+ urlParams = "apiKey="+@apiKey;
37
+ if geolocationParams != nil
38
+ if !Strings.isNullOrEmpty(geolocationParams.ip)
39
+ urlParams = urlParams + "&ip=";
40
+ urlParams = urlParams + geolocationParams.ip;
41
+ end
42
+ if !Strings.isNullOrEmpty(geolocationParams.fields)
43
+ urlParams = urlParams + "&fields=";
44
+ urlParams = urlParams + geolocationParams.fields;
45
+ end
46
+ if !Strings.isNullOrEmpty(geolocationParams.lang)
47
+ urlParams = urlParams + "&lang=";
48
+ urlParams = urlParams + geolocationParams.lang;
49
+ end
50
+ end
51
+ return urlParams;
52
+ end
53
+
54
+ def getTimezone(timezoneParams = nil)
55
+ if timezoneParams==nil
56
+ apiResponse = getTimezoneResponse(nil);
57
+ return Timezone.new(apiResponse);
58
+ else
59
+ apiResponse = getTimezoneResponse(timezoneParams)
60
+ return Timezone.new(apiResponse);
61
+ end
62
+ end
63
+
64
+ def getTimezoneResponse(timezoneParams)
65
+ urlParams = buildTimezoneUrlParams(timezoneParams);
66
+ return callAPIEndpoint("timezone", urlParams);
67
+ end
68
+
69
+ def buildTimezoneUrlParams(timezoneParams)
70
+ urlParams = "apiKey="+@apiKey
71
+ if timezoneParams != nil
72
+ if !Strings.isNullOrEmpty(timezoneParams.ip)
73
+ urlParams = urlParams+"&ip="
74
+ urlParams = urlParams+timezoneParams.ip
75
+ end
76
+
77
+ if !Strings.isNullOrEmpty(timezoneParams.timezone)
78
+ urlParams = urlParams+"&tz="
79
+ urlParams = urlParams+timezoneParams.timezone
80
+ end
81
+
82
+ latitude = timezoneParams.latitude;
83
+ longitude = timezoneParams.longitude;
84
+
85
+ if latitude != 1000.0 && longitude != 1000.0
86
+ urlParams = urlParams + "&lat="
87
+ urlParams = urlParams + latitude.to_s
88
+ urlParams = urlParams + "&long="
89
+ urlParams = urlParams + longitude.to_s
90
+ end
91
+ end
92
+ return urlParams
93
+ end
94
+
95
+ def callAPIEndpoint(endpoint, urlParams)
96
+ url = "https://api.ipgeolocation.io/" + endpoint + "?" + urlParams;
97
+ responseMap = Hash.new
98
+ begin
99
+ uri = URI.parse(url)
100
+ http = Net::HTTP.new(uri.host, uri.port)
101
+ http.use_ssl = true
102
+ resp = http.get("/" + endpoint + "?" + urlParams)
103
+ responseCode = resp.code
104
+ jsonString = resp.body
105
+ if Strings.isNullOrEmpty(responseCode) || Strings.isNullOrEmpty(jsonString)
106
+ responseCode = "422";
107
+ jsonString = "{\"message\":\"Something went wrong while parsing IP Geolocation API response\"}";
108
+ end
109
+ rescue StandardError => e
110
+ responseCode = "422";
111
+ jsonString = "{\"message\":\"Something went wrong while connecting to IP Geolocation API\"}";
112
+ rescue ArgumentError => e
113
+ responseCode = "422";
114
+ jsonString = "{\"message\":\"Something went wrong while parsing IP Geolocation API response\"}";
115
+ end
116
+ responseMap = JSON.parse(jsonString)
117
+ responseMap['status'] = responseCode
118
+ return responseMap
119
+ end
120
+
121
+ def getBulkGeolocation(geolocationParams)
122
+ data = Hash.new
123
+ data["ips"] = geolocationParams.ips;
124
+ urlParams = buildGeolocationUrlParams(geolocationParams);
125
+ apiResponse = callBulkGeolocationAPIEndpoint(data, urlParams);
126
+ geolocations = Array.new
127
+ for response in apiResponse
128
+ geolocations.push(Geolocation.new(response));
129
+ end
130
+ return geolocations;
131
+ end
132
+
133
+ def callBulkGeolocationAPIEndpoint(ipAddresses, urlParams)
134
+ uri = URI.parse("https://api.ipgeolocation.io/ipgeo-bulk" + "?" + urlParams);
135
+ begin
136
+ header = {'Content-Type': 'text/json'}
137
+ http = Net::HTTP.new(uri.host, uri.port)
138
+ http.use_ssl = true
139
+ request = Net::HTTP::Post.new(uri.request_uri, header)
140
+ request.body = ipAddresses.to_json
141
+ response = http.request(request)
142
+ responseCode = response.code
143
+ jsonString = response.body
144
+ if Strings.isNullOrEmpty(responseCode) || Strings.isNullOrEmpty(jsonString)
145
+ responseCode = "422";
146
+ jsonString = "{\"message\":\"Something went wrong while parsing IP Geolocation API response\"}";
147
+ end
148
+ rescue StandardError => e
149
+ responseCode = "422";
150
+ jsonString = "{\"message\":\"Something went wrong while connecting to IP Geolocation API\"}";
151
+ rescue ArgumentError => e
152
+ responseCode = "422";
153
+ jsonString = "{\"message\":\"Something went wrong while parsing IP Geolocation API response\"}";
154
+ end
155
+ mapList = Array.new
156
+ list = JSON.parse(jsonString)
157
+ list.each do |item|
158
+ if !item['status']
159
+ item['status'] = '200'
160
+ end
161
+ mapList.push(item)
162
+ end
163
+ return mapList
164
+ end
165
+ end
@@ -0,0 +1,11 @@
1
+ class Strings
2
+ def self.nullToEmpty(s)
3
+ if(s == nil)
4
+ s = "";
5
+ end
6
+ return s;
7
+ end
8
+ def self.isNullOrEmpty(str)
9
+ return str == nil || str == ''
10
+ end
11
+ end
@@ -0,0 +1,49 @@
1
+ require_relative "TimezoneGeo"
2
+ class Timezone
3
+ attr_reader :status;
4
+ attr_reader :message;
5
+ attr_reader :timezone;
6
+ attr_reader :timezoneOffset;
7
+ attr_reader :date;
8
+ attr_reader :dateTime;
9
+ attr_reader :dateTimeTxt;
10
+ attr_reader :dateTimeWti;
11
+ attr_reader :dateTimeYmd;
12
+ attr_reader :dateTimeUnix;
13
+ attr_reader :time24;
14
+ attr_reader :time12;
15
+ attr_reader :week;
16
+ attr_reader :month;
17
+ attr_reader :year;
18
+ attr_reader :yearAbbr;
19
+ attr_reader :isDST;
20
+ attr_reader :dstSavings;
21
+ attr_reader :timezoneGeo;
22
+
23
+ def initialize(json)
24
+ @status = json["status"].to_i;
25
+ if @status != 200 || json["message"] != nil
26
+ @message = json["message"].to_s;
27
+ else
28
+ @timezone = json["timezone"];
29
+ @timezoneOffset = json["timezone_offset"];
30
+ @date = json["date"];
31
+ @dateTime = json["date_time"];
32
+ @dateTimeTxt = json["date_time_txt"];
33
+ @dateTimeWti = json["date_time_wti"];
34
+ @dateTimeYmd = json["date_time_ymd"];
35
+ @dateTimeUnix = json["date_time_unix"];
36
+ @time24 = json["time_24"];
37
+ @time12 = json["time_12"];
38
+ @week = json["week"];
39
+ @month = json["month"];
40
+ @year = json["year"];
41
+ @yearAbbr = json["year_abbr"];
42
+ @isDST = json["is_dst"];
43
+ @dstSavings = json["dst_savings"];
44
+ if json["geo"] != nil
45
+ @timezoneGeo = TimezoneGeo.new(json["geo"]);
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,24 @@
1
+ class TimezoneGeo
2
+ attr_reader :countryCode2;
3
+ attr_reader :countryCode3;
4
+ attr_reader :countryName;
5
+ attr_reader :stateProvince;
6
+ attr_reader :district;
7
+ attr_reader :city;
8
+ attr_reader :zipCode;
9
+ attr_reader :latitude;
10
+ attr_reader :longitude;
11
+
12
+ def initialize(json)
13
+ @countryCode2 = json["country_code2"];
14
+ @countryCode3 = json["country_code3"];
15
+ @countryName = json["country_name"];
16
+ @stateProvince = json["state_prov"];
17
+ @district = json["district"];
18
+ @city = json["city"];
19
+ @zipCode = json["zipcode"];
20
+ @latitude = json["latitude"];
21
+ @longitude = json["longitude"];
22
+ end
23
+
24
+ end
@@ -0,0 +1,20 @@
1
+ class TimezoneParams
2
+ attr_accessor :timezone;
3
+ attr_accessor :ip;
4
+ attr_accessor :lang;
5
+ attr_accessor :latitude;
6
+ attr_accessor :longitude;
7
+
8
+ def initialize()
9
+ @timezone = "";
10
+ @ip = "";
11
+ @lang = "en";
12
+ @latitude = 1000.0;
13
+ @longitude = 1000.0;
14
+ end
15
+
16
+ def setLocation(latitude, longitude)
17
+ @latitude = latitude;
18
+ @longitude = longitude;
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module IpgeolocationIo
2
+ VERSION = "0.1.2"
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ipgeolocation_io
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ahsan nawaz
@@ -62,8 +62,21 @@ executables: []
62
62
  extensions: []
63
63
  extra_rdoc_files: []
64
64
  files:
65
+ - LICENSE.txt
66
+ - README.md
65
67
  - bin/console
66
68
  - bin/setup
69
+ - lib/ipgeolocation_io.rb
70
+ - lib/ipgeolocation_io/Geolocation.rb
71
+ - lib/ipgeolocation_io/GeolocationCurrency.rb
72
+ - lib/ipgeolocation_io/GeolocationParams.rb
73
+ - lib/ipgeolocation_io/GeolocationTimezone.rb
74
+ - lib/ipgeolocation_io/IPGeolocationAPI.rb
75
+ - lib/ipgeolocation_io/Strings.rb
76
+ - lib/ipgeolocation_io/Timezone.rb
77
+ - lib/ipgeolocation_io/TimezoneGeo.rb
78
+ - lib/ipgeolocation_io/TimezoneParams.rb
79
+ - lib/ipgeolocation_io/version.rb
67
80
  homepage: https://ipgeolocation.io/
68
81
  licenses:
69
82
  - MIT