Class: GenerateImage::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/generate_image/http.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration:) ⇒ HTTP

Returns a new instance of HTTP.



11
12
13
# File 'lib/generate_image/http.rb', line 11

def initialize(configuration:)
  @configuration = configuration
end

Instance Method Details

#post_json(path, body_hash) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/generate_image/http.rb', line 15

def post_json(path, body_hash)
  uri = build_uri(path)
  body = JSON.generate(body_hash)

  with_retries do
    request = Net::HTTP::Post.new(uri)
    request["Authorization"] = "Bearer #{api_key}"
    request["Content-Type"] = "application/json"
    request.body = body
    perform(uri, request)
  end
end

#post_multipart(path, fields, files) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/generate_image/http.rb', line 28

def post_multipart(path, fields, files)
  uri = build_uri(path)
  boundary = "----RubyGenerateImage#{SecureRandom.hex(16)}"
  body = build_multipart_body(boundary, fields, files)

  with_retries do
    request = Net::HTTP::Post.new(uri)
    request["Authorization"] = "Bearer #{api_key}"
    request["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
    request.body = body
    perform(uri, request)
  end
end