即使没有location.data ['country'] ['names'] ['en'],Geocoder :: Result :: Geoip2#country仍返回nil

时间:2019-01-22 08:22:13

标签: ruby-on-rails ruby rails-geocoder ahoy

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

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<ESHIPPER>
  <LOGIN>
    <COMPANY><%= credentials.company %></COMPANY>
    <PASSWORD><%= credentials.password %></PASSWORD>
    <APPID>EC</APPID>
    <APPVERSION>2.2</APPVERSION>
  </LOGIN>
  <CONSIGNMENTBATCH>
    <SENDER>
      <COMPANYNAME><%= transform_to_tnt_char_set(sender.company_name) %></COMPANYNAME>
      <STREETADDRESS1><%= transform_to_tnt_char_set(sender.address_line1) %></STREETADDRESS1>
      <STREETADDRESS2><%= transform_to_tnt_char_set(sender.address_line2) %></STREETADDRESS2>
      <STREETADDRESS3><%= transform_to_tnt_char_set(sender.address_line3) %></STREETADDRESS3>
      <CITY><%= transform_to_tnt_char_set(sender.city) %></CITY>
      <PROVINCE><%= sender.state_code.nil? ? "" : transform_to_tnt_char_set(sender.state_code) %></PROVINCE>
      <POSTCODE><%= xml_escape(sender.zip_code) %></POSTCODE>
      <COUNTRY><%= xml_escape(sender.country_code.upcase) %></COUNTRY>
      <% unless shipping_options.import %>
        <ACCOUNT><%= credentials.account %></ACCOUNT>
      <% else %>
        <ACCOUNT/>
      <% end %>
      <CONTACTNAME><%= transform_to_tnt_char_set(sender.attention) %></CONTACTNAME>
      <CONTACTDIALCODE><%= transform_to_tnt_char_set(sender.phone_number_dial_code) %></CONTACTDIALCODE>
        <CONTACTTELEPHONE><%= transform_to_tnt_char_set(sender.phone_number_number) %></CONTACTTELEPHONE>
      <CONTACTEMAIL><%= xml_escape(sender.email) %></CONTACTEMAIL>
      <COLLECTION>
        <COLLECTIONADDRESS>
          <COMPANYNAME><%= transform_to_tnt_char_set(collection_info.company_name) %></COMPANYNAME>
          <STREETADDRESS1><%= transform_to_tnt_char_set(collection_info.address_line1) %></STREETADDRESS1>
          <STREETADDRESS2><%= transform_to_tnt_char_set(collection_info.address_line2) %></STREETADDRESS2>
          <STREETADDRESS3><%= transform_to_tnt_char_set(collection_info.address_line3) %></STREETADDRESS3>
          <CITY><%= transform_to_tnt_char_set(collection_info.city) %></CITY>
          <PROVINCE><%= collection_info.state_code.nil? ? "" : transform_to_tnt_char_set(collection_info.state_code) %></PROVINCE>
          <POSTCODE><%= xml_escape(collection_info.zip_code) %></POSTCODE>
          <COUNTRY><%= xml_escape(collection_info.country_code.upcase) %></COUNTRY>
          <CONTACTNAME><%= transform_to_tnt_char_set(collection_info.attention) %></CONTACTNAME>
          <CONTACTDIALCODE><%= transform_to_tnt_char_set(collection_info.dial_code) %></CONTACTDIALCODE>
          <CONTACTTELEPHONE><%= transform_to_tnt_char_set(collection_info.telephone) %></CONTACTTELEPHONE>
          <CONTACTEMAIL><%= collection_info.email ? xml_escape(collection_info.email) : "" %></CONTACTEMAIL>
        </COLLECTIONADDRESS>
        <SHIPDATE><%= shipment.shipping_date.strftime('%d/%m/%Y') %></SHIPDATE>
        <PREFCOLLECTTIME>
          <FROM><%= transform_to_tnt_char_set(collection_info.from_time) %></FROM>
          <TO><%= transform_to_tnt_char_set(collection_info.to_time) %></TO>
        </PREFCOLLECTTIME>
        <COLLINSTRUCTIONS><%= transform_to_tnt_char_set(collection_info.instructions) %></COLLINSTRUCTIONS>
      </COLLECTION>
    </SENDER>
    <CONSIGNMENT>
      <CONREF><%= shipment.shipment_id %></CONREF>
      <DETAILS>
        <RECEIVER>
          <COMPANYNAME><%= transform_to_tnt_char_set(recipient.company_name) %></COMPANYNAME>
          <STREETADDRESS1><%= transform_to_tnt_char_set(recipient.address_line1) %></STREETADDRESS1>
          <STREETADDRESS2><%= transform_to_tnt_char_set(recipient.address_line2) %></STREETADDRESS2>
          <STREETADDRESS3><%= transform_to_tnt_char_set(recipient.address_line3) %></STREETADDRESS3>
          <CITY><%= transform_to_tnt_char_set(recipient.city) %></CITY>
          <PROVINCE><%= recipient.state_code.nil? ? "" : transform_to_tnt_char_set(recipient.state_code) %></PROVINCE>
          <POSTCODE><%= xml_escape(recipient.zip_code) %></POSTCODE>
          <COUNTRY><%= xml_escape(recipient.country_code.upcase) %></COUNTRY>
          <CONTACTNAME><%= transform_to_tnt_char_set(recipient.attention) %></CONTACTNAME>
          <CONTACTDIALCODE><%= transform_to_tnt_char_set(recipient.phone_number_dial_code) %></CONTACTDIALCODE>
          <CONTACTTELEPHONE><%= transform_to_tnt_char_set(recipient.phone_number_number) %></CONTACTTELEPHONE>
          <CONTACTEMAIL><%= xml_escape(recipient.email) %></CONTACTEMAIL>
          <% if shipping_options.import %>
            <ACCOUNT><%= credentials.account %></ACCOUNT>
            <ACCOUNTCOUNTRY><%= xml_escape(recipient.country_code.upcase) %></ACCOUNTCOUNTRY>
          <% end %>
        </RECEIVER>
        <DELIVERY>
          <COMPANYNAME><%= transform_to_tnt_char_set(recipient.company_name) %></COMPANYNAME>
          <STREETADDRESS1><%= transform_to_tnt_char_set(recipient.address_line1) %></STREETADDRESS1>
          <STREETADDRESS2><%= transform_to_tnt_char_set(recipient.address_line2) %></STREETADDRESS2>
          <STREETADDRESS3><%= transform_to_tnt_char_set(recipient.address_line3) %></STREETADDRESS3>
          <CITY><%= transform_to_tnt_char_set(recipient.city) %></CITY>
          <PROVINCE><%= recipient.state_code.nil? ? "" : transform_to_tnt_char_set(recipient.state_code) %></PROVINCE>
          <POSTCODE><%= xml_escape(recipient.zip_code) %></POSTCODE>
          <COUNTRY><%= xml_escape(recipient.country_code.upcase) %></COUNTRY>
          <CONTACTNAME><%= transform_to_tnt_char_set(recipient.attention) %></CONTACTNAME>
          <CONTACTDIALCODE><%= transform_to_tnt_char_set(recipient.phone_number_dial_code) %></CONTACTDIALCODE>
          <CONTACTTELEPHONE><%= transform_to_tnt_char_set(recipient.phone_number_number) %></CONTACTTELEPHONE>
          <CONTACTEMAIL><%= xml_escape(recipient.email) %></CONTACTEMAIL>
        </DELIVERY>
        <CUSTOMERREF><%= xml_escape(shipment.minimized_reference) %></CUSTOMERREF>
        <CONTYPE><%= shipping_options.contype %></CONTYPE>
        <PAYMENTIND><%= shipping_options.import ? 'R' : 'S' %></PAYMENTIND>
        <ITEMS><%= shipment.number_of_packages %></ITEMS>
        <TOTALWEIGHT><%= shipment.package_dimensions.total_weight %></TOTALWEIGHT>
        <% if shipping_options.service == TNTShipperLib::ServiceCodes::International::EXPRESS_DOCUMENT %>
          <TOTALVOLUME>0.0</TOTALVOLUME>
        <% else %>
          <TOTALVOLUME><%= "%.10f" % (shipment.package_dimensions.total_volume / 1000000.0) %></TOTALVOLUME>
        <% end %>
        <% if shipment.dutiable %>
          <CURRENCY><%= shipment.customs_currency %></CURRENCY>
          <GOODSVALUE><%= ActiveSupport::NumberHelper.number_to_rounded(shipment.customs_amount, precision: 2) %></GOODSVALUE>
        <% end %>
        <SERVICE><%= shipping_options.service %></SERVICE>
        <OPTION></OPTION>
        <DESCRIPTION><%= xml_escape(shipment.description) %></DESCRIPTION>
        <DELIVERYINST></DELIVERYINST>
        <% unless shipping_options.service == TNTShipperLib::ServiceCodes::International::EXPRESS_DOCUMENT %>
          <% shipment.package_dimensions.dimensions.each do |dimension| %>
            <PACKAGE>
              <ITEMS>1</ITEMS>
              <DESCRIPTION><%= xml_escape(shipment.description) %></DESCRIPTION>
              <LENGTH><%= dimension.length / 100.0 %></LENGTH>
              <HEIGHT><%= dimension.height / 100.0 %></HEIGHT>
              <WIDTH><%= dimension.width / 100.0 %></WIDTH>
              <WEIGHT><%= dimension.weight %></WEIGHT>
            </PACKAGE>
          <% end %>
        <% end %>
      </DETAILS>
    </CONSIGNMENT>
  </CONSIGNMENTBATCH>
  <ACTIVITY>
    <CREATE>
      <CONREF><%= shipment.shipment_id %></CONREF>
    </CREATE>
    <% if collection_info.book? %>
      <BOOK ShowBookingRef="Y">
        <CONREF><%= shipment.shipment_id %></CONREF>
      </BOOK>
    <% end %>
    <SHIP>
      <CONREF><%= shipment.shipment_id %></CONREF>
    </SHIP>
    <PRINT>
      <CONNOTE>
        <CONREF><%= shipment.shipment_id %></CONREF>
      </CONNOTE>
      <LABEL>
        <CONREF><%= shipment.shipment_id %></CONREF>
      </LABEL>
      <MANIFEST>
        <CONREF><%= shipment.shipment_id %></CONREF>
      </MANIFEST>
      <INVOICE>
        <CONREF><%= shipment.shipment_id %></CONREF>
      </INVOICE>
    </PRINT>
  </ACTIVITY>
</ESHIPPER>

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

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

使用地理编码器1.5.0

这些是我的初始化程序:

> Rails.version
=> "4.2.8"
> Ahoy::VERSION
=> "2.2.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

我想念什么? 谢谢!

1 个答案:

答案 0 :(得分:1)

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

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

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