class Smartling::Api
Constants
- EXPIRATION_OFFSET
Attributes
baseUrl[RW]
prefix[RW]
Public Class Methods
new(args = {})
click to toggle source
# File lib/smartling/api.rb, line 30 def initialize(args = {}) @userId = args[:userId] @userSecret = args[:userSecret] @baseUrl = args[:baseUrl] || Endpoints::CURRENT end
Public Instance Methods
call(uri, method, auth, upload, download, params = nil)
click to toggle source
# File lib/smartling/api.rb, line 84 def call(uri, method, auth, upload, download, params = nil) headers = {} headers[:Authorization] = token_header() if auth headers[:content_type] = :json unless upload headers[:accept] = :json unless download RestClient::Request.execute(:method => method, :url => uri.to_s, :payload => params, :headers => headers) {|res, _, _| if download check_response(res) res.body else process(res) end } end
check_response(res)
click to toggle source
# File lib/smartling/api.rb, line 45 def check_response(res) return if res.code == 200 format_api_error(res) raise 'API_ERROR' end
format_api_error(res)
click to toggle source
# File lib/smartling/api.rb, line 67 def format_api_error(res) begin body = MultiJson.load(res.body) rescue end if body && body['response'] body = body['response'] STDERR.puts "\e[31m#{body['code']}\e[0m" if body['errors'] body['errors'].each do |e| STDERR.puts "\e[31m#{e['message']}\e[0m" end end end end
get(uri)
click to toggle source
# File lib/smartling/api.rb, line 102 def get(uri) call(uri, :get, true, false, false) end
get_raw(uri)
click to toggle source
# File lib/smartling/api.rb, line 105 def get_raw(uri) call(uri, :get, true, false, true) end
log=(v)
click to toggle source
# File lib/smartling/api.rb, line 118 def log=(v) RestClient.log = v end
post(uri, params = nil)
click to toggle source
# File lib/smartling/api.rb, line 108 def post(uri, params = nil) call(uri, :post, true, false, false, MultiJson.dump(params)) end
post_file(uri, params = nil)
click to toggle source
# File lib/smartling/api.rb, line 111 def post_file(uri, params = nil) call(uri, :post, true, true, false, params) end
post_file_raw(uri, params = nil)
click to toggle source
# File lib/smartling/api.rb, line 114 def post_file_raw(uri, params = nil) call(uri, :post, true, true, true, params) end
process(res)
click to toggle source
# File lib/smartling/api.rb, line 51 def process(res) check_response(res) body = MultiJson.load(res.body) if body['response'] body = body['response'] if body['code'] == 'SUCCESS' return body['data'] else format_api_error(res) raise 'API_ERROR' end end raise 'API_ERROR' return nil end
process_auth(response)
click to toggle source
# File lib/smartling/api.rb, line 133 def process_auth(response) now = Time.new.to_i @token = response['accessToken'] @token_expiration = now + response['expiresIn'].to_i - EXPIRATION_OFFSET @refresh = response['refreshToken'] @refresh_expiration = now + response['refreshExpiresIn'].to_i - EXPIRATION_OFFSET end
proxy=(v)
click to toggle source
# File lib/smartling/api.rb, line 121 def proxy=(v) RestClient.proxy = v end
refresh()
click to toggle source
Refresh Authentication - /auth-api/v2/authenticate/refresh (POST)
# File lib/smartling/api.rb, line 162 def refresh() uri = uri('auth-api/v2/authenticate/refresh', {}, {}) RestClient.post(uri.to_s, MultiJson.dump({:refreshToken => @refresh}), {:content_type => :json, :accept => :json}) {|res, _, _| process_auth(process(res)) return @token } end
token()
click to toggle source
Authenticate - /auth-api/v2/authenticate (POST)
# File lib/smartling/api.rb, line 142 def token() # Check if current token is still valid if @token now = Time.new.to_i if @token_expiration > now return @token elsif @refresh && @refresh_expiration > now return refresh() end end # Otherwise call authenticate endpoint uri = uri('auth-api/v2/authenticate', {}, {}) RestClient.post(uri.to_s, MultiJson.dump({:userIdentifier => @userId, :userSecret => @userSecret}), {:content_type => :json, :accept => :json}) {|res, _, _| process_auth(process(res)) return @token } end
token_header()
click to toggle source
auth
# File lib/smartling/api.rb, line 127 def token_header() t = token() raise 'AUTH_ERROR' if t.nil? return "Bearer #{t}" end
uri(path, params1 = nil, params2 = nil)
click to toggle source
# File lib/smartling/api.rb, line 36 def uri(path, params1 = nil, params2 = nil) uri = Uri.new(@baseUrl, path) params = {} params.merge!(params1) if params1 params.merge!(params2) if params2 uri.params = params return uri end