Sha256: 797f58dae44db54ab7d368bf030963209f4150d2fba6457d302ebd493f95b240

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

require 'json'
require 'rest-client'

module AleAir
  class FetchJSON
    attr_writer :secret_token
    attr_reader :status, :message, :time_measured, :location, :quality, :danger_level

    def initialize(token = nil)
      @secret_token = token
    end

    def air_quality(city = nil)
      unless @secret_token.nil?
        document = JSON.parse(RestClient.get 'https://api.waqi.info/search/?keyword=' + cleaned(city) + '&token=' + @secret_token)
        get_info(document)
      end 
    end

    private
    def cleaned(chars = "")
      URI.encode(chars.downcase.strip)
    end

    def get_info(document = nil)
      @status = "error"
      unless document.nil?
        if document["status"] == "ok"
	  if document["data"].length > 0
            document["data"].each do |air|
	      if !air["aqi"].nil? || !air["aqi"].empty?
		unless is_int(air["aqi"])
	          @message = "Invalid Airquality Value"
		  return false
	        end	  
		@status = "ok"
		@message = "Air Quality"
		@quality = air["aqi"]
		@time_measured = air["time"]["stime"] + ' ' + air["time"]["tz"]
		@location = air["station"]["name"]
		@danger_level = danger_lev(air["aqi"].to_i)
	        return true
	      else
		@message = "No Data Available"
              end
            end
	  else
	    @message = "No Stations Found"
	  end
	else
          
	  if !document["data"].nil?
	    @message = document["data"]
	  else
            @message = "Unknown Error"
	    @message = document
          end
	end	  
      end
      return false
    end

    def danger_lev(aqi=0)
      case aqi
      when 0..50
        "Good"
      when 51..100
        "Moderate"
      when 101..150
        "Unhealthy for Sensitive Groups"
      when 151..200
	"Unhealthy"
      when 201..300
        "Very Unhealthy"
      else
	"Hazardous"
      end
    end

    def is_int(string)
      Integer(string).is_a?(Integer)
    rescue ArgumentError, TypeError
      false
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ale_air-0.0.3 lib/ale_air/fetch_json.rb