file-convert 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/file_convert.rb +2 -4
- data/lib/file_convert/client.rb +19 -8
- data/lib/file_convert/configure.rb +1 -4
- data/lib/file_convert/conversion.rb +3 -3
- data/lib/file_convert/exception.rb +13 -12
- data/lib/file_convert/version.rb +1 -1
- metadata +2 -2
data/lib/file_convert.rb
CHANGED
@@ -9,9 +9,7 @@ module FileConvert
|
|
9
9
|
|
10
10
|
extend FileConvert::Configure
|
11
11
|
|
12
|
-
|
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?
|
data/lib/file_convert/client.rb
CHANGED
@@ -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
|
-
|
25
|
+
options = APP_OPTIONS.merge(authorization: get_authorization(
|
26
|
+
gaccount['email'], gaccount['auth_url'], key
|
27
|
+
))
|
23
28
|
|
24
|
-
super(
|
25
|
-
client.authorization
|
26
|
-
@drive = client.discovered_api
|
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
|
37
|
-
|
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
|
@@ -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.
|
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::
|
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
|
19
|
-
def initialize(
|
18
|
+
class ConnectionError < StandardError
|
19
|
+
def initialize(result, action)
|
20
20
|
super()
|
21
|
-
@
|
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
|
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
|
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
|
-
|
38
|
-
|
39
|
-
|
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
|
|
data/lib/file_convert/version.rb
CHANGED
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.
|
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-
|
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
|