Class: Lara::Client
- Inherits:
-
Object
- Object
- Lara::Client
- 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
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Instance Method Summary collapse
-
#delete(path, params: nil, body: nil, headers: nil) ⇒ Hash, ...
Sends a DELETE request to the Lara API.
-
#get(path, params: nil, headers: nil) ⇒ Hash, ...
Sends a GET request to the Lara API.
-
#initialize(auth_method, base_url: DEFAULT_BASE_URL, connection_timeout: nil, read_timeout: nil) ⇒ Client
constructor
A new instance of Client.
-
#post(path, body: nil, files: nil, headers: nil, raw_response: false) {|Hash| ... } ⇒ Hash, ...
Sends a POST request to the Lara API.
-
#put(path, body: nil, files: nil, headers: nil) ⇒ Hash, ...
Sends a PUT request to the Lara API.
-
#set_extra_header(name, value) ⇒ Object
Sets an extra header.
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_url ⇒ Object (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.
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.
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.
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.
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
45 46 47 |
# File 'lib/lara/client.rb', line 45 def set_extra_header(name, value) @extra_headers[name] = value end |