gist-logs: extract methods
This commit is contained in:
parent
793b58a686
commit
08c581d5e7
@ -62,7 +62,7 @@ def http
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def post path, data
|
def make_request(path, data)
|
||||||
request = Net::HTTP::Post.new(path)
|
request = Net::HTTP::Post.new(path)
|
||||||
request['User-Agent'] = HOMEBREW_USER_AGENT
|
request['User-Agent'] = HOMEBREW_USER_AGENT
|
||||||
request['Content-Type'] = 'application/json'
|
request['Content-Type'] = 'application/json'
|
||||||
@ -70,18 +70,28 @@ def post path, data
|
|||||||
request['Authorization'] = "token #{HOMEBREW_GITHUB_API_TOKEN}"
|
request['Authorization'] = "token #{HOMEBREW_GITHUB_API_TOKEN}"
|
||||||
end
|
end
|
||||||
request.body = Utils::JSON.dump(data)
|
request.body = Utils::JSON.dump(data)
|
||||||
response = http.request(request)
|
request
|
||||||
raise "HTTP #{response.code} #{response.message}" if response.code != "201"
|
|
||||||
|
|
||||||
if !response.body.respond_to?(:force_encoding)
|
|
||||||
body = response.body
|
|
||||||
elsif response["Content-Type"].downcase == "application/json; charset=utf-8"
|
|
||||||
body = response.body.dup.force_encoding(Encoding::UTF_8)
|
|
||||||
else
|
|
||||||
body = response.body.encode(Encoding::UTF_8, :undef => :replace)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Utils::JSON.load(body)
|
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)
|
||||||
|
response.body
|
||||||
|
elsif response["Content-Type"].downcase == "application/json; charset=utf-8"
|
||||||
|
response.body.dup.force_encoding(Encoding::UTF_8)
|
||||||
|
else
|
||||||
|
response.body.encode(Encoding::UTF_8, :undef => :replace)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if ARGV.formulae.length != 1
|
if ARGV.formulae.length != 1
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user