{"id":15563072,"url":"https://github.com/dkam/datacenter_detector","last_synced_at":"2025-04-23T23:20:45.185Z","repository":{"id":50747718,"uuid":"519907493","full_name":"dkam/datacenter_detector","owner":"dkam","description":"Detect Datacenter by IP","archived":false,"fork":false,"pushed_at":"2023-05-16T01:14:36.000Z","size":51,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-30T04:31:43.534Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dkam.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-07-31T23:17:30.000Z","updated_at":"2023-05-16T16:34:49.000Z","dependencies_parsed_at":"2025-03-06T19:31:39.277Z","dependency_job_id":"6fbf6569-a264-42a3-a1f6-0e6b67fbc49b","html_url":"https://github.com/dkam/datacenter_detector","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dkam%2Fdatacenter_detector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dkam%2Fdatacenter_detector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dkam%2Fdatacenter_detector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dkam%2Fdatacenter_detector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dkam","download_url":"https://codeload.github.com/dkam/datacenter_detector/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250529618,"owners_count":21445655,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-02T16:17:19.774Z","updated_at":"2025-04-23T23:20:45.160Z","avatar_url":"https://github.com/dkam.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DatacenterDetector\nThe DatacenterDetector gem is no more. Below is a simple class to perform the same actions, but using a Redis cache via Kredis.\n\nThe following Ruby class is a slim wrapper around the [IP Address API](https://github.com/NikolaiT/IP-Address-API).\n\nThe number of hits and misses is also tracked.\n\n# A Working Cache\n\n```ruby\nrequire 'kredis'\nrequire 'open-uri'\n\nclass IpAddressApi\n  # Outside of Rails, configure Kredis like: \n  #   Kredis::Connections.connections[:shared] = Redis.new(url: \"redis://localhost:6379/0\")\n  attr_accessor :prefix, :agent, :cache_only, :ttl\n\n  def initialize(prefix: IpAddressApi.prefix, agent: IpAddressApi.agent, cache_only: false, ttl: 60 * 60 * 24)\n    @cache_only = cache_only\n    @prefix     = prefix\n    @agent      = agent\n    @ttl        = ttl \n  end\n  \n  def key_name(ip) = \"#{@prefix}#{ip}\"\n  def hit_counter  = @hit_counter  ||= Kredis.counter(\"#{prefix}hits\")\n  def miss_counter = @miss_counter ||= Kredis.counter(\"#{prefix}miss\")\n  \n  def stats\n    hit  = hit_counter.value\n    miss = miss_counter.value\n\n    {hit: hit, miss:, total: hit + miss}\n  end\n\n  def hitrate\n    hit  = hit_counter.value\n    miss = miss_counter.value\n    \n    hit / (hit + miss).to_f\n  end\n\n  def reset_stats\n    hit_counter.reset\n    miss_counter.reset\n  end\n\n  def lookup(ip=nil)\n    return {} if ip.nil?\n\n    ip_data = Kredis.json(key_name(ip), expires_in: ttl)\n    \n    if ip_data.value.blank? \u0026\u0026 cache_only == true\n      miss!\n    elsif ip_data.value.blank? \u0026\u0026 cache_only == false\n      miss!\n      ip_data.value = JSON.parse( URI.open(\"https://ipapi.is/json/?q=#{ip}\", \"User-Agent\" =\u003e agent).read)\n      ip_data.value ||= {}\n    else\n      hit!\n    end\n\n    return ip_data.value\n  rescue OpenURI::HTTPError =\u003e e\n    puts(\"IP Address API error looking up IP: #{ip} : #{e.inspect}\")\n    return {}\n  end\n\n  def self.prefix      = 'IpAddressApi_'\n  def self.agent       = \"Ruby/#{RUBY_VERSION}\"\n  def self.stats       = IpAddressApi.new.stats\n  def self.reset_stats = IpAddressApi.new.reset_stats\n  def self.hitrate     = IpAddressApi.new.hit_rate\n\n  def self.lookup(ip=nil, cache_only: false, agent: nil) = IpAddressApi.new(cache_only:, agent:).lookup(ip)\n\n  private\n  def hit!         = hit_counter.increment\n  def miss!        = miss_counter.increment\nend\n```\n\n## Usage\n\nYou can use an instance or class methods.\n\n```ruby\n\u003e result = IpAddressApi.lookup('1.1.1.1')\n\u003e result.dig('is_datacenter')\n=\u003e false\n\u003e result.dig('company', 'name')\n=\u003e \"APNIC Research and Development\"\n\u003e result.dig('asn', 'org')\n=\u003e \"Cloudflare, Inc.\"\n\n\u003e client = IpAddressApi.new\n\u003e client.lookup('52.93.127.126')\n\u003e result.is_datacenter\n=\u003e true\n\u003e result.name\n=\u003e \"Amazon Technologies Inc.\"\n\nThe cache records its hitrate:\n\n```ruby\n\u003e client.hitrate\n=\u003e 0.6829268292682927\n```\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdkam%2Fdatacenter_detector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdkam%2Fdatacenter_detector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdkam%2Fdatacenter_detector/lists"}