timezone_detection 0.0.3 → 0.0.4

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.
@@ -1,17 +1,21 @@
1
1
  module TimezoneDetection
2
2
 
3
3
  class IpTimezone < ActiveRecord::Base
4
- include TimezoneDetection::IpConvertion
4
+ extend TimezoneDetection::IpConvertion
5
5
 
6
6
  attr_accessor :ip
7
7
  attr_accessible :ip, :utc_offset
8
8
 
9
9
  before_save :cancel_if_current_ip_already_exists
10
10
 
11
+ def self.find_ip(ip)
12
+ IpTimezone.where(ip_in_longint: IpTimezone.ip2long(ip))
13
+ end
14
+
11
15
  private
12
16
 
13
17
  def cancel_if_current_ip_already_exists
14
- self.ip_in_longint = ip2long(ip)
18
+ self.ip_in_longint = IpTimezone.ip2long(ip)
15
19
  !IpTimezone.where(ip_in_longint: self.ip_in_longint).exists? #Stops saving if return false
16
20
  end
17
21
  end
@@ -1,9 +1,12 @@
1
1
  module TimezoneDetection
2
2
  module ClassMethods
3
3
  def set_timezone
4
- ip_info = IpInfoDB.new
5
- offset = ip_info.get_timezone_offset(request.remote_ip)
6
- ip_timezone = IpTimezone.create(ip: request.remote_ip, utc_offset: offset)
4
+ ip_timezone = IpTimezone.find_ip(request.remote_ip).first
5
+ unless ip_timezone.present?
6
+ ip_info = IpInfoDB.new
7
+ offset = ip_info.get_timezone_offset(request.remote_ip)
8
+ ip_timezone = IpTimezone.create(ip: request.remote_ip, utc_offset: offset)
9
+ end
7
10
  Time.zone = ActiveSupport::TimeZone[ip_timezone.utc_offset]
8
11
  end
9
12
  end
@@ -1,3 +1,3 @@
1
1
  module TimezoneDetection
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -937,3 +937,32 @@ Connecting to database specified by database.yml
937
937
   (0.1ms) rollback transaction
938
938
   (0.1ms) begin transaction
939
939
   (0.0ms) rollback transaction
940
+ Connecting to database specified by database.yml
941
+  (0.6ms) begin transaction
942
+  (0.1ms) rollback transaction
943
+  (0.1ms) begin transaction
944
+  (0.1ms) rollback transaction
945
+  (0.0ms) begin transaction
946
+ TimezoneDetection::IpTimezone Load (0.1ms) SELECT "timezone_detection_ip_timezones".* FROM "timezone_detection_ip_timezones"
947
+  (0.1ms) SAVEPOINT active_record_1
948
+ TimezoneDetection::IpTimezone Exists (0.2ms) SELECT 1 AS one FROM "timezone_detection_ip_timezones" WHERE "timezone_detection_ip_timezones"."ip_in_longint" = 1245284686 LIMIT 1
949
+ SQL (20.4ms) INSERT INTO "timezone_detection_ip_timezones" ("ip_in_longint", "utc_offset") VALUES (?, ?) [["ip_in_longint", 1245284686], ["utc_offset", 3]]
950
+  (0.1ms) RELEASE SAVEPOINT active_record_1
951
+  (0.1ms) SELECT COUNT(*) FROM "timezone_detection_ip_timezones" 
952
+  (0.2ms) rollback transaction
953
+  (0.1ms) begin transaction
954
+  (0.0ms) SAVEPOINT active_record_1
955
+ TimezoneDetection::IpTimezone Exists (0.2ms) SELECT 1 AS one FROM "timezone_detection_ip_timezones" WHERE "timezone_detection_ip_timezones"."ip_in_longint" = 1245284686 LIMIT 1
956
+ SQL (0.2ms) INSERT INTO "timezone_detection_ip_timezones" ("ip_in_longint", "utc_offset") VALUES (?, ?) [["ip_in_longint", 1245284686], ["utc_offset", 3]]
957
+  (0.1ms) RELEASE SAVEPOINT active_record_1
958
+  (0.1ms) SAVEPOINT active_record_1
959
+ TimezoneDetection::IpTimezone Exists (0.2ms) SELECT 1 AS one FROM "timezone_detection_ip_timezones" WHERE "timezone_detection_ip_timezones"."ip_in_longint" = 1245284686 LIMIT 1
960
+  (0.1ms) ROLLBACK TO SAVEPOINT active_record_1
961
+  (0.2ms) SELECT COUNT(*) FROM "timezone_detection_ip_timezones" 
962
+  (0.2ms) rollback transaction
963
+  (0.1ms) begin transaction
964
+  (0.1ms) rollback transaction
965
+  (0.1ms) begin transaction
966
+  (0.1ms) rollback transaction
967
+  (0.1ms) begin transaction
968
+  (0.1ms) rollback transaction
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timezone_detection
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: