Class: TranslationCms::GoogleDrive::Document
- Inherits:
-
Object
- Object
- TranslationCms::GoogleDrive::Document
- Defined in:
- lib/translation_cms/google_drive/document.rb
Constant Summary collapse
- FILE_API_ENDPOINT =
'https://www.googleapis.com/drive/v2/files/%{file_id}'
- DEFAULT_UA =
"CarrierWave/#{CarrierWave::VERSION}"
- DEFAULT_AUTH =
'Bearer %{token}'
- HEADER_KEYS =
{ user_agent: 'User-Agent', authorization: 'Authorization' }.freeze
- AVAILABLE_EXPORTS =
%w[ text/plain application/rtf text/html ].freeze
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#file_id ⇒ Object
readonly
Returns the value of attribute file_id.
Class Method Summary collapse
- .default_request(uri, access_token = nil, headers = {}) ⇒ Object
-
.download!(uri, access_token, filepath, headers = {}) ⇒ Object
just download by chunks into file, see more: ruby-doc.org/stdlib-2.1.4/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-Streaming+Response+Bodies.
-
.meta(file_id, access_token, _headers = {}) ⇒ Object
get google drive file meta info.
Instance Method Summary collapse
- #download!(filepath = nil) ⇒ Object
- #get_direct_link ⇒ Object
-
#initialize(file_id, access_token) ⇒ Document
constructor
class << self.
- #meta ⇒ Object
- #tempname(uri) ⇒ Object
Constructor Details
#initialize(file_id, access_token) ⇒ Document
class << self
63 64 65 66 |
# File 'lib/translation_cms/google_drive/document.rb', line 63 def initialize(file_id, access_token) @file_id = file_id @access_token = access_token end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
24 25 26 |
# File 'lib/translation_cms/google_drive/document.rb', line 24 def access_token @access_token end |
#file_id ⇒ Object (readonly)
Returns the value of attribute file_id.
24 25 26 |
# File 'lib/translation_cms/google_drive/document.rb', line 24 def file_id @file_id end |
Class Method Details
.default_request(uri, access_token = nil, headers = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/translation_cms/google_drive/document.rb', line 27 def default_request(uri, access_token = nil, headers = {}) request = Net::HTTP::Get.new uri request.add_field HEADER_KEYS[:user_agent], headers[HEADER_KEYS[:user_agent]] || DEFAULT_UA if access_token.present? request.add_field HEADER_KEYS[:authorization], format((headers[HEADER_KEYS[:authorization]] || DEFAULT_AUTH), token: access_token) end headers.each { |key, val| request.add_field(key, val) } if headers.present? request end |
.download!(uri, access_token, filepath, headers = {}) ⇒ Object
just download by chunks into file, see more: ruby-doc.org/stdlib-2.1.4/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-Streaming+Response+Bodies
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/translation_cms/google_drive/document.rb', line 40 def download!(uri, access_token, filepath, headers = {}) request = default_request uri, access_token, headers Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| http.request(request) do |response| open(filepath, 'wb') do |io| response.read_body do |chunk| io.write chunk end end end end end |
.meta(file_id, access_token, _headers = {}) ⇒ Object
get google drive file meta info
54 55 56 57 58 59 60 |
# File 'lib/translation_cms/google_drive/document.rb', line 54 def (file_id, access_token, _headers = {}) uri = URI(format(FILE_API_ENDPOINT, file_id: file_id)) response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| http.request(default_request(uri, access_token)) end JSON.parse(response.body) if response.is_a?(Net::HTTPSuccess) end |
Instance Method Details
#download!(filepath = nil) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/translation_cms/google_drive/document.rb', line 94 def download!(filepath = nil) url = get_direct_link if url.present? uri = URI(url) filepath ||= tempname(uri) begin Document.download!(uri, access_token, filepath) yield filepath if block_given? ensure File.delete(filepath) if File.exist?(filepath) && block_given? end end filepath.presence end |
#get_direct_link ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/translation_cms/google_drive/document.rb', line 80 def get_direct_link url = ['downloadUrl'] # if no download link, but can get export link if url.blank? && ['exportLinks'].present? exports = ['exportLinks'].keys # detect first present available for convertion mime-type available = AVAILABLE_EXPORTS.detect do |mimetype| exports.include?(mimetype) && ['exportLinks'][mimetype].present? end url = ['exportLinks'][available] end url end |
#meta ⇒ Object
68 69 70 71 |
# File 'lib/translation_cms/google_drive/document.rb', line 68 def @meta ||= self.class.(file_id, access_token) @meta || {} end |
#tempname(uri) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/translation_cms/google_drive/document.rb', line 73 def tempname(uri) original_name = ['title'] || File.basename(uri.path) || 'undefined' extname = File.extname(original_name) basename = File.basename original_name, extname Rails.root.join('public', 'uploads', 'tmp', Dir::Tmpname.make_tmpname(basename, extname)) end |