<commit_before>require 'rubygems' if RUBY_VERSION < '1.9.0' && Puppet.features.rubygems?
require 'json' if Puppet.features.json?

Puppet::Type.type(:sensu_check).provide(:json) do
  confine :feature => :json

  def initialize(*args)
    super

    @conf = nil
  end

  def conf
    resource[:realname] = resource[:name] if resource[:realname] == nil
    begin
      @conf ||= JSON.parse(File.read("/etc/sensu/conf.d/check_#{resource[:realname]}.json"))
    rescue
      @conf ||= {}
    end
  end

  def flush
    File.open("/etc/sensu/conf.d/check_#{resource[:realname]}.json", 'w') do |f|
      f.puts JSON.pretty_generate(conf)
    end
  end

  def create
    conf['checks'] = {}
    conf['checks'][resource[:realname]] = {}
    self.handlers = resource[:handlers]
    self.command = resource[:command]
    self.interval = resource[:interval]
    self.subscribers = resource[:subscribers]
    # Optional arguments
    self.aggregate = resource[:aggregate] unless resource[:aggregate].nil?
    self.standalone = resource[:standalone] unless resource[:standalone].nil?
    self.high_flap_threshold = resource[:high_flap_threshold] unless resource[:high_flap_threshold].nil?
    self.low_flap_threshold = resource[:low_flap_threshold] unless resource[:low_flap_threshold].nil?
    self.occurrences = resource[:occurrences] unless resource[:occurrences].nil?
    self.refresh = resource[:refresh] unless resource[:refresh].nil?
    self.notification = resource[:notification] unless resource[:notification].nil?
  end

  def destroy
    conf = nil
  end

  def exists?
    conf.has_key?('checks') and conf['checks'].has_key?(resource[:realname])
  end

  def interval
    conf['checks'][resource[:realname]]['interval'].to_s
  end

  def interval=(value)
    conf['checks'][resource[:realname]]['interval'] = value.to_i
  end

  def handlers
    conf['checks'][resource[:realname]]['handlers'] || []
  end<commit_msg>ensure type is set first puppet run<commit_after>require 'rubygems' if RUBY_VERSION < '1.9.0' && Puppet.features.rubygems?
require 'json' if Puppet.features.json?

Puppet::Type.type(:sensu_check).provide(:json) do
  confine :feature => :json

  def initialize(*args)
    super

    @conf = nil
  end

  def conf
    resource[:realname] = resource[:name] if resource[:realname] == nil
    begin
      @conf ||= JSON.parse(File.read("/etc/sensu/conf.d/check_#{resource[:realname]}.json"))
    rescue
      @conf ||= {}
    end
  end

  def flush
    File.open("/etc/sensu/conf.d/check_#{resource[:realname]}.json", 'w') do |f|
      f.puts JSON.pretty_generate(conf)
    end
  end

  def create
    conf['checks'] = {}
    conf['checks'][resource[:realname]] = {}
    self.handlers = resource[:handlers]
    self.command = resource[:command]
    self.interval = resource[:interval]
    self.subscribers = resource[:subscribers]
    # Optional arguments
    self.type = resource[:type] unless resource[:type].nil?
    self.aggregate = resource[:aggregate] unless resource[:aggregate].nil?
    self.standalone = resource[:standalone] unless resource[:standalone].nil?
    self.high_flap_threshold = resource[:high_flap_threshold] unless resource[:high_flap_threshold].nil?
    self.low_flap_threshold = resource[:low_flap_threshold] unless resource[:low_flap_threshold].nil?
    self.occurrences = resource[:occurrences] unless resource[:occurrences].nil?
    self.refresh = resource[:refresh] unless resource[:refresh].nil?
    self.notification = resource[:notification] unless resource[:notification].nil?
  end

  def destroy
    conf = nil
  end

  def exists?
    conf.has_key?('checks') and conf['checks'].has_key?(resource[:realname])
  end

  def interval
    conf['checks'][resource[:realname]]['interval'].to_s
  end

  def interval=(value)
    conf['checks'][resource[:realname]]['interval'] = value.to_i
  end

  def handlers
    conf['checks'][resource[:realname]]['handlers'] || []
  end
{'fraction_non_alphanumeric': 0.1481859989780276, 'fraction_numerical': 0.0015329586101175269, 'mean_word_length': 4.276280323450135, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1068918', 'n_tokens_mistral': 1279, 'n_tokens_neox': 1227, 'n_words': 233}