Class: Buble::HTTP

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

Defined Under Namespace

Classes: FilePart

Constant Summary collapse

DEFAULT_BASE_URL =
'https://buble.ai'
DEFAULT_TIMEOUT =
60

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, headers: {}) ⇒ HTTP

Returns a new instance of HTTP.

Raises:



20
21
22
23
24
25
26
27
# File 'lib/buble/http.rb', line 20

def initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, headers: {})
  raise Error, 'Missing Buble API key. Pass api_key or set BUBLE_API_KEY.' if blank?(api_key)

  @api_key = api_key
  @base_url = base_url.to_s.sub(%r{/+\z}, '')
  @timeout = timeout
  @headers = stringify_hash(headers)
end

Class Method Details

.encode_model_path(value) ⇒ Object



80
81
82
# File 'lib/buble/http.rb', line 80

def self.encode_model_path(value)
  value.to_s.split('/').map { |segment| encode_segment(segment) }.join('/')
end

.encode_segment(value) ⇒ Object



76
77
78
# File 'lib/buble/http.rb', line 76

def self.encode_segment(value)
  CGI.escape(value.to_s).gsub('+', '%20')
end

Instance Method Details

#multipart(path, fields:, file:, query: nil, headers: nil, timeout: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/buble/http.rb', line 34

def multipart(path, fields:, file:, query: nil, headers: nil, timeout: nil)
  part = coerce_file_part(file)
  request = Net::HTTP::Post.new(resolve(path, query))
  request_headers(headers).each { |name, value| request[name] = value }
  request['Accept'] = 'application/json'

  form = stringify_hash(fields).reject { |_key, value| value.nil? || value == '' }.map do |key, value|
    [key, value.to_s]
  end
  form << ['file', part.io, { filename: part.filename, content_type: part.content_type }]
  request.set_form(form, 'multipart/form-data')

  response = start_http(request.uri, timeout || @timeout) { |http| http.request(request) }
  decode_response(response)
ensure
  part&.close
end

#request(method, path, query: nil, body: nil, headers: nil, timeout: nil) ⇒ Object



29
30
31
32
# File 'lib/buble/http.rb', line 29

def request(method, path, query: nil, body: nil, headers: nil, timeout: nil)
  response = perform(method, path, query: query, body: body, headers: headers, timeout: timeout)
  decode_response(response)
end

#stream_lines(method, path, query: nil, body: nil, headers: nil, timeout: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/buble/http.rb', line 52

def stream_lines(method, path, query: nil, body: nil, headers: nil, timeout: nil)
  Enumerator.new do |yielder|
    request = build_request(method, path, query: query, body: body, headers: {
      'Accept' => 'text/event-stream'
    }.merge(stringify_hash(headers || {})))

    buffer = +''
    start_http(request.uri, timeout || @timeout) do |http|
      http.request(request) do |response|
        raise api_error(response) unless success?(response)

        response.read_body do |chunk|
          buffer << chunk
          while (index = buffer.index("\n"))
            line = buffer.slice!(0..index)
            yielder << line.chomp
          end
        end
      end
    end
    yielder << buffer unless buffer.empty?
  end
end