class SmartlingApi::Clients::Smartling

Client used to contact smartling api

Constants

SMARTLING_API

Public Instance Methods

authenticate(url:, body:) click to toggle source

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(url:, token:, params:) click to toggle source

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(url:, token:, params: {}) click to toggle source

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(url:, token:, body:) click to toggle source

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(url:, token:, body:) click to toggle source

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

connection() click to toggle source
# 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
header(token) click to toggle source
# File lib/smartling_api/clients/smartling.rb, line 76
def header(token)
  { 'Authorization' => "Bearer #{token}" }
end
multipart_connection() click to toggle source
# 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