Module: Roda::RodaPlugins::IpShield::RequestMethods

Defined in:
lib/ip_shield.rb

Instance Method Summary collapse

Instance Method Details

#authorise_ipString|Array

Note:

Add an IP ti the authorised list. You can only add any of the following:

  1. IP as a string. Ex; ‘0.0.0.0’

  2. An array of low and high boundaries. Ex; [‘0.0.0.0’, ‘0.0.0.7’]

The IP validity will get checked automatically before it gets added to the authorise IP list. However, there are no checks for duplicate IPs. Its recommended to check if the IP exist before authorise it.

Examples:

r.authorise_ip unless r.authorised_ip?


107
108
109
110
111
# File 'lib/ip_shield.rb', line 107

def authorise_ip
  IPValidator
    .instance
    .add_ip(ip_addr(self))
end

#authorised_ip?TrueClass, FalseClass

A simple but fun way to check if the request IP is authorised. Only added IPs are authorised. If the IP is not added, then it will be considered as deauthorise ip and therefore this function will return false.

Examples:

r.authorised_ip? ? 'IP is authorised' : 'IP is not authorised'


47
48
49
50
51
52
53
# File 'lib/ip_shield.rb', line 47

def authorised_ip?
  req_ip_addr = ip_addr(self)

  IPValidator
    .instance
    .is_authorize_ip?(req_ip_addr)
end

#deauthorise_ipObject

Remove an IP from the authorised list. The IP must be valid and exist in the authorised IP list. Its recommended to check if the IP exist before deauthorise it.

Examples:

r.deauthorise_ip if r.authorised_ip?

Raises:

  • IP_DOES_NOT_EXIST: the given IP is not within the authorised list



88
89
90
91
92
# File 'lib/ip_shield.rb', line 88

def deauthorise_ip
  IPValidator
    .instance
    .remove_ip(ip_addr(self))
end

#must_be_authorised_ipTrueClass, FalseClass

This function will raise UnauthorisedIP error if the request IP is not authorised. Use this function if you would like to hard-stop the program execution but be sure to handle the error.

Examples:

begin
r.must_be_authorised_ip
'IP is authorised'
rescue UnauthorisedIP
'IP is not authorised'
end

Raises:

  • UnauthorisedIP: The request IP is not authorised’



71
72
73
74
75
76
77
78
# File 'lib/ip_shield.rb', line 71

def must_be_authorised_ip
  req_ip_addr = ip_addr(self)

  IPValidator
    .instance
    .is_authorize_ip?(req_ip_addr)
    .tap { |auth| raise UnauthorisedIP, 'The request IP is not authorised' unless auth }
end