Skip to content

Instantly share code, notes, and snippets.

@elijah
Forked from andreacampi/consume_json.rb
Created September 29, 2012 22:38

Revisions

  1. Andrea Campi created this gist Sep 28, 2012.
    33 changes: 33 additions & 0 deletions consume_json.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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)