lib/heroku/api.rb in heroku-api-0.2.6 vs lib/heroku/api.rb in heroku-api-0.2.7

- old
+ new

@@ -7,12 +7,10 @@ __LIB_DIR__ = File.expand_path(File.join(File.dirname(__FILE__), "..")) unless $LOAD_PATH.include?(__LIB_DIR__) $LOAD_PATH.unshift(__LIB_DIR__) end -require "heroku/api/vendor/okjson" - require "heroku/api/errors" require "heroku/api/mock" require "heroku/api/version" require "heroku/api/addons" @@ -32,10 +30,40 @@ srand module Heroku class API + begin + + require('json') + + def self.json_decode(json) + JSON.parse(json) + rescue + json + end + + def self.json_encode(object) + JSON.dump(object) + end + + rescue LoadError + + require('heroku/api/vendor/okjson') + + def self.json_decode(json) + Heroku::API::OkJson.decode(json) + rescue + json + end + + def self.json_encode(object) + Heroku::API::OkJson.encode(object) + end + + end + def initialize(options={}) @api_key = options.delete(:api_key) || ENV['HEROKU_API_KEY'] user_pass = ":#{@api_key}" options = { :headers => {}, @@ -79,12 +107,10 @@ if response.body && !response.body.empty? if response.headers['Content-Encoding'] == 'gzip' response.body = Zlib::GzipReader.new(StringIO.new(response.body)).read end begin - response.body = Heroku::API::OkJson.decode(response.body) - rescue - # leave non-JSON body as is + response.body = Heroku::API.json_decode(response.body) end end # reset (non-persistent) connection @connection.reset