Sha256: d7f37a337501b93db52d513b2ca3c664fdd6e1aba2d88fb0ea2cde9bb8d56050

Contents?: true

Size: 969 Bytes

Versions: 2

Compression:

Stored size: 969 Bytes

Contents

require 'httparty'
require 'active_support/core_ext/class/attribute'
require 'active_support/json/decoding'
require 'active_support/core_ext/object/to_query'
require 'active_support/concern'
module ChinaAqi
  module Utility
    extend ::ActiveSupport::Concern

    included do
      include HTTParty
      attr_accessor :parmas
      class_attribute :base_uri, :method
      self.base_uri = URI::HTTP.build({host: 'www.pm25.in'})
    end

    module ClassMethods
      def get(*args)
        self.new(*args).get
      end
    end

    def get
      ::ActiveSupport::JSON.decode(HTTParty.get(url).body)
    end

    def uri
      raise NotImplementedError, "Please set value for 'method' class attributes in '#{self.class.name}' class." unless self.class.method
      self.class.base_uri.path = "/api/querys/#{self.class.method.to_s}.json"
      self.class.base_uri.query = @parmas.to_query
      self.class.base_uri
    end

    def url
      uri.to_s
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
china_aqi-0.0.3 lib/china_aqi/utility.rb
china_aqi-0.0.2 lib/china_aqi/utility.rb