Skip to content

Instantly share code, notes, and snippets.

@kyrylo
Last active August 7, 2024 06:11
Show Gist options
  • Select an option

  • Save kyrylo/563a8cb6f44532b58ccb48bf0d30651f to your computer and use it in GitHub Desktop.

Select an option

Save kyrylo/563a8cb6f44532b58ccb48bf0d30651f to your computer and use it in GitHub Desktop.

Revisions

  1. kyrylo revised this gist Aug 5, 2024. No changes.
  2. kyrylo created this gist Aug 5, 2024.
    34 changes: 34 additions & 0 deletions service.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    # frozen_string_literal: true

    class ApplicationService
    def self.call(...)
    new(...).call
    end

    def initialize(...)
    end
    end

    class CurrentIpService < ApplicationService
    def initialize(request)
    super

    @request = request
    end

    def call
    ip_headers = [
    @request.env["HTTP_CF_CONNECTING_IP"],
    @request.env["HTTP_CLIENT_IP"],
    @request.env["HTTP_X_FORWARDED_FOR"],
    @request.env["HTTP_X_FORWARDED"],
    @request.env["HTTP_FORWARDED_FOR"],
    @request.env["HTTP_FORWARDED"],
    @request.env["REMOTE_ADDR"]
    ]

    (ip_headers.find(&:present?) || "127.0.0.1").split(",").first
    end
    end

    CurrentIpService.call(request)