Class: Tolq::Api::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tolq-api/client.rb

Overview

Client handles and encapsulates API requests

Constant Summary collapse

BASE_URL =
'https://%{key}:%{secret}@api.tolq.com/v1'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, secret) ⇒ Client

Creates a client to interface with the Tolq API.

Parameters:

  • key (String)

    Your API key

  • secret (String)

    Your API secret



14
15
16
17
# File 'lib/tolq-api/client.rb', line 14

def initialize(key, secret)
  @key = key
  @secret = secret
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/tolq-api/client.rb', line 8

def key
  @key
end

#secretObject (readonly)

Returns the value of attribute secret.



8
9
10
# File 'lib/tolq-api/client.rb', line 8

def secret
  @secret
end

Instance Method Details

#delete(path, data = nil) ⇒ Object



47
48
49
50
# File 'lib/tolq-api/client.rb', line 47

def delete(path, data = nil)
  response = do_request(:delete, path, data)
  handle_response(response)
end

#get(path, data = nil) ⇒ Object



37
38
39
40
# File 'lib/tolq-api/client.rb', line 37

def get(path, data = nil)
  response = do_request(:get, path, data)
  handle_response(response)
end

#post(path, data = nil) ⇒ Object



42
43
44
45
# File 'lib/tolq-api/client.rb', line 42

def post(path, data = nil)
  response = do_request(:post, path, data)
  handle_response(response)
end

#put(path, data = nil) ⇒ Object



52
53
54
55
# File 'lib/tolq-api/client.rb', line 52

def put(path, data = nil)
  response = do_request(:put, path, data)
  handle_response(response)
end

#translation_requestsTranslationRequestApi

Via this method you can make requests to the Tolq API

Returns:



22
23
24
# File 'lib/tolq-api/client.rb', line 22

def translation_requests
  TranslationRequestApi.new(self)
end

#valid_signature?(signature, payload) ⇒ Boolean

Verifies a signature on a callback payload You can find the signature in the X-Tolq-Signature header

Parameters:

  • signature (String)

    A sha1 encoded HMAC signature including the ‘sha1=’ prefix

  • payload (String)

    The body of the payload as a string

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/tolq-api/client.rb', line 32

def valid_signature?(signature, payload)
  payload_signature = 'sha1=' + OpenSSL::HMAC.digest('sha1', self.key, payload)
  payload_signature == signature
end