Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7458899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:44:47+00:00 2026-05-29T13:44:47+00:00

Here’s the code I wrote in the Application controller: class ApplicationController < ActionController::Base protect_from_forgery

  • 0

Here’s the code I wrote in the Application controller:

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :redirect_if_bolivia

  private

  def redirect_if_bolivia
    if (bolivia_ip_block).includes? request.remote_ip
        #redirect user to www.foo.com
    end
  end

  def bolivia_ip_block
    %w {
        12.144.86.0 - 12.144.87.255
31.201.1.176 - 31.201.1.179
46.36.198.101 - 46.36.198.105
46.136.172.0 - 46.136.172.255
63.65.11.0 - 63.65.12.254
65.173.56.0 - 65.173.63.255
67.23.241.179 - 67.23.241.198
72.32.164.56 - 72.32.164.63
72.46.244.32 - 72.46.244.47
74.91.16.48 - 74.91.16.55
74.91.16.208 - 74.91.16.215
74.91.20.48 - 74.91.20.71
74.112.134.120 - 74.112.134.127
74.112.135.104 - 74.112.135.111
74.205.37.16 - 74.205.37.23
78.24.205.32 - 78.24.205.47
98.129.27.88 - 98.129.27.95
98.129.91.40 - 98.129.91.47
166.114.0.0 - 166.114.255.255
167.157.0.0 - 167.157.255.255
174.143.165.80 - 174.143.165.87
186.0.156.0 - 186.0.159.255
186.2.0.0 - 186.2.127.255
186.27.0.0 - 186.27.127.255
190.0.248.0 - 190.0.255.255
190.3.184.0 - 190.3.191.255
    }
  end
end

So basically, what is the easiest way to check if a request.remote_ip belongs to a Bolivia IP block?

I don’t think that iterating through each block and spitting out a corresponding IP, adding it to an array is efficient. In fact, that would lead to me create an array of thousands of entries for every single request.

I’m sure this is not a new problem, so I’d love a proven solution to this.

Maybe I can check if the first three octets match, then it belongs to the block? How would I do that simple string comparison in Ruby?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T13:45:08+00:00Added an answer on May 29, 2026 at 1:45 pm

    You can convert the subnets to CIDR notation since blocks are not arbitrary. Then you can take advantage of IPAddr#include

    require 'ipaddr'
    
    class ApplicationController < ActionController::Base
      protect_from_forgery
    
      before_filter :redirect_if_bolivia
    
      private
    
      def redirect_if_bolivia
        if bolivian_blocks.any? { |block| block.include?(request.remote_ip) }
            #redirect user to www.foo.com
        end
      end
    
      def bolivian_blocks
        %w{
          12.144.86.0/23
          31.201.1.176/30
        }.map { |subnet| IPAddr.new subnet }
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my simplified data structure: Object1.h template <class T> class Object1 { private:
Here is my code, which takes two version identifiers in the form 1, 5,
Here is my problem : I have a post controller with the action create.
Here is the code I'm using http://jsbin.com/evike5/edit When the jQuery UI dialog is fired
Here's some of my production code (I had to force line breaks): task =
Here is a great article about GC may occur at unexpected point of code
Here is my javascript code for a cursor focus function to go to username
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here is my code sample, let me know if it can be further improved?
Here is my code (Say we have a single button on the page that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.