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"
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if !response.body.respond_to?(:force_encoding)
 | 
					def post(path, data)
 | 
				
			||||||
    body = response.body
 | 
					  request = make_request(path, data)
 | 
				
			||||||
  elsif response["Content-Type"].downcase == "application/json; charset=utf-8"
 | 
					
 | 
				
			||||||
    body = response.body.dup.force_encoding(Encoding::UTF_8)
 | 
					  case response = http.request(request)
 | 
				
			||||||
 | 
					  when Net::HTTPCreated
 | 
				
			||||||
 | 
					    Utils::JSON.load get_body(response)
 | 
				
			||||||
  else
 | 
					  else
 | 
				
			||||||
    body = response.body.encode(Encoding::UTF_8, :undef => :replace)
 | 
					    raise "HTTP #{response.code} #{response.message} (expected 201)"
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Utils::JSON.load(body)
 | 
					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