aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Nagel2013-06-22 16:51:08 -0500
committerJack Nagel2013-06-22 21:34:02 -0500
commit163067512bb15e0582ad9d59555697126b9aeac6 (patch)
treef0128fb96018751be17281a59161fe3b72e8ad56
parente3f061d9900187e0082f17d4e857a55ff8fc4874 (diff)
downloadhomebrew-163067512bb15e0582ad9d59555697126b9aeac6.tar.bz2
Add Utils::JSON to wrap the JSON implementation
-rwxr-xr-xLibrary/Contributions/cmd/brew-ls-taps.rb4
-rw-r--r--Library/Formula/libvbucket.rb4
-rw-r--r--Library/Homebrew/cmd/info.rb7
-rw-r--r--Library/Homebrew/cmd/search.rb4
-rw-r--r--Library/Homebrew/download_strategy.rb6
-rw-r--r--Library/Homebrew/tab.rb6
-rw-r--r--Library/Homebrew/test/test_json.rb6
-rw-r--r--Library/Homebrew/utils.rb8
-rw-r--r--Library/Homebrew/utils/json.rb21
9 files changed, 43 insertions, 23 deletions
diff --git a/Library/Contributions/cmd/brew-ls-taps.rb b/Library/Contributions/cmd/brew-ls-taps.rb
index 98c14dccf..40cf0cafe 100755
--- a/Library/Contributions/cmd/brew-ls-taps.rb
+++ b/Library/Contributions/cmd/brew-ls-taps.rb
@@ -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}"
diff --git a/Library/Formula/libvbucket.rb b/Library/Formula/libvbucket.rb
index 272ce8d66..57a16f6d8 100644
--- a/Library/Formula/libvbucket.rb
+++ b/Library/Formula/libvbucket.rb
@@ -14,9 +14,9 @@ class Libvbucket < Formula
end
test do
- require 'multi_json'
+ require 'utils/json'
require 'open3'
- json = MultiJson.encode(
+ json = Utils::JSON.dump(
{
"hashAlgorithm" => "CRC",
"numReplicas" => 2,
diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb
index de98d692c..b5f133dfd 100644
--- a/Library/Homebrew/cmd/info.rb
+++ b/Library/Homebrew/cmd/info.rb
@@ -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
diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb
index 0ac76e435..2ec8afd81 100644
--- a/Library/Homebrew/cmd/search.rb
+++ b/Library/Homebrew/cmd/search.rb
@@ -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}"
diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb
index 8e3bd8139..c7d6aeefb 100644
--- a/Library/Homebrew/download_strategy.rb
+++ b/Library/Homebrew/download_strategy.rb
@@ -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
diff --git a/Library/Homebrew/tab.rb b/Library/Homebrew/tab.rb
index d35f69f8a..48f709113 100644
--- a/Library/Homebrew/tab.rb
+++ b/Library/Homebrew/tab.rb
@@ -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,
diff --git a/Library/Homebrew/test/test_json.rb b/Library/Homebrew/test/test_json.rb
index 6bece67c0..f77b7852d 100644
--- a/Library/Homebrew/test/test_json.rb
+++ b/Library/Homebrew/test/test_json.rb
@@ -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
diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb
index 60eead67c..f8b5466f2 100644
--- a/Library/Homebrew/utils.rb
+++ b/Library/Homebrew/utils.rb
@@ -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
diff --git a/Library/Homebrew/utils/json.rb b/Library/Homebrew/utils/json.rb
new file mode 100644
index 000000000..f52881c84
--- /dev/null
+++ b/Library/Homebrew/utils/json.rb
@@ -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