Created
September 28, 2012 21:31
-
-
Save andreacampi/3802182 to your computer and use it in GitHub Desktop.
cookbooks/consume_json/libraries/consume_json.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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