Module: Trav3::POST

Defined in:
lib/trav3/post.rb

Class Method Summary collapse

Class Method Details

.call(travis, url, **fields) ⇒ Object

rubocop:disable Metrics/MethodLength


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/trav3/post.rb', line 10

def self.call(travis, url, **fields)
  uri = URI( url.sub(/\?.*$/, '') )
  req = Net::HTTP::Post.new(uri.request_uri)
  travis.headers.each_pair do |header, value|
    req[header] = value
  end
  body = fields.delete(:body)
  req.body = body if body
  req.set_form_data(**fields) unless fields.empty?
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == 'https')
  response = http.request(req)

  if [Net::HTTPAccepted, Net::HTTPOK].include? response.code_type
    Success.new(travis, response)
  else
    RequestError.new(travis, response)
  end
end