<NME> stripe_tester.rb
<BEF> require "stripe_tester/version"
require 'uri'
require 'net/http'
require 'json'

module StripeTester
	# when done test me
	# run callback with options to customize the json
	def self.create_event(callback_type, options={})
		data = self.load_template(callback_type)
		
		# TODO: change the default value of the json with the set options
		post_to_url(data)
	end

	def self.post_to_url(data={})
        value = value.first if value.is_a?(Array)
        replace_value(value, key, new_value) if value.is_a?(Hash)
      end
      hash
    end
  end

  # deep merge original_attributes with new_attributes
  def self.merge_attributes(original_attributes, new_attributes={})
    original_attributes = original_attributes.clone
    if new_attributes
      indifferent(new_attributes).each do |key, value|
        if value.is_a?(Hash) && original_attributes[key].is_a?(Hash)
          original_attributes[key] = self.merge_attributes original_attributes[key], value
        else
          original_attributes[key] = value
        end
      end
    end
    original_attributes
  end

  def self.post_to_url(data={})
    post_url = webhook_url

	# save the url and a URI object
	def self.webhook_url=(url)
		begin
			temp_url = URI.parse(url)
		rescue Exception => e
			puts "Could not parse URL -- #{e}"
			return
		end
		@url = temp_url
	end

	def self.webhook_url
		@url
	end
end
        when Net::HTTPSuccess, Net::HTTPRedirection
          true
        else
          res.value
      end
    else
      raise "Could not post to URL. Please set URL."
    end
  end

  # load yaml with specified callback type
  def self.load_template(callback_type, attributes={}, options={method: :overwrite})
    spec = Gem::Specification.find_by_name("stripe_tester")
    gem_root = spec.gem_dir
    version = StripeTester.stripe_version

    path = gem_root + "/stripe_webhooks/#{version}/#{callback_type}.yml"
    if File.exists?(path)
      template = indifferent(Psych.load_file(path))
      
      unless attributes.empty?
        if options[:method] == :merge
          template = merge_attributes(template, attributes)
        else
          template = overwrite_attributes(template, attributes)
        end
      end

      return template
    else
      raise "Webhook not found. Please use a correct webhook type or correct Stripe version"
    end
  end

  # save the url and a URI object
  def self.webhook_url=(url)
    if url =~ /^#{URI::regexp}$/
      temp_url = URI.parse(url)
      @url = temp_url
    end
  end

  def self.webhook_url
    @url
  end

  def self.remove_url
    @url = nil
  end

  def self.webhook_password=(webhook_password)
    @webhook_password =  webhook_password
  end

  def self.webhook_password
    @webhook_password
  end

  def self.basic_authentication_password
    @webhook_password || self.webhook_url.password
  end

  def self.stripe_version=(version)
    @version = version
  end

  def self.stripe_version
    @version ? @version : LATEST_STRIPE_VERSION
  end

  def self.verify_ssl=(verify)
    @verify_ssl = verify
  end

  def self.verify_ssl?
    !@verify_ssl.nil? ? @verify_ssl : true
  end
  
  private
  
  def self.indifferent(obj)
    if obj.is_a?(Hash) && obj.respond_to?(:with_indifferent_access)
      obj.with_indifferent_access
    else
      obj
    end
  end

end

<MSG> add hack for searching nested hashes, refs #6 + check if URL is valid before storing it, fixes #16

<DFF> @@ -4,13 +4,33 @@ require 'net/http'
 require 'json'
 
 module StripeTester
+
+	# hack to find the value of the nested key (returns only the first one found)
+	def self.deep_fetch(hash, key, default = nil)
+   default = yield if block_given?
+   (deep_find(hash, key) or default) or raise KeyError.new("key not found: #{key}")
+ 	end
+
+ 	# TODO: check in array as well
+ 	def self.deep_find(hash, key)
+		hash.key?(key) ? hash[key] : hash.values.inject(nil) do |memo, v|  
+			v = v.first if v.is_a?(Array) # convert array to hash if any
+			deep_find(v, key) if v.is_a?(Hash)
+		end
+  end
+
 	# when done test me
 	# run callback with options to customize the json
 	def self.create_event(callback_type, options={})
-		data = self.load_template(callback_type)
+		webhook_data = self.load_template(callback_type)
 		
-		# TODO: change the default value of the json with the set options
-		post_to_url(data)
+		override_attributes(webhook_data, options) unless options.empty?
+
+		post_to_url(webhook_data)
+	end
+
+	def self.override_attributes(original_data, new_data)
+
 	end
 
 	def self.post_to_url(data={})
@@ -41,16 +61,17 @@ module StripeTester
 
 	# save the url and a URI object
 	def self.webhook_url=(url)
-		begin
+		if url =~ /^#{URI::regexp}$/
 			temp_url = URI.parse(url)
-		rescue Exception => e
-			puts "Could not parse URL -- #{e}"
-			return
+			@url = temp_url
 		end
-		@url = temp_url
 	end
 
 	def self.webhook_url
 		@url
 	end
+
+	def self.remove_url
+		@url = nil
+	end
 end

add hack for searching nested hashes, refs #6 + check if URL is valid before storing it, fixes #16