Add Utils::JSON to wrap the JSON implementation

This commit is contained in:
Jack Nagel 2013-06-22 16:51:08 -05:00
parent c4272a25cc
commit 083b3c84d0
8 changed files with 41 additions and 21 deletions

View File

@ -1,7 +1,7 @@
require 'vendor/multi_json'
require 'utils/json'
GitHub.open "https://api.github.com/legacy/repos/search/homebrew" do |f|
MultiJson.decode(f.read)["repositories"].each do |repo|
Utils::JSON.load(f.read)["repositories"].each do |repo|
if repo['name'] =~ /^homebrew-(\S+)$/
puts tap = if repo['username'] == "Homebrew"
"homebrew/#{$1}"

View File

@ -3,6 +3,7 @@ require 'tab'
require 'keg'
require 'caveats'
require 'blacklist'
require 'utils/json'
module Homebrew extend self
def info
@ -46,14 +47,12 @@ module Homebrew extend self
end
def print_json
require 'vendor/multi_json'
formulae = ARGV.include?("--all") ? Formula : ARGV.formulae
json = formulae.map {|f| f.to_hash}
if json.size == 1
puts MultiJson.encode json.pop
puts Utils::JSON.dump(json.pop)
else
puts MultiJson.encode json
puts Utils::JSON.dump(json)
end
end

View File

@ -1,7 +1,7 @@
require 'formula'
require 'blacklist'
require 'utils'
require 'vendor/multi_json'
require 'utils/json'
module Homebrew extend self
def search
@ -73,7 +73,7 @@ module Homebrew extend self
results = []
GitHub.open "https://api.github.com/repos/#{user}/homebrew-#{repo}/git/trees/HEAD?recursive=1" do |f|
user.downcase! if user == "Homebrew" # special handling for the Homebrew organization
MultiJson.decode(f.read)["tree"].map{ |hash| hash['path'] }.compact.each do |file|
Utils::JSON.load(f.read)["tree"].map{ |hash| hash['path'] }.compact.each do |file|
name = File.basename(file, '.rb')
if file =~ /\.rb$/ and name =~ rx
results << "#{user}/#{repo}/#{name}"

View File

@ -1,5 +1,5 @@
require 'open-uri'
require 'vendor/multi_json'
require 'utils/json'
class AbstractDownloadStrategy
attr_accessor :local_bottle_path
@ -172,12 +172,12 @@ end
# Detect and download from Apache Mirror
class CurlApacheMirrorDownloadStrategy < CurlDownloadStrategy
def _fetch
mirrors = MultiJson.decode(open("#{@url}&asjson=1").read)
mirrors = Utils::JSON.load(open("#{@url}&asjson=1").read)
url = mirrors.fetch('preferred') + mirrors.fetch('path_info')
ohai "Best Mirror #{url}"
curl url, '-C', downloaded_size, '-o', @temporary_path
rescue IndexError, MultiJson::DecodeError
rescue IndexError, Utils::JSON::Error
raise "Couldn't determine mirror. Try again later."
end
end

View File

@ -1,6 +1,6 @@
require 'ostruct'
require 'options'
require 'vendor/multi_json'
require 'utils/json'
# Inherit from OpenStruct to gain a generic initialization method that takes a
# hash and creates an attribute for each key and value. `Tab.new` probably
@ -27,7 +27,7 @@ class Tab < OpenStruct
end
def self.from_file path
tab = Tab.new MultiJson.decode(open(path).read)
tab = Tab.new Utils::JSON.load(open(path).read)
tab.tabfile = path
tab
end
@ -90,7 +90,7 @@ class Tab < OpenStruct
end
def to_json
MultiJson.encode({
Utils::JSON.dump({
:used_options => used_options.to_a,
:unused_options => unused_options.to_a,
:built_as_bottle => built_as_bottle,

View File

@ -1,16 +1,16 @@
require 'testing_env'
require 'vendor/multi_json'
require 'utils/json'
class JsonSmokeTest < Test::Unit::TestCase
def test_encode
hash = { "foo" => ["bar", "baz"] }
json = %q|{"foo":["bar","baz"]}|
assert_equal json, MultiJson.encode(hash)
assert_equal json, Utils::JSON.dump(hash)
end
def test_decode
hash = { "foo" => ["bar", "baz"], "qux" => 1 }
json = %q|{"foo":["bar","baz"],"qux":1}|
assert_equal hash, MultiJson.decode(json)
assert_equal hash, Utils::JSON.load(json)
end
end

View File

@ -1,7 +1,7 @@
require 'pathname'
require 'exceptions'
require 'macos'
require 'vendor/multi_json'
require 'utils/json'
require 'open-uri'
class Tty
@ -265,7 +265,7 @@ module GitHub extend self
Kernel.open(url, default_headers.merge(headers), &block)
rescue OpenURI::HTTPError => e
if e.io.meta['x-ratelimit-remaining'].to_i <= 0
raise "GitHub #{MultiJson.decode(e.io.read)['message']}"
raise "GitHub #{Utils::JSON.load(e.io.read)['message']}"
else
raise e
end
@ -283,7 +283,7 @@ module GitHub extend self
uri = URI.parse("https://api.github.com/legacy/issues/search/mxcl/homebrew/open/#{name}")
GitHub.open uri do |f|
MultiJson.decode(f.read)['issues'].each do |issue|
Utils::JSON.load(f.read)['issues'].each do |issue|
# don't include issues that just refer to the tool in their body
issues << issue['html_url'] if issue['title'].include? name
end
@ -297,7 +297,7 @@ module GitHub extend self
uri = URI.parse("https://api.github.com/legacy/issues/search/mxcl/homebrew/open/#{query}")
GitHub.open uri do |f|
MultiJson.decode(f.read)['issues'].each do |pull|
Utils::JSON.load(f.read)['issues'].each do |pull|
yield pull['pull_request_url'] if rx.match pull['title'] and pull['pull_request_url']
end
end

View File

@ -0,0 +1,21 @@
require 'vendor/multi_json'
module Utils
module JSON
extend self
Error = Class.new(StandardError)
def load(str)
MultiJson.load(str)
rescue MultiJson::DecodeError => e
raise Error, e.message
end
def dump(obj)
MultiJson.dump(obj)
rescue MultiJson::EncodeError => e
raise Error, e.message
end
end
end