Class: Audd::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/audd/http_client.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

USER_AGENT =
"audd-ruby/#{VERSION} ruby/#{RUBY_VERSION} (#{RUBY_PLATFORM})"
FILE_FIELD =

AudD only accepts multipart uploads; the field name for the upload is fixed by the API.

"file"

Instance Method Summary collapse

Instance Method Details

#post_form(url, fields:, timeout:, file: nil) ⇒ Object

POSTs fields (and optionally one file) to url.

Parameters:

  • file (Array(String, IO), nil) (defaults to: nil)

    a [filename, io] pair



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/audd/http_client.rb', line 23

def post_form(url, fields:, timeout:, file: nil)
  uri = URI.parse(url)
  boundary = SecureRandom.hex(16)

  request = Net::HTTP::Post.new(uri)
  request["User-Agent"] = USER_AGENT
  request["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
  request.body = multipart_body(fields, file, boundary)

  perform(uri, request, timeout)
end