Skip to content

Instantly share code, notes, and snippets.

@hedgehog
Forked from andreacampi/consume_json.rb
Created September 29, 2012 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hedgehog/3804054 to your computer and use it in GitHub Desktop.
Save hedgehog/3804054 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