Class: Toptranslation::Connection
- Inherits:
-
Object
- Object
- Toptranslation::Connection
- Defined in:
- lib/toptranslation/connection.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#upload_token ⇒ Object
writeonly
Sets the attribute upload_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
- #upload(filepath, type, &block) ⇒ Object
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( = {}) @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 |
#upload_token=(value) ⇒ Object
Sets the attribute upload_token
3 4 5 |
# File 'lib/toptranslation/connection.rb', line 3 def upload_token=(value) @upload_token = value 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
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, = {}) transform_response(request(:get, path, ), ) end |
#patch(path, options = {}) ⇒ Object
21 22 23 |
# File 'lib/toptranslation/connection.rb', line 21 def patch(path, = {}) transform_response(request(:patch, path, ), ) end |
#post(path, options = {}) ⇒ Object
17 18 19 |
# File 'lib/toptranslation/connection.rb', line 17 def post(path, = {}) transform_response(request(:post, path, ), ) 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!() return if @access_token = { email: [:email], password: [:password], application_id: 'pollux' }.merge() @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 |