用于地理编码的 Webmock JSON 响应



我尝试使用webmock对HTTP请求进行地理编码。

但我总是收到此错误:

NoMethodError: undefined method `[]' for nil:NilClass

  context "geocoding" do
    before :each do
      @user = Fabricate :user
      stub_request(:get, /.*yboss.yahooapis.com*/).to_return(:body => File.read(File.join("spec", "fixtures", "geocoder", "yahoo_maps_data.json")))
    end
    it 'should geocode if coordinates missing' do
      without_coordinates = Location.new(name: "Test",
                                   street: "Revierstrasse 1",
                                   zipcode: "5020",
                                   country: "AT",
                                   user: @user, affiliate: @user)
      result = without_coordinates.geocode
      without_coordinates.coordinates.should == [47.8029, 13.0472]
    end
end

JSON 文件/spec/fixtures/geocoder/yahoo_maps_data.json

{"quality": "59",
    "latitude": "47.8029",
    "longitude": "13.0472",
    "offsetlat": "47.802898",
    "offsetlon": "13.04185",
    "radius": "8600",
    "boundingbox": {
        "north": "47.854401",
        "south": "47.7514",
        "east": "13.1075",
        "west": "12.9869"
    }, "name": "",
    "line1": "",
    "line2": "5020 Salzburg",
    "line3": "",
    "line4": "Austria",
    "cross": "",
    "house": "", "street": "",
    "xstreet": "",
    "unittype": "",
    "unit": "",
    "postal": "5020",
    "neighborhood": "",
    "city": "Salzburg",
    "county": "Salzburg",
    "state": "Salzburg",
    "country": "Austria",
    "countrycode": "AT",
    "statecode": "5",
    "countycode": "5",
    "timezone": "Europe/Vienna",
    "uzip": "5020",
    "hash": "",
    "woeid": "12816173",
    "woetype": "11"}

成功实施它的正确方法是什么?

谢谢!

我发现了错误。我的 JSON 文件不完整:

{"bossresponse": {
    "responsecode": "200",
    "placefinder": {
        "start": "0",
        "count": "1",
        "request": "flags=JXTSR&gflags=AC&locale=en_US&location=Salzburg",
        "results": [
            {
                "quality": "40",
                "latitude": "47.80067",
                "longitude": "13.04338",
                "offsetlat": "47.80067",
                "offsetlon": "13.04338",
                "radius": "8600",
                "boundingbox": {
                    "north": "47.85383",
                    "south": "47.75096",
                    "east": "13.12707",
                    "west": "12.98608"
                },
                "name": "",
                "line1": "",
                "line2": "Salzburg",
                "line3": "",
                "line4": "Austria",
                "cross": "",
                "house": "",
                "street": "",
                "xstreet": "",
                "unittype": "",
                "unit": "",
                "postal": "",
                "neighborhood": "",
                "city": "Salzburg",
                "county": "Salzburg",
                "state": "Salzburg",
                "country": "Austria",
                "countrycode": "AT",
                "statecode": "5",
                "countycode": "5",
                "timezone": "Europe/Vienna",
                "uzip": "5020",
                "hash": "",
                "woeid": "547826",
                "woetype": "7"
            }
        ]
    }
}}

最新更新