Sha256: 149aa6588113aa7f9d61fa0494ad50e0bc5d0849e6e8d53b0bf6f0a64f37011f

Contents?: true

Size: 847 Bytes

Versions: 1

Compression:

Stored size: 847 Bytes

Contents

# frozen_string_literal: true

require 'net/http'
require 'uri'
require 'json'

module Trav3
  module POST
    # rubocop:disable Metrics/MethodLength
    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
    # rubocop:enable Metrics/MethodLength
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trav3-0.2.1 lib/trav3/post.rb