Class: Ip2Geo::Methods::ConvertIPs
- Inherits:
-
Object
- Object
- Ip2Geo::Methods::ConvertIPs
- Defined in:
- lib/ip2geo/methods/convert_ips.rb
Class Method Summary collapse
-
.call(options = {}) ⇒ Hash?
Get geo information from multiple IP addresses.
Class Method Details
.call(options = {}) ⇒ Hash?
Get geo information from multiple IP addresses.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ip2geo/methods/convert_ips.rb', line 12 def call( = {}) ips = [:ips] unless ips.is_a?(Array) && !ips.empty? return error_response( Data.translate('conversions.ip-addresses-are-required-to-convert-them'), nil ) end valid_ips = [] invalid_ips = [] ips.each do |ip| validation = Helpers::IpValidation.validate(ip) if validation[:ip4] || validation[:ip6] valid_ips << ip else invalid_ips << ip end end if invalid_ips.any? all_invalid = invalid_ips.length == ips.length = all_invalid ? 'conversions.all-ip-addresses-are-invalid' : 'conversions.some-ip-addresses-are-invalid' return error_response( Data.translate(), invalid_ips.map { |ip| { 'ip' => ip, 'conversion' => nil } } ) end url = "#{Data::API_ENDPOINT}#{Data::ENDPOINTS[:CONVERT]}" body = { ips: ips } if Data::State.cache cache_key = "convert-multi:#{ips.sort.join(',')}" cached = Helpers::Cache.get(cache_key) if cached Thread.new do fresh = Helpers::Http.request(url, body) Helpers::Cache.set(cache_key, fresh) if fresh && fresh['success'] rescue StandardError # silently ignore background refresh errors end return cached end response = Helpers::Http.request(url, body) Helpers::Cache.set(cache_key, response) if response && response['success'] return response end Helpers::Http.request(url, body) rescue StandardError => e warn "[Ip2Geo::ConvertIPs] Error: #{e.}" nil end |