small medium large xlarge

Generic-user-small
10 Jun 2016, 19:09
H. Paul Robertson (9 posts)

I’m just getting going in “Day 0” and I’m getting an error with the weather endpoint.

Using the curl command in the book returns a bunch of HTML. I opened that url in a browser and got an error page:

NoMethodError in WeatherController#zip
undefined method `[]' for nil:NilClass

Extracted source (around line #35):
            
  def self.from_json_string(json_string)
    json = JSON.parse json_string
    item = json["query"]["results"]["channel"]["item"]
    conditions = item["condition"]
    forecasts = item["forecast"]
    self.new.tap do |r|

If I go to /weather in the browser it shows a form for entering a zip code, but when I enter a value and submit it gives the same error.

Mypicture_pragsmall
10 Jun 2016, 19:16
Tony Hillerson (39 posts)

Hey Paul,

The API doesn’t have a lot of error checking. This looks like you’re getting bad data back from the Yahoo Weather API for one reason or another. From the rails logs (or the console that’s running rails) can you tell me the request you made?

Generic-user-small
10 Jun 2016, 19:37
H. Paul Robertson (9 posts)

This is the command I ran in the terminal:

curl -H "Content-Type: application/json" -H "Accept: application/json" http://localhost:3000/weather/80003

This is what showed up in the terminal that’s running rails:

Started GET "/weather/80003" for ::1 at 2016-06-10 14:32:52 -0500
Processing by WeatherController#zip as JSON
  Parameters: {"zip"=>"80003", "weather"=>{}}
Completed 500 Internal Server Error in 583ms (ActiveRecord: 0.0ms)

NoMethodError (undefined method `[]' for nil:NilClass):
  app/models/weather_report.rb:35:in `from_json_string'
  app/models/weather_report.rb:30:in `report_from_url'
  app/models/weather_report.rb:15:in `for_zip'
  app/controllers/weather_controller.rb:27:in `zip'


  Rendered /Users/paul/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_source.erb (3.6ms)
  Rendered /Users/paul/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.3ms)
  Rendered /Users/paul/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
  Rendered /Users/paul/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (59.7ms)
Cannot render console with content type application/jsonAllowed content types: [#<Mime::Type:0x007fd7c9d378d8 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, #<Mime::Type:0x007fd7c9d37518 @synonyms=[], @symbol=:text, @string="text/plain">, #<Mime::Type:0x007fd7ca0eebe8 @synonyms=[], @symbol=:url_encoded_form, @string="application/x-www-form-urlencoded">]
Generic-user-small
10 Jun 2016, 21:44
H. Paul Robertson (9 posts)

Update: the url in “Day 0” for testing doesn’t work, but if I use CURL with the URL in the actual app code then it works:

curl -H "Content-Type:application/json" -H "Accept:application/json" http://localhost:3000/weather/location.json?name=Arvada,CO
Mypicture_pragsmall
14 Jun 2016, 03:46
Tony Hillerson (39 posts)

Good catch, Paul. I’m not sure why the zip form isn’t working, but I did end up just using the location form for any kind from the code. I’ve fixed the API chapter just now.

You must be logged in to comment