Sha256: 57d14e38003b7e1c085b9f2472ab6a67ab054ca8b630f01981336e7b1d6881d1

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

require 'json'

module CoreExtensions
  module Net
    module HTTPResponse
      module WeatherResponse
        # rdoc
        # Returns the weather
        def weather
          parse_weather
          @weather
        end

        # rdoc
        # Returns the response code
        def weather_code
          parse_weather
          return (weather['cod'] || '200').to_i if weather.is_a? Hash

          200
        end

        # rdoc
        # Returns the response message
        def weather_message
          parse_weather
          return weather['message'] if weather.is_a? Hash

          'None'
        end

        # rdoc
        # Returns boolean if the response contains an error or not.
        def error?
          weather_code != 200
        end

        private

        # rdoc
        # Sets the weather variable
        def weather=(weather)
          @weather = weather if @weather.nil?
        end

        # rdoc
        # Attempts to parse the body to JSON.  This is so we don't have to continually
        # parse the raw JSON.
        def parse_weather
          # Try to parse the response and return a hash
          @weather = JSON.parse(body)
        rescue StandardError
          # Return the body string if parsing fails (used for html and xml responses)
          @weather = body
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
owmo-2.1.0 lib/core_extensions/net/http_response/weather_response.rb
owmo-2.0.4 lib/core_extensions/net/http_response/weather_response.rb
owmo-2.0.3 lib/core_extensions/net/http_response/weather_response.rb