1

我无法让 Ahoy 与 Geocoder 一起工作,问题与此 Ahoy有关:

module Ahoy
class GeocodeV2Job < ActiveJob::Base
[...]
if location && location.country.present? # <= but my location.country is never present...

这真的很奇怪,因为从我的控制台,我得到了这个:

> heroku run rails c

> visit = Ahoy::Visit.last

> location = Geocoder.search(visit.ip).first

=> #<Geocoder::Result::Geoip2:0x00000006e14910 @data=#<MaxMindDB::Result:0x00000006e150b8 @raw={"city"=>{"geoname_id"=>3170918, "names"=>{"de"=>"Piasco", "en"=>"Piasco", "fr"=>"Piasco", "pt-BR"=>"Piasco"}}, "continent"=>{"code"=>"EU", "geoname_id"=>6255148, "names"=>{"de"=>"Europa", "en"=>"Europe", "es"=>"Europa", "fr"=>"Europe", "ja"=>"ヨーロッパ", "pt-BR"=>"Europa", "ru"=>"Европа", "zh-CN"=>"欧洲"}}, "country"=>{"geoname_id"=>3175395, "is_in_european_union"=>true, "iso_code"=>"IT", "names"=>{"de"=>"Italien", "en"=>"Italy", "es"=>"Italia", "fr"=>"Italie", "ja"=>"イタリア共和国", "pt-BR"=>"Itália", "ru"=>"Италия", "zh-CN"=>"意大利"}}, "location"=>{"accuracy_radius"=>50, "latitude"=>44.5611, "longitude"=>7.4442, "time_zone"=>"Europe/Rome"}, "postal"=>{"code"=>"12026"}, "registered_country"=>{"geoname_id"=>3175395, "is_in_european_union"=>true, "iso_code"=>"IT", "names"=>{"de"=>"Italien", "en"=>"Italy", "es"=>"Italia", "fr"=>"Italie", "ja"=>"イタリア共和国", "pt-BR"=>"Itália", "ru"=>"Италия", "zh-CN"=>"意大利"}}, "subdivisions"=>[{"geoname_id"=>3170831, "iso_code"=>"21", "names"=>{"de"=>"Piemont", "en"=>"Piedmont", "es"=>"Piamonte", "fr"=>"Piémont", "ja"=>"ピエモンテ州", "ru"=>"Пьемонт", "zh-CN"=>"皮埃蒙特"}}, {"geoname_id"=>3177699, "iso_code"=>"CN", "names"=>{"en"=>"Provincia di Cuneo", "fr"=>"Coni"}}], "network"=>"85.159.180.0/23"}>, @cache_hit=nil>

所以位置不是零!但是之后:

> location.country
=> ""

即使

irb(main):008:0> location.data['country']['names']['en']
=> "Italy"

请注意,这location是一个:

> location.class
=> Geocoder::Result::Geoip2

我骑:

> Rails.version
=> "4.2.8"
> Ahoy::VERSION
=> "2.2.0"

使用地理编码器 1.5.0

这些是我的初始化程序:

module Ahoy
  class Store < Ahoy::DatabaseStore
    def track_visit(data)
      data[:ip] = request.headers['CF-Connecting-IP'] if request.headers['CF-Connecting-IP'].present? # Because I'm using Cloudflare
      super(data)
    end

    def track_event(data)
      data[:properties][:subdomain] = request.subdomain
      data[:properties][:domain] = request.domain
      super(data)
    end
  end
end

Ahoy.api = true
Ahoy.geocode = true
Geocoder.configure(
  lookup: :location_iq,
  api_key: ENV['LOCATION_IQ_ACCESS_TKN'],
  language: 'it',
  cache: Rails.cache,
  ip_lookup: :geoip2,
  geoip2: {
    file: Rails.root.join('lib', 'geocoder', 'GeoLite2-City.mmdb')
  }
)

我错过了什么?谢谢!

4

1 回答 1

1

好的,我想通了。像往常一样,问题是我的祖国;-)

换句话说...在我的地理编码器初始化程序中,我将其设置'it'为默认语言,嵌入在中的GeoLite2-City.mmdb数据库gem 'maxminddb'不支持意大利语。

所以我只需要弄清楚如何让 ip_lookup 用英语工作,一切都很好:)

于 2019-01-22T09:17:00.460 回答