module CoreExtensions::Net::HTTPResponse::WeatherResponse

Public Instance Methods

has_error?() click to toggle source

Returns boolean if the response contains an error or not.

# File lib/core_extensions/net/http_response/weather_response.rb, line 37
def has_error?
  weather_code != 200
end
weather() click to toggle source

Returns the weather

# File lib/core_extensions/net/http_response/weather_response.rb, line 11
def weather
  parse_weather
  @weather
end
weather_code() click to toggle source

Returns the response code

# File lib/core_extensions/net/http_response/weather_response.rb, line 19
def weather_code
  parse_weather
  return (weather['cod'] || "200").to_i if weather.is_a? Hash
  200
end
weather_message() click to toggle source

Returns the response message

# File lib/core_extensions/net/http_response/weather_response.rb, line 28
def weather_message
  parse_weather
  return weather['message'] if weather.is_a? Hash
  "None"
end

Private Instance Methods

parse_weather() click to toggle source

Attempts to parse the body to JSON. This is so we don't have to continually parse the raw JSON.

# File lib/core_extensions/net/http_response/weather_response.rb, line 54
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
weather=(weather) click to toggle source

Sets the weather variable

# File lib/core_extensions/net/http_response/weather_response.rb, line 46
def weather=(weather)
  @weather = weather if @weather.nil?
end