Sha256: 8638f633e9ca70b375f917e83d796bddf84dbaae64ff253645b7a3f9b30392fd

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

require 'core_extensions/net/http_response/weather_response'
require 'net/http'
require 'uri'


=begin rdoc
Include some weather response info into Net::HTTPResponse
=end
Net::HTTPResponse.include CoreExtensions::Net::HTTPResponse::WeatherResponse

module OWMO
=begin rdoc
Net module Mixins
=end
  module API

=begin rdoc
Weather response error to handle errors received from OpenWeatherMap.orgs API
=end
    class WeatherResponseError < StandardError
      def initialize(response)
        @response = response
        super("ERROR #{@response.weather_code}: #{@response.weather_message}")
      end # initialize
    end # WeatherResponseError

=begin rdoc
Sends the GET request to OpenWeatherMap.org, and returns the response
=end
    def self.get(uri)

      raise "Invalid URI:  Expected URI, got #{uri.class}" unless uri.is_a? URI

      # Send the request
      request = Net::HTTP::Get.new(uri)

      response = Net::HTTP.start(uri.hostname, uri.port) do |http|
        http.request(request)
      end # response

      # Check the response
      raise OWMO::API::WeatherResponseError.new(response) unless response.weather_code == 200

      return response.weather

    end # get
  end # Net

end # OWMO

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
owmo-1.0.0 lib/owmo/api.rb
owmo-0.2.0 lib/owmo/api.rb