diff --git a/app/services/get_weather_data.rb b/app/services/get_weather_data.rb index 63573c5..a88fdfe 100644 --- a/app/services/get_weather_data.rb +++ b/app/services/get_weather_data.rb @@ -1,25 +1,25 @@ class GetWeatherData include Callable - def initialize(city) + def initialize(city, client = Clients::OpenWeatherMap) @city = city + @client = client end def call - @result = Clients::OpenWeatherMap.call(@city) - OpenStruct.new( - success?: success?, - payload: @result[:payload], - error: @result[:error], - error_status_code: error_status_code - ) - end - - def success? - @result[:error].nil? - end - - def error_status_code - @result[:error_status_code] + result = @client.call(@city) + + if result[:error].nil? + OpenStruct.new( + success?: true, + payload: result[:payload], + ) + else + OpenStruct.new( + success?: false, + error: result[:error], + error_status_code: result[:error_status_code] + ) + end end end \ No newline at end of file diff --git a/spec/requests/api/v1/weather_spec.rb b/spec/requests/api/v1/weather_spec.rb index 56911d0..45048f4 100644 --- a/spec/requests/api/v1/weather_spec.rb +++ b/spec/requests/api/v1/weather_spec.rb @@ -25,15 +25,18 @@ expect(data["wind_speed"]).to be_kind_of(Float) end - it "returns a failure message on fail" do - allow_any_instance_of(GetWeatherData).to receive(:success?).and_return(false) - allow_any_instance_of(GetWeatherData).to receive(:error_status_code).and_return('500') + it "returns a failure message and status code on fail" do + allow_any_instance_of(Clients::OpenWeatherMap).to receive(:call) + .and_return(OpenStruct.new( + error: 'example error', + error_status_code: 500 + )) city = "Tokyo" get "/api/v1/weather/#{city}" expect(response).to have_http_status(:internal_server_error) - expect(response.parsed_body["message"]).to match(/Something went wrong/) + expect(response.parsed_body["message"]).to match(/Something went wrong: example error/) end end end