file-convert 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,9 +9,7 @@ module FileConvert
9
9
 
10
10
  extend FileConvert::Configure
11
11
 
12
- FileConvert::Configure.init_config!
13
-
14
- ###
12
+ ##
15
13
  # @param [String] file_path
16
14
  # Source file for conversions
17
15
  # @param [String]
@@ -25,7 +23,7 @@ module FileConvert
25
23
  FileConvert::Conversion.new(client, upload.file, target_mime_type)
26
24
  end
27
25
 
28
- ###
26
+ ##
29
27
  # Initialize a new FileConvert::Client
30
28
  def self.client
31
29
  fail Exception::MissingConfig unless config_present?
@@ -1,15 +1,18 @@
1
1
  module FileConvert
2
2
  require 'google/api_client'
3
+ require 'signet/oauth_2/client'
3
4
 
4
5
  class Client < Google::APIClient
5
6
  APP_OPTIONS = {
6
7
  application_name: 'file-convert',
7
- application_version: FileConvert::Version::STRING
8
+ application_version: FileConvert::Version::STRING,
9
+ # Retry request **once** in case authorization failed
10
+ retries: 1
8
11
  }
9
12
 
10
13
  attr_reader :drive
11
14
 
12
- ###
15
+ ##
13
16
  # Inherits from Google::APIClient
14
17
  # Basically wraps authentiation for simple configuration
15
18
  #
@@ -19,11 +22,13 @@ module FileConvert
19
22
  gaccount['auth_url'] ||= 'https://www.googleapis.com/auth/drive'
20
23
 
21
24
  key = load_key gaccount['pkcs12_file_path'], 'notasecret'
22
- asserter = get_asserter gaccount['email'], gaccount['auth_url'], key
25
+ options = APP_OPTIONS.merge(authorization: get_authorization(
26
+ gaccount['email'], gaccount['auth_url'], key
27
+ ))
23
28
 
24
- super(APP_OPTIONS).tap do |client|
25
- client.authorization = asserter.authorize
26
- @drive = client.discovered_api('drive', 'v2')
29
+ super(options).tap do |client|
30
+ client.authorization.fetch_access_token!
31
+ @drive = client.discovered_api 'drive', 'v2'
27
32
  end
28
33
  end
29
34
 
@@ -33,8 +38,14 @@ module FileConvert
33
38
  Google::APIClient::PKCS12.load_key(path, secret)
34
39
  end
35
40
 
36
- def get_asserter(email, auth_url, key)
37
- Google::APIClient::JWTAsserter.new email, auth_url, key
41
+ def get_authorization(email, auth_url, key)
42
+ Signet::OAuth2::Client.new(
43
+ token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
44
+ audience: 'https://accounts.google.com/o/oauth2/token',
45
+ scope: auth_url,
46
+ issuer: email,
47
+ signing_key: key
48
+ )
38
49
  end
39
50
  end
40
51
  end
@@ -1,11 +1,8 @@
1
1
  module FileConvert
2
2
  module Configure
3
3
  def self.config
4
- @@config
5
- end
6
-
7
- def self.init_config!
8
4
  @@config ||= {}
5
+ @@config
9
6
  end
10
7
 
11
8
  def configure
@@ -47,19 +47,19 @@ module FileConvert
47
47
  available_links.to_hash
48
48
  end
49
49
 
50
- ###
50
+ ##
51
51
  # Actually downloads the file
52
52
  # Raises if request was not successfull
53
53
  #
54
54
  # @return [Google::APIClient::Result]
55
55
  def fetch_file
56
56
  @client.execute(uri: export_links[@mime_type]).tap do |result|
57
- fail connection_error_exception(result) unless result.status == 200
57
+ fail connection_error_exception(result) unless result.success?
58
58
  end
59
59
  end
60
60
 
61
61
  def data_error_exception
62
- Exception::UploadedFileDataError.new @original_upload_result
62
+ Exception::UploadConnectionError.new @original_upload_result
63
63
  end
64
64
 
65
65
  def missing_mime_type_exception
@@ -15,30 +15,31 @@ module FileConvert
15
15
  end
16
16
  end
17
17
 
18
- class UploadedFileDataError < StandardError
19
- def initialize(upload_result)
18
+ class ConnectionError < StandardError
19
+ def initialize(result, action)
20
20
  super()
21
- @upload_result = upload_result
21
+ @result = result
22
+ @error_message = result.error_message
23
+ @action = action
22
24
  end
23
25
 
24
26
  def message
25
27
  ''"
26
- An error occured while uploading: #{@upload_result.error_message}.
28
+ An error occured while #{@action}: #{@error_message}.
29
+ Body of request was: #{@result.body}
27
30
  "''
28
31
  end
29
32
  end
30
33
 
31
- class DownloadConnectionError < StandardError
34
+ class UploadConnectionError < ConnectionError
32
35
  def initialize(result)
33
- super()
34
- @result = result
36
+ super(result, 'uploading')
35
37
  end
38
+ end
36
39
 
37
- def message
38
- ''"
39
- An error occured connection to Google Drive.
40
- Result of request was: #{@result}
41
- "''
40
+ class DownloadConnectionError < ConnectionError
41
+ def initialize(result)
42
+ super(result, 'downloading')
42
43
  end
43
44
  end
44
45
 
@@ -2,7 +2,7 @@ module FileConvert
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 1
5
+ TINY = 2
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].compact.join('.')
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file-convert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2014-09-08 00:00:00.000000000 Z
14
+ date: 2014-09-10 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: google-api-client