aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Contributions
diff options
context:
space:
mode:
authorJack Nagel2014-09-07 17:33:51 -0500
committerJack Nagel2014-09-07 17:34:13 -0500
commit08c581d5e7f9128634d762e686347bcbbabcae5a (patch)
tree63d6cb32719e94dfb3828f67b105b78fef84ef77 /Library/Contributions
parent793b58a68618dc21f6143aaf708f93efb5ed17c6 (diff)
downloadbrew-08c581d5e7f9128634d762e686347bcbbabcae5a.tar.bz2
gist-logs: extract methods
Diffstat (limited to 'Library/Contributions')
-rwxr-xr-xLibrary/Contributions/cmd/brew-gist-logs.rb26
1 files changed, 18 insertions, 8 deletions
diff --git a/Library/Contributions/cmd/brew-gist-logs.rb b/Library/Contributions/cmd/brew-gist-logs.rb
index b2357ef60..8a3a02eab 100755
--- a/Library/Contributions/cmd/brew-gist-logs.rb
+++ b/Library/Contributions/cmd/brew-gist-logs.rb
@@ -62,7 +62,7 @@ def http
end
end
-def post path, data
+def make_request(path, data)
request = Net::HTTP::Post.new(path)
request['User-Agent'] = HOMEBREW_USER_AGENT
request['Content-Type'] = 'application/json'
@@ -70,18 +70,28 @@ def post path, data
request['Authorization'] = "token #{HOMEBREW_GITHUB_API_TOKEN}"
end
request.body = Utils::JSON.dump(data)
- response = http.request(request)
- raise "HTTP #{response.code} #{response.message}" if response.code != "201"
+ request
+end
+
+def post(path, data)
+ request = make_request(path, data)
+ case response = http.request(request)
+ when Net::HTTPCreated
+ Utils::JSON.load get_body(response)
+ else
+ raise "HTTP #{response.code} #{response.message} (expected 201)"
+ end
+end
+
+def get_body(response)
if !response.body.respond_to?(:force_encoding)
- body = response.body
+ response.body
elsif response["Content-Type"].downcase == "application/json; charset=utf-8"
- body = response.body.dup.force_encoding(Encoding::UTF_8)
+ response.body.dup.force_encoding(Encoding::UTF_8)
else
- body = response.body.encode(Encoding::UTF_8, :undef => :replace)
+ response.body.encode(Encoding::UTF_8, :undef => :replace)
end
-
- Utils::JSON.load(body)
end
if ARGV.formulae.length != 1