0

I'm trying to create a gem and I would like to have a feature inside it that takes the client ip address when using it. Basically it's just like the rails ActionController request.remote_ip, but within the gem I don't want to rely/depend on rails.

Is there any way to have something like this purely using Ruby?

I've found this, but when deployed to a production server it only gets the server ip address and not the client one.

Any help would be pretty much appreciated

Thanks a lot

Community
  • 1
  • 1
zanona
  • 12,345
  • 25
  • 86
  • 141
  • I had the same problem myself and tracked it all the way down to the Rack level. I never did find any way of accessing the current request object. It seems that's it's tied to the ApplicationController and there doesn't seem to be any way to access it from a gem/plugin scope. It seems the only solution is to have the IP passed in call to your gem from a controller. – aNoble Aug 17 '10 at 19:03
  • aNoble is correct, an arbitrary gem included into a Rails app does not have any path "up" to the object with the client's request context. If your gem requires this data to fulfill it's intended purpose, your API must be changed to require the information be passed "down" into your gem. This could be explicitly (as the IP address/string) or implicitly (as a reference to an object you can get the request data from) – SciPhi Oct 22 '12 at 18:55

1 Answers1

-1

it's possible because of Ruby :) if you are developing a web app you should use rack ! it's pretty cool and simple otherwise you will have to deal with the 'net' module.

hkairi
  • 385
  • 2
  • 9