Class: Smartling::Api
- Inherits:
-
Object
- Object
- Smartling::Api
- Defined in:
- lib/smartling/api.rb
Constant Summary collapse
- EXPIRATION_OFFSET =
60
Instance Attribute Summary collapse
-
#baseUrl ⇒ Object
Returns the value of attribute baseUrl.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
Instance Method Summary collapse
- #call(uri, method, auth, upload, download, params = nil) ⇒ Object
- #check_response(res) ⇒ Object
- #format_api_error(res) ⇒ Object
- #get(uri) ⇒ Object
- #get_raw(uri) ⇒ Object
-
#initialize(args = {}) ⇒ Api
constructor
A new instance of Api.
- #log=(v) ⇒ Object
- #post(uri, params = nil) ⇒ Object
- #post_file(uri, params = nil) ⇒ Object
- #post_file_raw(uri, params = nil) ⇒ Object
- #process(res) ⇒ Object
- #process_auth(response) ⇒ Object
- #proxy=(v) ⇒ Object
-
#refresh ⇒ Object
Refresh Authentication - /auth-api/v2/authenticate/refresh (POST).
-
#token ⇒ Object
Authenticate - /auth-api/v2/authenticate (POST).
-
#token_header ⇒ Object
auth.
- #uri(path, params1 = nil, params2 = nil) ⇒ Object
Constructor Details
Instance Attribute Details
#baseUrl ⇒ Object
Returns the value of attribute baseUrl.
28 29 30 |
# File 'lib/smartling/api.rb', line 28 def baseUrl @baseUrl end |
#prefix ⇒ Object
Returns the value of attribute prefix.
28 29 30 |
# File 'lib/smartling/api.rb', line 28 def prefix @prefix end |
Instance Method Details
#call(uri, method, auth, upload, download, params = nil) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# 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) ⇒ Object
45 46 47 48 49 |
# 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) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# 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) ⇒ Object
102 103 104 |
# File 'lib/smartling/api.rb', line 102 def get(uri) call(uri, :get, true, false, false) end |
#get_raw(uri) ⇒ Object
105 106 107 |
# File 'lib/smartling/api.rb', line 105 def get_raw(uri) call(uri, :get, true, false, true) end |
#log=(v) ⇒ Object
118 119 120 |
# File 'lib/smartling/api.rb', line 118 def log=(v) RestClient.log = v end |
#post(uri, params = nil) ⇒ Object
108 109 110 |
# 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) ⇒ Object
111 112 113 |
# 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) ⇒ Object
114 115 116 |
# File 'lib/smartling/api.rb', line 114 def post_file_raw(uri, params = nil) call(uri, :post, true, true, true, params) end |
#process(res) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# 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) ⇒ Object
133 134 135 136 137 138 139 |
# 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) ⇒ Object
121 122 123 |
# File 'lib/smartling/api.rb', line 121 def proxy=(v) RestClient.proxy = v end |
#refresh ⇒ Object
Refresh Authentication - /auth-api/v2/authenticate/refresh (POST)
162 163 164 165 166 167 168 |
# 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 ⇒ Object
Authenticate - /auth-api/v2/authenticate (POST)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# 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 ⇒ Object
auth
127 128 129 130 131 |
# 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) ⇒ Object
36 37 38 39 40 41 42 43 |
# 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 |