Module: Roda::RodaPlugins::IpShield::RequestMethods
- Defined in:
- lib/ip_shield.rb
Instance Method Summary collapse
-
#authorise_ip ⇒ String|Array
Add an IP ti the authorised list.
-
#authorised_ip? ⇒ TrueClass, FalseClass
A simple but fun way to check if the request IP is authorised.
-
#deauthorise_ip ⇒ Object
Remove an IP from the authorised list.
-
#must_be_authorised_ip ⇒ TrueClass, FalseClass
This function will raise
UnauthorisedIPerror if the request IP is not authorised.
Instance Method Details
#authorise_ip ⇒ String|Array
Add an IP ti the authorised list. You can only add any of the following:
-
IP as a string. Ex; ‘0.0.0.0’
-
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.
107 108 109 110 111 |
# File 'lib/ip_shield.rb', line 107 def 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.
47 48 49 50 51 52 53 |
# File 'lib/ip_shield.rb', line 47 def req_ip_addr = ip_addr(self) IPValidator .instance .(req_ip_addr) end |
#deauthorise_ip ⇒ Object
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.
88 89 90 91 92 |
# File 'lib/ip_shield.rb', line 88 def IPValidator .instance .remove_ip(ip_addr(self)) end |
#must_be_authorised_ip ⇒ TrueClass, 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.
71 72 73 74 75 76 77 78 |
# File 'lib/ip_shield.rb', line 71 def req_ip_addr = ip_addr(self) IPValidator .instance .(req_ip_addr) .tap { |auth| raise , 'The request IP is not authorised' unless auth } end |