class Tolq::Api::Client
Client
handles and encapsulates API requests
Constants
- BASE_URL
Attributes
key[R]
secret[R]
Public Class Methods
new(key, secret)
click to toggle source
Creates a client to interface with the Tolq
API.
@param key [String] Your API key @param secret [String] Your API secret
# File lib/tolq-api/client.rb, line 14 def initialize(key, secret) @key = key @secret = secret end
Public Instance Methods
delete(path, data = nil)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/tolq-api/client.rb, line 52 def put(path, data = nil) response = do_request(:put, path, data) handle_response(response) end
translation_requests()
click to toggle source
Via this method you can make requests to the Tolq
API
@return [TranslationRequestApi] An interface for making api requests.
# File lib/tolq-api/client.rb, line 22 def translation_requests TranslationRequestApi.new(self) end
valid_signature?(signature, payload)
click to toggle source
Verifies a signature on a callback payload You can find the signature in the X-Tolq-Signature header
@param signature [String] A sha1 encoded HMAC signature including the 'sha1=' prefix @param payload [String] The body of the payload as a string
# File lib/tolq-api/client.rb, line 32 def valid_signature?(signature, payload) payload_signature = 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), self.key, payload) payload_signature == signature end
Private Instance Methods
base_url()
click to toggle source
# File lib/tolq-api/client.rb, line 59 def base_url # TODO remove interpolation # not needed with Net::HTTP BASE_URL % { key: key, secret: secret } end
do_request(method, path, data = nil)
click to toggle source
# File lib/tolq-api/client.rb, line 65 def do_request(method, path, data = nil) uri = URI.parse(base_url + path) https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = case method when :post Net::HTTP::Post.new(uri.path) when :get Net::HTTP::Get.new(uri.path) when :delete Net::HTTP::Delete.new(uri.path) end request.body = data.to_json if data request.basic_auth @key, @secret request['Content-Type'] = 'application/json' https.request(request) end
handle_response(response)
click to toggle source
# File lib/tolq-api/client.rb, line 87 def handle_response(response) Response.new(status: response.code.to_i, body: response.body) end