Module: BlueFactory::Net

Defined in:
lib/blue_factory/tasks/support.rb

Defined Under Namespace

Classes: ResponseError

Class Method Summary collapse

Class Method Details

.post_request(server, method, data, auth: nil, content_type: "application/json") ⇒ Object

Raises:



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/blue_factory/tasks/support.rb', line 8

def self.post_request(server, method, data, auth: nil, content_type: "application/json")
  headers = {}
  headers['Content-Type'] = content_type
  headers['Authorization'] = "Bearer #{auth}" if auth

  body = data.is_a?(String) ? data : data.to_json

  response = ::Net::HTTP.post(URI("#{server}/xrpc/#{method}"), body, headers)
  raise ResponseError, "Invalid response: #{response.code} #{response.body}" if response.code.to_i / 100 != 2

  JSON.parse(response.body)
end