immosquare-constants 0.1.11 → 0.1.13

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
  SHA256:
3
- metadata.gz: 66e6fdddfe8d5b3700f1f14d0b355b9ef0da49174db20ea516fe0aa4eb8cf23c
4
- data.tar.gz: 77d44924b9d4224fe3a7cc8eada1f98a2a0d6c5c16d30359592ea83763a2998f
3
+ metadata.gz: 5073fe427e2b2b4c43e1152815e581dd34e1ad8b212c0fb5331a3d9b17c95c28
4
+ data.tar.gz: 774752277082db562cf31143d55e5df66456148dc261ad820bf4ea25b16a3fd2
5
5
  SHA512:
6
- metadata.gz: ad8ff8d7eb7b021344d53a6e859a748d667be4f0123fba569c8dbf7a12a7016a937588a316018e207e3880bfc23863b93f8803729c18d6b53de4470c30176825
7
- data.tar.gz: 6d4b3090578cb4af74cb2b3aa686fa90765462503996ceb272b418f5d9d84f212d30aa5bb4b40ea578b62b87b28160c44603126137fda802107d26afc256a104
6
+ metadata.gz: 0dfa8a292ab75f8ed0f5b18f7360fb6b93eded2aaf2dd9ad01d308e5bcecd8b9ddf7c1f645c702a5310376ae31ec5a82a72808c2d35fe08f97f2c0839e36fc87
7
+ data.tar.gz: 685f991fa70989e6560332590d1d9309ca5b1df1e099751ccb0ec5a76514bbd4c0103b3ad60add4b736249d2db7ce84af60a8890d22d25dd883c77f7092b7573
@@ -154,7 +154,8 @@ module ImmosquareConstants
154
154
  class << self
155
155
 
156
156
  def color_name_to_hex(color_name)
157
- COLORS[color_name.to_sym] || nil
157
+ return nil if color_name.nil?
158
+ COLORS[color_name.to_s.downcase.to_sym] || nil
158
159
  end
159
160
 
160
161
  end
@@ -1,15 +1,123 @@
1
1
  require "socket"
2
2
  require "net/http"
3
3
  require "uri"
4
+ require "json"
4
5
 
5
6
  module ImmosquareConstants
6
7
  module Ip
8
+ ##============================================================##
9
+ ## Simple class to provide dot notation access to IP addresses
10
+ ##============================================================##
11
+ class IpResult
12
+
13
+ attr_reader :local, :client
14
+
15
+ def initialize(local_ip, client_ip)
16
+ @local = local_ip
17
+ @client = client_ip
18
+ end
19
+
20
+ def to_json(*)
21
+ {
22
+ :local => @local,
23
+ :client => @client
24
+ }.to_json(*)
25
+ end
26
+
27
+ def to_hash
28
+ {
29
+ :local => @local,
30
+ :client => @client
31
+ }
32
+ end
33
+
34
+ def to_s
35
+ "local: #{@local}, client: #{@client}"
36
+ end
37
+
38
+ def inspect
39
+ "#<ImmosquareConstants::Ip::IpResult local: #{@local.inspect}, client: #{@client.inspect}>"
40
+ end
41
+
42
+ def to_a
43
+ [@local, @client]
44
+ end
45
+
46
+ def to_h
47
+ to_hash
48
+ end
49
+
50
+ end
51
+
7
52
  class << self
8
53
 
9
54
  ##============================================================##
10
- ## On récupère l'IP réelle
55
+ ## Method to get the public IP address used to access the internet
56
+ ##============================================================##
57
+ def get_my_ip_from_aws
58
+ get_public_ip_from_aws
59
+ end
60
+
61
+ ##============================================================##
62
+ ## Method to get available IP addresses
11
63
  ##============================================================##
12
- def get_real_ip
64
+ def get_ips(request = nil)
65
+ IpResult.new(
66
+ get_local_ip,
67
+ get_client_ip(request)
68
+ )
69
+ end
70
+
71
+ private
72
+
73
+ ##============================================================##
74
+ ## Use a Socket to get the local IP address
75
+ ## by connecting to Google's DNS server
76
+ ##============================================================##
77
+ def get_local_ip
78
+ local_ip = nil
79
+ begin
80
+ socket = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM)
81
+ socket.connect(Addrinfo.tcp("8.8.8.8", 53))
82
+ local_ip = socket.local_address.ip_address
83
+ socket.close
84
+ rescue StandardError => e
85
+ puts("Error getting local IP: #{e.message}")
86
+ ensure
87
+ return local_ip
88
+ end
89
+ end
90
+
91
+ ##============================================================##
92
+ ## Get client IP with intelligent proxy handling
93
+ ## IP retrieval hierarchy (from most reliable to least reliable):
94
+ ## 1. HTTP_X_REAL_IP : Custom header often set by load balancers/proxies
95
+ ## 2. request.ip : Rails intelligent method that analyzes X-Forwarded-For, X-Real-IP, etc.
96
+ ## 3. request.remote_ip : Direct connection IP (might be proxy IP)
97
+ ##
98
+ ## Example: Client (203.0.113.1) → Proxy (10.0.0.1) → Rails Server
99
+ ## - request.remote_ip would return 10.0.0.1 (proxy)
100
+ ## - request.ip would return 203.0.113.1 (real client)
101
+ ## - HTTP_X_REAL_IP might contain 203.0.113.1 if set by proxy
102
+ ##============================================================##
103
+ def get_client_ip(request = nil)
104
+ begin
105
+ raise("No request provided") if request.nil?
106
+
107
+ ip = request.env["HTTP_X_REAL_IP"] || request.ip || request.remote_ip
108
+ raise("No IP found") if ip.nil?
109
+ raise("IP is localhost (127.0.0.1)") if ip == "127.0.0.1"
110
+
111
+ ip
112
+ rescue StandardError => e
113
+ get_public_ip_from_aws
114
+ end
115
+ end
116
+
117
+ ##============================================================##
118
+ ## Get public IP address with fallback (for production)
119
+ ##============================================================##
120
+ def get_public_ip_from_aws
13
121
  begin
14
122
  begin
15
123
  uri = URI.parse("https://checkip.amazonaws.com/")
@@ -19,8 +127,8 @@ module ImmosquareConstants
19
127
  response.body.strip
20
128
  rescue StandardError => e
21
129
  s = Socket.ip_address_list.find(&:ipv4_private?)
22
- s = Socket.ip_address_list.first if s.blank?
23
- raise("No IP found") if s.blank?
130
+ s = Socket.ip_address_list.first if s.nil?
131
+ raise("No IP found") if s.nil?
24
132
 
25
133
  s.ip_address
26
134
  end
@@ -29,6 +137,7 @@ module ImmosquareConstants
29
137
  end
30
138
  end
31
139
 
140
+
32
141
  end
33
142
  end
34
143
  end
@@ -212,6 +212,7 @@ module ImmosquareConstants
212
212
  ##============================================================##
213
213
  def native_name_for_locale(locale)
214
214
  begin
215
+ return nil if locale.nil? || locale.to_s.strip.empty?
215
216
  key = locale.to_s.split("-").map.with_index {|part, index| index == 0 ? part.downcase : part.upcase }.join("-").to_sym
216
217
  LOCALES[key]
217
218
  rescue StandardError
@@ -1,3 +1,3 @@
1
1
  module ImmosquareConstants
2
- VERSION = "0.1.11".freeze
2
+ VERSION = "0.1.13".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-constants
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - immosquare
@@ -41,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
41
  - !ruby/object:Gem::Version
42
42
  version: '0'
43
43
  requirements: []
44
- rubygems_version: 3.6.8
44
+ rubygems_version: 3.6.9
45
45
  specification_version: 4
46
46
  summary: ImmosquareConstants is a comprehensive collection of constants useful for
47
47
  a variety of applications.