Class: Toptranslation::Connection
- Inherits:
-
Object
- Object
- Toptranslation::Connection
- Defined in:
- lib/toptranslation/connection.rb
Constant Summary collapse
- API_VERSION =
'v2'.freeze
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #download(url, path, &block) ⇒ Object
- #get(path, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #patch(path, options = {}) ⇒ Object
- #post(path, options = {}) ⇒ Object
-
#sign_in!(_options = {}) ⇒ Object
deprecated
Deprecated.
Email/password sign-in was removed from the Toptranslation API (authentication moved to the auth service) — the endpoint this method used no longer exists. Initialize the client with a static access token instead.
-
#upload(filepath, _type = 'document', &block) ⇒ Object
Uploads go directly to the document store (castor): the response carries the document token that authorizes further use of the file.
Constructor Details
#initialize(options = {}) ⇒ Connection
Returns a new instance of Connection.
7 8 9 10 11 12 13 |
# File 'lib/toptranslation/connection.rb', line 7 def initialize( = {}) @base_url = [:base_url] || 'https://api.toptranslation.com' @files_url = [:files_url] || 'https://files.toptranslation.com' @access_token = [:access_token] @verbose = [:verbose] || false sign_in!() if @access_token.nil? && .values_at(:email, :password).all? end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
3 4 5 |
# File 'lib/toptranslation/connection.rb', line 3 def access_token @access_token end |
#verbose ⇒ Object
Returns the value of attribute verbose.
3 4 5 |
# File 'lib/toptranslation/connection.rb', line 3 def verbose @verbose end |
Instance Method Details
#download(url, path, &block) ⇒ Object
27 28 29 30 31 |
# File 'lib/toptranslation/connection.rb', line 27 def download(url, path, &block) puts "# downloading #{url}" if @verbose uri = URI.parse(url) download_uri(uri, path, &block) end |
#get(path, options = {}) ⇒ Object
15 16 17 |
# File 'lib/toptranslation/connection.rb', line 15 def get(path, = {}) transform_response(request(:get, path, )) end |
#patch(path, options = {}) ⇒ Object
23 24 25 |
# File 'lib/toptranslation/connection.rb', line 23 def patch(path, = {}) transform_response(request(:patch, path, )) end |
#post(path, options = {}) ⇒ Object
19 20 21 |
# File 'lib/toptranslation/connection.rb', line 19 def post(path, = {}) transform_response(request(:post, path, )) end |
#sign_in!(_options = {}) ⇒ Object
Email/password sign-in was removed from the Toptranslation API (authentication moved to the auth service) — the endpoint this method used no longer exists. Initialize the client with a static access token instead.
45 46 47 48 49 50 51 |
# File 'lib/toptranslation/connection.rb', line 45 def sign_in!( = {}) raise Toptranslation::SignInRemovedError, 'sign_in! with email/password is no longer supported: the Toptranslation API ' \ 'removed POST /v2/auth/sign_in. Initialize the client with a static access ' \ "token instead: Toptranslation.new(access_token: '...'). Contact " \ 'support@toptranslation.com to obtain a token.' end |
#upload(filepath, _type = 'document', &block) ⇒ Object
Uploads go directly to the document store (castor): the response carries the document token that authorizes further use of the file.
35 36 37 38 39 |
# File 'lib/toptranslation/connection.rb', line 35 def upload(filepath, _type = 'document', &block) uri = URI.parse("#{@files_url}/v2/documents") file = File.new(filepath) upload_file(file, uri, &block) end |