Add Utils::JSON to wrap the JSON implementation
This commit is contained in:
parent
c4272a25cc
commit
083b3c84d0
@ -1,7 +1,7 @@
|
|||||||
require 'vendor/multi_json'
|
require 'utils/json'
|
||||||
|
|
||||||
GitHub.open "https://api.github.com/legacy/repos/search/homebrew" do |f|
|
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+)$/
|
if repo['name'] =~ /^homebrew-(\S+)$/
|
||||||
puts tap = if repo['username'] == "Homebrew"
|
puts tap = if repo['username'] == "Homebrew"
|
||||||
"homebrew/#{$1}"
|
"homebrew/#{$1}"
|
||||||
|
@ -3,6 +3,7 @@ require 'tab'
|
|||||||
require 'keg'
|
require 'keg'
|
||||||
require 'caveats'
|
require 'caveats'
|
||||||
require 'blacklist'
|
require 'blacklist'
|
||||||
|
require 'utils/json'
|
||||||
|
|
||||||
module Homebrew extend self
|
module Homebrew extend self
|
||||||
def info
|
def info
|
||||||
@ -46,14 +47,12 @@ module Homebrew extend self
|
|||||||
end
|
end
|
||||||
|
|
||||||
def print_json
|
def print_json
|
||||||
require 'vendor/multi_json'
|
|
||||||
|
|
||||||
formulae = ARGV.include?("--all") ? Formula : ARGV.formulae
|
formulae = ARGV.include?("--all") ? Formula : ARGV.formulae
|
||||||
json = formulae.map {|f| f.to_hash}
|
json = formulae.map {|f| f.to_hash}
|
||||||
if json.size == 1
|
if json.size == 1
|
||||||
puts MultiJson.encode json.pop
|
puts Utils::JSON.dump(json.pop)
|
||||||
else
|
else
|
||||||
puts MultiJson.encode json
|
puts Utils::JSON.dump(json)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
require 'formula'
|
require 'formula'
|
||||||
require 'blacklist'
|
require 'blacklist'
|
||||||
require 'utils'
|
require 'utils'
|
||||||
require 'vendor/multi_json'
|
require 'utils/json'
|
||||||
|
|
||||||
module Homebrew extend self
|
module Homebrew extend self
|
||||||
def search
|
def search
|
||||||
@ -73,7 +73,7 @@ module Homebrew extend self
|
|||||||
results = []
|
results = []
|
||||||
GitHub.open "https://api.github.com/repos/#{user}/homebrew-#{repo}/git/trees/HEAD?recursive=1" do |f|
|
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
|
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')
|
name = File.basename(file, '.rb')
|
||||||
if file =~ /\.rb$/ and name =~ rx
|
if file =~ /\.rb$/ and name =~ rx
|
||||||
results << "#{user}/#{repo}/#{name}"
|
results << "#{user}/#{repo}/#{name}"
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
require 'open-uri'
|
require 'open-uri'
|
||||||
require 'vendor/multi_json'
|
require 'utils/json'
|
||||||
|
|
||||||
class AbstractDownloadStrategy
|
class AbstractDownloadStrategy
|
||||||
attr_accessor :local_bottle_path
|
attr_accessor :local_bottle_path
|
||||||
@ -172,12 +172,12 @@ end
|
|||||||
# Detect and download from Apache Mirror
|
# Detect and download from Apache Mirror
|
||||||
class CurlApacheMirrorDownloadStrategy < CurlDownloadStrategy
|
class CurlApacheMirrorDownloadStrategy < CurlDownloadStrategy
|
||||||
def _fetch
|
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')
|
url = mirrors.fetch('preferred') + mirrors.fetch('path_info')
|
||||||
|
|
||||||
ohai "Best Mirror #{url}"
|
ohai "Best Mirror #{url}"
|
||||||
curl url, '-C', downloaded_size, '-o', @temporary_path
|
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."
|
raise "Couldn't determine mirror. Try again later."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
require 'ostruct'
|
require 'ostruct'
|
||||||
require 'options'
|
require 'options'
|
||||||
require 'vendor/multi_json'
|
require 'utils/json'
|
||||||
|
|
||||||
# Inherit from OpenStruct to gain a generic initialization method that takes a
|
# 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
|
# hash and creates an attribute for each key and value. `Tab.new` probably
|
||||||
@ -27,7 +27,7 @@ class Tab < OpenStruct
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.from_file path
|
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.tabfile = path
|
||||||
tab
|
tab
|
||||||
end
|
end
|
||||||
@ -90,7 +90,7 @@ class Tab < OpenStruct
|
|||||||
end
|
end
|
||||||
|
|
||||||
def to_json
|
def to_json
|
||||||
MultiJson.encode({
|
Utils::JSON.dump({
|
||||||
:used_options => used_options.to_a,
|
:used_options => used_options.to_a,
|
||||||
:unused_options => unused_options.to_a,
|
:unused_options => unused_options.to_a,
|
||||||
:built_as_bottle => built_as_bottle,
|
:built_as_bottle => built_as_bottle,
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
require 'testing_env'
|
require 'testing_env'
|
||||||
require 'vendor/multi_json'
|
require 'utils/json'
|
||||||
|
|
||||||
class JsonSmokeTest < Test::Unit::TestCase
|
class JsonSmokeTest < Test::Unit::TestCase
|
||||||
def test_encode
|
def test_encode
|
||||||
hash = { "foo" => ["bar", "baz"] }
|
hash = { "foo" => ["bar", "baz"] }
|
||||||
json = %q|{"foo":["bar","baz"]}|
|
json = %q|{"foo":["bar","baz"]}|
|
||||||
assert_equal json, MultiJson.encode(hash)
|
assert_equal json, Utils::JSON.dump(hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_decode
|
def test_decode
|
||||||
hash = { "foo" => ["bar", "baz"], "qux" => 1 }
|
hash = { "foo" => ["bar", "baz"], "qux" => 1 }
|
||||||
json = %q|{"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
|
||||||
end
|
end
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
require 'pathname'
|
require 'pathname'
|
||||||
require 'exceptions'
|
require 'exceptions'
|
||||||
require 'macos'
|
require 'macos'
|
||||||
require 'vendor/multi_json'
|
require 'utils/json'
|
||||||
require 'open-uri'
|
require 'open-uri'
|
||||||
|
|
||||||
class Tty
|
class Tty
|
||||||
@ -265,7 +265,7 @@ module GitHub extend self
|
|||||||
Kernel.open(url, default_headers.merge(headers), &block)
|
Kernel.open(url, default_headers.merge(headers), &block)
|
||||||
rescue OpenURI::HTTPError => e
|
rescue OpenURI::HTTPError => e
|
||||||
if e.io.meta['x-ratelimit-remaining'].to_i <= 0
|
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
|
else
|
||||||
raise e
|
raise e
|
||||||
end
|
end
|
||||||
@ -283,7 +283,7 @@ module GitHub extend self
|
|||||||
uri = URI.parse("https://api.github.com/legacy/issues/search/mxcl/homebrew/open/#{name}")
|
uri = URI.parse("https://api.github.com/legacy/issues/search/mxcl/homebrew/open/#{name}")
|
||||||
|
|
||||||
GitHub.open uri do |f|
|
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
|
# don't include issues that just refer to the tool in their body
|
||||||
issues << issue['html_url'] if issue['title'].include? name
|
issues << issue['html_url'] if issue['title'].include? name
|
||||||
end
|
end
|
||||||
@ -297,7 +297,7 @@ module GitHub extend self
|
|||||||
uri = URI.parse("https://api.github.com/legacy/issues/search/mxcl/homebrew/open/#{query}")
|
uri = URI.parse("https://api.github.com/legacy/issues/search/mxcl/homebrew/open/#{query}")
|
||||||
|
|
||||||
GitHub.open uri do |f|
|
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']
|
yield pull['pull_request_url'] if rx.match pull['title'] and pull['pull_request_url']
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
21
Library/Homebrew/utils/json.rb
Normal file
21
Library/Homebrew/utils/json.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user