Sha256: f0dcaf8cb484b3cb99a2a3319a0efe9bc416e295e0cfb5a1ea5840056795c3d0

Contents?: true

Size: 757 Bytes

Versions: 1

Compression:

Stored size: 757 Bytes

Contents

require 'json'
require 'net/http'
require 'uri'

module UndergroundWeather
  class ApiCall
    attr_reader :response, :error
    
    BASE_URL = 'http://api.wunderground.com/api'
        
    def initialize(api_key, feature, query)
      @api_key = api_key
      @feature = feature
      @query   = query
      @error   = false

      @response = JSON.parse(get)
      @error = true if @response['response']['error']
    end
    
    def url
      URI.parse("#{BASE_URL}/#{@api_key}/#{@feature}/q/#{@query}.json")
    end    
    
    def get
      resp = Net::HTTP.get_response(url)
      
      if resp.code == "200"
        resp.body
      else
        @error = true
        
        # raise exception here
        {}
      end    
    end
  end  
end
  

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
undergroundweather-0.0.2 lib/undergroundweather/api_call.rb