Class: Lara::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/lara/client.rb

Overview

This class is used to interact with Lara via the REST API.

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.laratranslate.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_method, base_url: DEFAULT_BASE_URL, connection_timeout: nil, read_timeout: nil) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lara/client.rb', line 18

def initialize(auth_method, base_url: DEFAULT_BASE_URL, connection_timeout: nil,
               read_timeout: nil)
  case auth_method
  when Credentials
    @credentials = auth_method
    @auth_token = nil
  when AuthToken
    @credentials = nil
    @auth_token = auth_method
  else
    raise ArgumentError, "auth_method must be Credentials or AuthToken"
  end

  @base_url = base_url.to_s.sub(%r{/+$}, "")
  @connection_timeout = connection_timeout
  @read_timeout = read_timeout
  @extra_headers = {}
  @auth_mutex = Monitor.new

  @connection = build_connection
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



40
41
42
# File 'lib/lara/client.rb', line 40

def base_url
  @base_url
end

Instance Method Details

#delete(path, params: nil, body: nil, headers: nil) ⇒ Hash, ...

Sends a DELETE request to the Lara API.

Parameters:

  • path (String)

    The path to send the request to.

  • params (Hash, nil) (defaults to: nil)

    The parameters to send with the request.

  • headers (Hash, nil) (defaults to: nil)

    Additional headers to include in the request.

Returns:

  • (Hash, Array, String, nil)

    The JSON ‘content’ from the API or CSV body for csv responses.



85
86
87
# File 'lib/lara/client.rb', line 85

def delete(path, params: nil, body: nil, headers: nil)
  request(:delete, path, body: body, headers: headers, params: params)
end

#get(path, params: nil, headers: nil) ⇒ Hash, ...

Sends a GET request to the Lara API.

Parameters:

  • path (String)

    The path to send the request to.

  • params (Hash, nil) (defaults to: nil)

    The parameters to send with the request.

  • headers (Hash, nil) (defaults to: nil)

    Additional headers to include in the request.

Returns:

  • (Hash, Array, String, nil)

    The JSON ‘content’ from the API or CSV body for csv responses.



54
55
56
# File 'lib/lara/client.rb', line 54

def get(path, params: nil, headers: nil)
  request(:get, path, body: nil, headers: headers, params: params)
end

#post(path, body: nil, files: nil, headers: nil, raw_response: false) {|Hash| ... } ⇒ Hash, ...

Sends a POST request to the Lara API.

Parameters:

  • path (String)

    The path to send the request to.

  • body (Hash, nil) (defaults to: nil)

    The parameters to send with the request.

  • files (Hash, nil) (defaults to: nil)

    The files to send with the request. If present, request will be sent as multipart/form-data.

  • headers (Hash, nil) (defaults to: nil)

    Additional headers to include in the request.

  • raw_response (Boolean) (defaults to: false)

    If true, returns the raw response body (useful for binary data like images).

Yields:

  • (Hash)

    Each partial JSON result from the stream (if streaming)

Returns:

  • (Hash, Array, String, nil)

    The JSON ‘content’ from the API, CSV body, or raw bytes.



66
67
68
# File 'lib/lara/client.rb', line 66

def post(path, body: nil, files: nil, headers: nil, raw_response: false, &callback)
  request(:post, path, body: body, files: files, headers: headers, raw_response: raw_response, &callback)
end

#put(path, body: nil, files: nil, headers: nil) ⇒ Hash, ...

Sends a PUT request to the Lara API.

Parameters:

  • path (String)

    The path to send the request to.

  • body (Hash, nil) (defaults to: nil)

    The parameters to send with the request.

  • files (Hash, nil) (defaults to: nil)

    The files to send with the request. If present, request will be sent as multipart/form-data.

  • headers (Hash, nil) (defaults to: nil)

    Additional headers to include in the request.

Returns:

  • (Hash, Array, String, nil)

    The JSON ‘content’ from the API or CSV body for csv responses.



76
77
78
# File 'lib/lara/client.rb', line 76

def put(path, body: nil, files: nil, headers: nil)
  request(:put, path, body: body, files: files, headers: headers)
end

#set_extra_header(name, value) ⇒ Object

Sets an extra header

Parameters:

  • name (String)

    Header name

  • value (String)

    Header value



45
46
47
# File 'lib/lara/client.rb', line 45

def set_extra_header(name, value)
  @extra_headers[name] = value
end