Sha256: 05510e9197fd438214550205b5aee272f4ae977eafb25f5dd5df0d697b287212

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'json'

module CoreExtensions
  module Net
    module HTTPResponse
      module WeatherResponse

=begin rdoc
Returns the weather
=end
        def weather
          parse_weather
          @weather
        end

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

=begin rdoc
Returns the response message
=end
        def weather_message
          parse_weather
          return weather['message'] if weather.is_a? Hash
          "None"
        end

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

        private

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

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

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
owmo-2.0.1 lib/core_extensions/net/http_response/weather_response.rb