Rails Gem Geocoder Lookup method not obeying /config/initializers/geocoder.rb - ruby-on-rails

I dont know exactly why but the gem is working properly except by the fact that it appears to not obbey the configuration file.
When I test its initialization with a 'puts' command it show in the console that the file is beeing readed.
The central problem is that i want to make my geocoder gem to use the method lookup to determine the user location with maxmind's or google's ip location services because freegeoip just give me wrong results.
Here is my /config/initializers/geocoder.rb file witch i created by myself:
# config/initializers/geocoder.rb
def self.options_and_defaults
[
# geocoding service timeout (secs)
[:timeout, 3],
# name of geocoding service (symbol)
[:lookup, :google],
# ISO-639 language code
[:language, :en],
# use HTTPS for lookup requests? (if supported)
[:use_https, false],
# HTTP proxy server (user:pass#host:port)
[:http_proxy, nil],
# HTTPS proxy server (user:pass#host:port)
[:https_proxy, nil],
# API key for geocoding service
[:api_key, nil],
# cache object (must respond to #[], #[]=, and #keys)
[:cache, nil],
# prefix (string) to use for all cache keys
[:cache_prefix, "geocoder:"],
# exceptions that should not be rescued by default
# (if you want to implement custom error handling);
# supports SocketError and TimeoutError
[:always_raise, []]
]
end
I also used this code with my google api code and google for lookup, and with maxmind... both returns me that actually geocoder still uses freegeoip
I implemented a 'puts' entry on my controller with:
puts 'oioioi'
puts request.ip
puts Geocoder.search(request.ip).first
puts request.location
so i could detect it on the console output.
I got:
oioioi
127.0.0.1
#<Geocoder::Result::Freegeoip:0x007fbe846eb258>
#<Geocoder::Result::Freegeoip:0x007fbe847232e8>
I know that im using a local IP but i already tested on deplyoment and the fact is that the lookup is still using Freegeoip as lookup server.
Hope anybody could help me... Thanks in advance.

I wasn't using the right config. I needed to set :ip_lookup to get it working.

Related

How do I transfer Data using Web Server/TCPsockets in Ruby?

I have a data scraper in ruby that retrieves article data.
Another dev on my team needs my scraper to spin up a webServer he can make a request to so that he may import the data on a Node Application he's built.
Being a junior, I do not understand the following :
a) Is there a proper convention in Rails that tells me where to place my scraper.rb file
b) Once that file is properly placed, how would i get the server to accept connections with the scrapedData
c)What (functionally) is the relationship between the ports, sockets, and routing
I understand this may be a "rookieQuestion" but I honestly dont know.
Can someone please BREAK THIS DOWN.
I have already:
i) Setup a server.rb file and have it linking to localhost:2000 but Im not sure how to create a proper route or connection that allows someone to use Postman for a valid route and connect to my data.
require 'socket'
require 'mechanize'
require 'awesome_print'
port = ENV.fetch("PORT",2000).to_i
server = TCPServer.new(port)
puts "Listening on port #{port}..."
puts "Current Time : #{Time.now}"
loop do
client = server.accept
client.puts "= Running Web Server ="
general_sites = [
"https://www.lovebscott.com/",
"https://bleacherreport.com/",
"https://balleralert.com/",
"https://peopleofcolorintech.com/",
"https://afrotech.com/",
"https://bossip.com/",
"https://www.itsonsitetv.com/",
"https://theshaderoom.com/",
"https://shadowandact.com/",
"https://hollywoodunlocked.com/",
"https://www.essence.com/",
"http://karencivil.com/",
"https://www.revolt.tv/"
]
holder=[]
agent = Mechanize.new
general_sites.each do |site|
page=agent.get(site);
newRet = page.search('a')
newRet.each do |e|
data = e.attr('href').to_s
if(data.length > 50)
holder.push(data)
end
end
pp holder.length.to_s + " [ posts total] ==> Now Scraping --> " + site
end
client.write(holder)
client.close
end
In Rails you don't spin up a web server manually, as it's done for you using rackup, unicorn, puma or any other compatible application server.
Rails itself is never "talking" to the HTTP clients directly, it is just a specific application that exposes a rack-compatible API (basically have an object that responds to call(hash) and returns [integer, hash, enumerable_of_strings]); the app server will get the data from unix/tcp sockets and call your application.
If you want to expose your scraper to an external consumer (provided it's fast enough), you can create a controller with a method that accepts some data, runs the scraper, and finally renders back the scraping results in some structured way. Then in the router you connect some URL to your controller method.
# config/routes.rb
post 'scrape/me', to: 'my_controller#scrape'
# app/controllers/my_controller.rb
class MyController < ApplicationController
def scrape
site = params[:site]
results = MyScraper.run(site)
render json: results
end
end
and then with a simple POST yourserver/scrape/me?site=www.example.com you will get back your data.

json-rpc event-machine stand alone service

What am I doing wrong?
I try to run example code from json-rpc documentation. Togather with EventMachine:
require 'json-rpc'
require 'thin'
class AsyncApp
include JsonRpc
AsyncResponse = [-1, {}, []].freeze
def call env
rpc_call(env)
end
def rpc_sum a, b
result = Rpc::AsyncResult.new
EventMachine::next_tick do
result.reply a + b
result.succeed
end
result
end
end
EM::run do
Thin::Server.start('0.0.0.0', 8999) do
map('/'){ run AsyncApp.new }
end
end
There is no error on this server console appears.
The result is on transport layer on the json-rpc client is:
500 Internal Server Error
I've try same client with jimson gem implememtation - it work fine but does not support EventMachine and async calls. (Show example if you know how it possible)
The problem was at default "welcome" page assigned to route "/".
I do not try to go with browser to "/", but only try to connect by rpc client.
Some how default "welcome" page route "/" rule is not overwrited by map("/"){...} rule.
The solution is rewrite route rule like this map("/rpc"){...}

How to use $remote_addr with rails and nginx secure_link

I have a rails application that makes calls to another server via net::http to retrieve documents.
I have set up Nginx with secure_link.
The nginx config has
secure_link $arg_md5,$arg_expires;
secure_link_md5 "$secure_link_expires$uri$remote_addr mySecretCode";
On the client side (which is in fact my rails server) I have to create the secure url something like:
time = (Time.now + 5.minute).to_i
hmac = Digest::MD5.base64digest("#{time}/#{file_path}#{IP_ADDRESS} mySecretCode").tr("+/","-_").gsub("==",'')
return "#{DOCUMENT_BASE_URL}/#{file_path}?md5=#{hmac}&expires=#{time}"
What I want to know is the best way to get the value above for IP_ADDRESS
There are multiple answers in SO on how to get the ip address but alot of them do not seem as reliable as actually making a request to a web service that returns the ip address of the request as this is what the nginx secure link will see (we don't want some sort of localhost address).
I put the following method on my staging server:
def get_client_ip
data=Hash.new
begin
data[:ip_address]=request.ip
data[:error]=nil
rescue Exception =>ex
data[:error]=ex.message
end
render :json=>data
end
I then called the method from the requesting server:
response = Net::HTTP.get_response(URI("myserver.com/web_service/get_client_ip"))
if response.class==Net::HTTPOK
response_hash=JSON.parse response.body
ip=response_hash["ip_address"] unless response_hash[:error]
else
#deal with error
end
After getting the ip address successfully I just cached it and did not keep on calling the web service method.

What does 'HTTP Response: 403 Forbidden' mean when trying to access Amazon's API?

I'm building a simple Ruby on Rails application and am using the Amazon Product Advertising API (amazon-ecs) gem docs. Right now I am just trying to get a response from Amazon and I'm getting a 403 Forbidden error. This is the following code that I have so far:
Amazon::Ecs.options = {
:AWS_access_key_id => '[myaccesskey]',
:AWS_secret_key => '[mysecretkey]'
}
res = Amazon::Ecs.item_search('soccer books')
puts res.total_items
Expecting to see an integer with the number of responses but the code stops running and gives me an error.
I'm going exactly by the documentation so I'm not sure what I'm doing wrong here.
According to documentation:
# Set the default options; options will be camelized and converted to REST request parameters.
# associate_tag and AWS_access_key_id are required options, associate_tag is required option
# since API version 2011-08-01.
#
# To sign your request, include AWS_secret_key.
So it seems like you should just add associate_tag option, since it is required option.
More about associate tag: http://docs.aws.amazon.com/AWSECommerceService/latest/DG/AssociateTag.html

geocoder not working (possibly due to network proxy? )

I am a beginner in Rails. I got to know exciting feature of geocoder from railscasts
[ http://railscasts.com/episodes/273-geocoder ]
But same source code also downloaded from it not working behind proxy. as it doesn't populate any longitudes or latitudes.
How to deal with with proxyserver of my workspace network?
else from another machine having direct internet connection things work fine.
geocoder has http proxy support, but it's not obvious from the documentation for where to configure it.
you can find it when looking at the initializer, that should get created for your rails generate call: https://github.com/alexreisner/geocoder/blob/master/lib/generators/geocoder/config/templates/initializer.rb
Geocoder.configure(
[...]
# :http_proxy => nil, # HTTP proxy server (user:pass#host:port)
# :https_proxy => nil, # HTTPS proxy server (user:pass#host:port)
)

Resources