How to get city name and Remote IP address from Geocoder gem? - ruby-on-rails-4

Rails 4 + Ruby 2 + Geocoder gem
Controller Action
def index
#your_id = request.ip #here i am getting localhost IP address like "127.0.0.1"
binding.pry
end
I think in #your_id , it should capture IP address not localhost IP address("127.0.0.1").
But at view i need to show the city that depends on my location example if you are in NY city then it should show NY city (by ip address) dynamically.
Any idea how to get city_name and IP address?

The geocoder gem provides a #location method on the request object (source) which returns a location result with latitude and longitude:
request.location
=> #<Geocoder::Result::Freegeoip:0x007f8ecca5d608
#cache_hit=true, #data= {
"ip"=>"199.21.87.210", "country_code"=>"US",
"country_name"=>"United States", "region_code"=>"CA", "region_name"=>"California", "city"=>"Berkeley", "zipcode"=>"", "latitude"=>37.8716, "longitude"=>-122.2728, "metro_code"=>"807", "areacode"=>"510"
}>
To test this functionality in the browser, try using ngrok to expose your local server and throw some debugger statements in your controller to see what the request.location object looks like.

Related

Can't lookup ENS name on testnet of an address

I'm trying to create an app that works with ENS. I tried registering a test domain on Georli network. Here's the link when I lookup that test domain on app.ens.domains.
https://app.ens.domains/name/rikikudo6.test/details
I try using ethers to resolve ens domain as well as looking up a specific address. Here's the code I'm using:
var ethers = require("ethers");
// I use rpc endpoint generated by alchemy on Georli network
var provider = new ethers.providers.JsonRpcProvider(rpc);
(async () => {
var address = await provider.resolveName("rikikudo6.test");
console.log("address :>> ", address);
const ens = await provider.lookupAddress(address);
console.log("ens :>> ", ens);
})();
And here's the result when I run the above script:
address :>> 0x8A2D9D8e54FF47d28Cc35Aa2ba244d49F14944cA
ens :>> null
I don't understand why it can resolve the name for rikikudo6.test but can't lookup ens for the address returned by the resolving function. Am I missing anything?
I found the answer myself. The lookupAddress only returns the ENS name which is set to the default ENS name of user. If the user doesn't have the default ENS name, it will return null.

Is it possible to get the IP address of an incoming websocket connection?

My websocket server runs this way:
ws_hdl = WebSocketHandler.new do |ws|
# here we should determine the IP address of an incoming connection
end
srv = Server.new ws_hdl
srv.listen("0.0.0.0", 8080)
Is it possible to obtain an IP address of the remote host?
It is required for logging and for security reasons as well.
Thanks in advance for any good advice!
I don't know what you use but with Kemal i would do that:
require "kemal"
ws "/" do |socket, env|
p env.request
end
Kemal.run
Headers look like this:
HTTP::Headers{"Host" => "example.com:80", "User-Agent" =>
"curl/7.49.1", "Accept" => "/", "Connection" => "Upgrade", "Upgrade"
=> "websocket", "Origin" => "http://example.com:80", "Sec-WebSocket-Key" => "SGVsbG8sIHdvcmxkIQ==", "Sec-WebSocket-Version"
=> "13"}
And then need to setup proxy like Nginx in front of the client for add IP to headers: https://stackoverflow.com/a/27458651/1597964
Sorry I can't test this properly, but I think I have a solution.
The block has the web socket ws, but can also have a HTTP::Context as a second argument. This will then contain a HTTP::Request, containing the headers. Including something like REMOTE_ADDR.
ws_hdl = WebSocketHandler.new do |ws, context|
context.request.headers # all headers
context.request.headers["REMOTE_ADDR"]? # Should be the IP address
end
srv = Server.new ws_hdl
srv.listen("0.0.0.0", 8080)

Rails 4 + Storing Email address in Cookie to Remember Me - Unable to store correct email address

Here, I am trying to store email address in cookie to autocomplete the email box as per last logged in user.
Rails Controller Code :-
cookies[:email] = { :value => user_email, :expires => 7.days.from_now }
For example - admin#example.com is the email address while inspecting the email address in cookie using debugger.
Its show
admin%40example.com
By using JS I tried to retrieve the email address from cookie, now in input box it show
admin%40example.com
Please suggest how to escape this symbol conversion. Thanks
%40 is encoded form of #
I you are trying autocomplete the email address from jQuery, you can use this plain javascript function to decode it:
var cookieEmail = decodeURIComponent('admin%40example.com')
$('#emailBox').val(cookieEmail)
(#emailBox is id assigned to the html element of your email input .)

Ruby Geocoder.search by IP not returning city attribute

I am using geocoder gem to fetch coordinates by IP address. In my production environment it seems to be working but it's not returning city:
city = request.location.city
returns nil. I have tried to search my IP through the rails console and returns:
[#<Geocoder::Result::Freegeoip:0xd282cd0 #data={"ip"=>"958.....", \
"country_code"=>"NL", "country_name"=>"Netherlands", "region_code"=>"", \
"region_name"=>"", "city"=>"", "zipcode"=>"", "latitude"=>52.5, \
"longitude"=>5.75, "metro_code"=>"", "area_code"=>""}, #cache_hit=nil>]
Does anyone have an idea of why this is happening?

GeoIP always shows the server's IP address

I am trying to use GeoIP, but I have a problem when I use REMOTE_ADDR. The IP shown is that of my server and not the client.
from django.contrib.gis.geoip import GeoIP
Example context:
g = GeoIP()
ip = self.request.META.get('REMOTE_ADDR')
context['my_ip'] = ip # this display ip client
context['pais_anuncio'] = g.country_code('ip') # this display ip my server.
What am I doing wrong, Thank you.
My guess is, since you are passing the string 'ip', it is defaulting to your server's IP. Try passing in the variable ip, like this:
context['pais_anuncio'] = g.country_code(ip)