Class: Toptranslation::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/toptranslation/connection.rb

Constant Summary collapse

API_VERSION =
'v2'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @base_url = options[:base_url] || 'https://api.toptranslation.com'
  @files_url = options[:files_url] || 'https://files.toptranslation.com'
  @access_token = options[:access_token]
  @verbose = options[:verbose] || false
  sign_in!(options) if @access_token.nil? && options.values_at(:email, :password).all?
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



3
4
5
# File 'lib/toptranslation/connection.rb', line 3

def access_token
  @access_token
end

#verboseObject

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, options = {})
  transform_response(request(:get, path, options))
end

#patch(path, options = {}) ⇒ Object



23
24
25
# File 'lib/toptranslation/connection.rb', line 23

def patch(path, options = {})
  transform_response(request(:patch, path, options))
end

#post(path, options = {}) ⇒ Object



19
20
21
# File 'lib/toptranslation/connection.rb', line 19

def post(path, options = {})
  transform_response(request(:post, path, options))
end

#sign_in!(_options = {}) ⇒ Object

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.



45
46
47
48
49
50
51
# File 'lib/toptranslation/connection.rb', line 45

def sign_in!(_options = {})
  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