Skip to content

Instantly share code, notes, and snippets.

@andreacampi
Created September 28, 2012 21:31
Show Gist options
  • Save andreacampi/3802182 to your computer and use it in GitHub Desktop.
Save andreacampi/3802182 to your computer and use it in GitHub Desktop.
cookbooks/consume_json/libraries/consume_json.rb
class Chef
module Mixin
module ConsumeJson
def consume_json(url)
Chef::Log.debug "consume_json: requesting url: #{url}"
info = nil
fetch(url) do |data|
info = JSON.parse(data)
Chef::Log.debug "consume_json: parsed: #{info.inspect}"
yield(info) if block_given?
end
info
end
protected
def fetch(url)
Chef::REST.new(url, nil, nil, http_client_opts).fetch(url) do |raw_file|
data = File.read(raw_file.path)
yield(data)
end
end
def http_client_opts
{}
end
end
end
end
Chef::Recipe.send(:include, Chef::Mixin::ConsumeJson)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment