Sha256: 2b3700ec34cdac625ed6ec9c54a8088a7cd12e671c43bec2bda5f6c2a0cdb3b3

Contents?: true

Size: 1.43 KB

Versions: 4

Compression:

Stored size: 1.43 KB

Contents

module Pushover
  module Priority
    extend self

    # A sash for our receipts.
    Receipts = Bini::Sash.new options:{
      file:"#{Bini.cache_dir}/receipts.yaml", auto_load:true, auto_save:true
    }
    LEVELS = {
      low:-1,
      normal:0,
      high:1,
      emergency:2
    }

    def priority=(level)
      @priority = parse level
    end

    def parse(level)
      return level if level.class == Fixnum
      if level.class == String
        LEVELS.each { |k,v| return v if k.to_s.start_with? level.downcase }
      end

      return 0
    end

    # Pull one from cache, or fetch one if not available.
    def find_receipt(prefix)
      results = Receipts.select { |k,v| k =~ /^#{prefix}/ }
      return nil if results.empty?
      return results.first
    end

    def update_receipts
      updates = Receipts.select {|k,v| v["acknowledged"] == 0 && v["expired"] == 0}
      updates.keys.each do |key|
        process_receipt key
      end
    end

    def process_receipt(receipt)
      r = fetch_receipt(receipt)

      return nil if !r
      Receipts[receipt] = r.to_h

      return Receipts[receipt]
    end

    def is_emergency?(priority)
      return true if priority && Pushover::Priority.parse(priority) == LEVELS[:emergency]
      return false
    end

    private
    def fetch_receipt(receipt)
      HTTParty.get("https://api.pushover.net/1/receipts/#{receipt}.json",
        body:{token:Pushover::App.current_app})
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pushover-1.0.3 lib/pushover/priority.rb
pushover-1.0.2 lib/pushover/priority.rb
pushover-1.0.1 lib/pushover/priority.rb
pushover-1.0.0 lib/pushover/priority.rb