Class: Toptranslation::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



5
6
7
8
9
10
11
# File 'lib/toptranslation/connection.rb', line 5

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

#upload_token=(value) ⇒ Object

Sets the attribute upload_token

Parameters:

  • value

    the value to set the attribute upload_token to.



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

def upload_token=(value)
  @upload_token = value
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



25
26
27
28
29
# File 'lib/toptranslation/connection.rb', line 25

def download(url, path, &block)
  puts "# downloading #{url}" if @verbose
  uri = URI.parse(url)
  download_uri(uri, path, &block)
end

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



13
14
15
# File 'lib/toptranslation/connection.rb', line 13

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

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



21
22
23
# File 'lib/toptranslation/connection.rb', line 21

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

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



17
18
19
# File 'lib/toptranslation/connection.rb', line 17

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

#sign_in!(options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/toptranslation/connection.rb', line 37

def sign_in!(options)
  return if @access_token

   = {
    email: options[:email],
    password: options[:password],
    application_id: 'pollux'
  }.merge(options)

  @access_token = post('/auth/sign_in', .merge(version: 2))['access_token']

  puts "# Requested access token #{@access_token}" if @verbose

  @access_token
end

#upload(filepath, type, &block) ⇒ Object



31
32
33
34
35
# File 'lib/toptranslation/connection.rb', line 31

def upload(filepath, type, &block)
  uri = URI.parse("#{@files_url}/documents")
  file = File.new(filepath)
  upload_file(file, type, uri, &block)
end