open-weather-ruby-client icon indicating copy to clipboard operation
open-weather-ruby-client copied to clipboard

Responses to calls against one_call(dt:, lat:, lon:) aren't decoded to objects correctly (using Base Plan)?

Open aleksmsd opened this issue 1 year ago • 4 comments

Using open-weather-ruby-client (0.5.0)

The Cassette used here does not match the response structure that is actually sent back calling the same endpoint with the same parameters (On my end at least), and so the resulting OpenWeather::Models::OneCall::Weather Object only has the keys "lat", "lon", "timezone".

Here's what that response looks like for the same URI:

https://api.openweathermap.org/data/3.0/onecall/timemachine?appid=<my-api-key>&dt=1589173200&lat=33.441792&lon=-94.037689
{
  "lat": 33.4418,
  "lon": -94.0377,
  "timezone": "America/Chicago",
  "timezone_offset": -18000,
  "data": [
    {
      "dt": 1589173200,
      "sunrise": 1589195910,
      "sunset": 1589245597,
      "temp": 289.91,
      "feels_like": 289.78,
      "pressure": 1025,
      "humidity": 82,
      "dew_point": 286.82,
      "clouds": 20,
      "visibility": 10000,
      "wind_speed": 0,
      "wind_deg": 0,
      "wind_gust": 0,
      "weather": [
        {
          "id": 801,
          "main": "Clouds",
          "description": "few clouds",
          "icon": "02n"
        }
      ]
    }
  ]
}

This may be a result of the fact that I'm calling with the base plan, but the object should still be able to decode properly to some degree, no?

aleksmsd avatar Sep 05 '24 02:09 aleksmsd

@aleksmsd Yes, this is likely a plan problem and yes both versions should be decodable. Add another test with the data you're seeing returned and help fix this?

dblock avatar Sep 05 '24 11:09 dblock

@aleksmsd Yes, this is likely a plan problem and yes both versions should be decodable. Add another test with the data you're seeing returned and help fix this?

@dblock I think it's probably premature to add a test before discussing how it should actually be decoded.

I patched this with an extension on my end like this:

# frozen_string_literal: true

require 'open_weather/models/one_call/weather'

module OpenWeather
  module Models
    module OneCall
      class Weather < Model
        property 'data'

        alias __initialize initialize

        # Disable this, old __initialize already calls suport
        # rubocop:disable Lint/MissingSuper
        def initialize(args = nil, options = {})
          __initialize(args, options)

          if data&.first
            self.current = OpenWeather::Models::OneCall::CurrentWeather.new(data&.first, options)
          end
        end
        # rubocop:enable Lint/MissingSuper
      end
    end
  end
end

But I don't know if other client users would want to shim this particular way.

Do you think the test should simply check if the data property is decoded as a single-element Array of CurrentWeather?

aleksmsd avatar Sep 08 '24 18:09 aleksmsd

@aleksmsd the important question is what does this API return in the different cases? then the result should match that.

dblock avatar Sep 09 '24 00:09 dblock

@dblock I think it's probably premature to add a test before discussing how it should actually be decoded.

You can write a test of what the expectations are and record another VCR for the data in your case. Then we can see the best fix.

dblock avatar Sep 09 '24 00:09 dblock

My apologies, I don't have the time to take out a PR, but I grabbed one of our Cassettes and sanitized it.

---
http_interactions:
- request:
    method: get
    uri: https://api.openweathermap.org/data/3.0/onecall/timemachine?appid=api-key&dt=1728508430&lat=33.44&lon=94.04
    body:
      encoding: US-ASCII
      string: ''
    headers:
      Accept:
      - application/json; charset=utf-8
      Content-Type:
      - application/json
      User-Agent:
      - OpenWeather Ruby Client/0.5.0
  response:
    status:
      code: 200
      message: OK
    headers:
      server:
      - openresty
      date:
      - Tue, 15 Oct 2024 21:15:45 GMT
      content-type:
      - application/json; charset=utf-8
      content-length:
      - '372'
      connection:
      - keep-alive
      x-cache-key:
      - "/data/3.0/onecall/timemachine?dt=1728508430&lat=33.44&lon=94.04"
      access-control-allow-origin:
      - "*"
      access-control-allow-credentials:
      - 'true'
      access-control-allow-methods:
      - GET, POST
    body:
      encoding: UTF-8
      string: '{"lat":33.44,"lon":94.04,"timezone":"Asia/Shanghai","timezone_offset":28800,"data":[{"dt":1728508430,"sunrise":1728517468,"sunset":1728559012,"temp":270.75,"feels_like":267.28,"pressure":1014,"humidity":90,"dew_point":269.5,"clouds":33,"wind_speed":2.5,"wind_deg":131,"wind_gust":3.14,"weather":[{"id":802,"main":"Clouds","description":"scattered
        clouds","icon":"03n"}]}]}'
  recorded_at: Tue, 15 Oct 2024 21:15:45 GMT
recorded_with: VCR 6.1.0

aleksmsd avatar Oct 15 '24 21:10 aleksmsd