class SmartlingApi::Clients::Smartling
Client used to contact smartling api
Constants
- SMARTLING_API
Public Instance Methods
Authentication
request used to contact Smartling
API. Need to pass Smartling
id and secret with the request. This is the only request that does not use a token in request.
@param url [String] Path corresponding to API get request @param body [Hash] Request body to pass with request @return [Hash] Data returned from request @raise [Errors::Client] If response does not return a 2xx or 3xx
# File lib/smartling_api/clients/smartling.rb, line 43 def authenticate(url:, body:) response = connection.post(url, body, {}) response.body.fetch("response", {}).fetch("data") end
Download a file from smartling for the given url
@param url [String] Path corresponding to API get request @param token [String] OAuth2 token used for accessing endpoint @param params [Hash] Request params to pass with request @return [Hash] Response returned from request @raise [Errors::Client] If response does not return a 2xx or 3xx
# File lib/smartling_api/clients/smartling.rb, line 69 def download(url:, token:, params:) response = connection.get(url, params, header(token)) response.body end
Get request used to contact Smartling
API
@param url [String] Path corresponding to API get request @param token [String] OAuth2 token used for accessing endpoint @param params [Hash] Additional parameters to pass with request @return [Hash] Data returned from request @raise [Errors::Client] If response does not return a 2xx or 3xx
# File lib/smartling_api/clients/smartling.rb, line 18 def get(url:, token:, params: {}) response = connection.get(url, params, header(token)) response.body.fetch("response", {}).fetch("data") end
Post request used to contact Smartling
API
@param url [String] Path corresponding to API get request @param token [String] OAuth2 token used for accessing endpoint @param body [Hash] Request body to pass with request @return [Hash] Response returned from request @raise [Errors::Client] If response does not return a 2xx or 3xx
# File lib/smartling_api/clients/smartling.rb, line 30 def post(url:, token:, body:) response = connection.post(url, body, header(token)) response.body.fetch("response", {}) end
Upload a file using a multipart request
@param url [String] Path corresponding to API get request @param token [String] OAuth2 token used for accessing endpoint @param body [Hash] Request body to pass with request @return [Hash] Response returned from request @raise [Errors::Client] If response does not return a 2xx or 3xx
# File lib/smartling_api/clients/smartling.rb, line 55 def upload(url:, token:, body:) response = multipart_connection.post(url, body, header(token)).body return response if response.is_a?(String) response.fetch('response') end
Private Instance Methods
# File lib/smartling_api/clients/smartling.rb, line 80 def connection Faraday.new(url: SMARTLING_API) do |faraday| faraday.request :json faraday.response :json, content_type: /\bjson$/ faraday.adapter :net_http faraday.use Errors::RaiseError end end
# File lib/smartling_api/clients/smartling.rb, line 76 def header(token) { 'Authorization' => "Bearer #{token}" } end
# File lib/smartling_api/clients/smartling.rb, line 91 def multipart_connection Faraday.new(url: SMARTLING_API) do |faraday| faraday.request :multipart faraday.request :url_encoded faraday.response :json, content_type: /\bjson$/ faraday.adapter :net_http faraday.use Errors::RaiseError end end