|
| 1 | +module Google |
| 2 | + module Drive |
| 3 | + |
| 4 | + def self.included(base) |
| 5 | + base.send :include, InstanceMethods |
| 6 | + end |
| 7 | + |
| 8 | + module InstanceMethods |
| 9 | + #retrieve new Google AccessToken if it get expired |
| 10 | + def refresh_access_token(user) |
| 11 | + access_token_age = (Time.zone.now - user.google_token_updated_at) |
| 12 | + if access_token_age > APP_CONSTANTS["google_token_expire_time"] |
| 13 | + client_id = Rails.application.secrets.google_drive["client_id"] |
| 14 | + client_secret = Rails.application.secrets.google_drive["client_secret"] |
| 15 | + payload = "client_id=#{client_id}&client_secret=#{client_secret}&refresh_token=#{user.google_refresh_token}&grant_type=refresh_token" |
| 16 | + rest_resource = RestClient::Resource.new("https://www.googleapis.com/oauth2/v3/token") |
| 17 | + response = rest_resource.post payload , :content_type => 'application/x-www-form-urlencoded' |
| 18 | + new_access_token = JSON.parse(response)["access_token"] |
| 19 | + # update user with new Google access token |
| 20 | + user.update(google_access_token: new_access_token, google_token_updated_at: Time.now) |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + # Delete the file with the file_id from google drive using Google Drive API |
| 25 | + def delete_current_presentation(file_id, user) |
| 26 | + delete_url = "https://www.googleapis.com/drive/v2/files/#{file_id}" |
| 27 | + headers = { 'Authorization': "Bearer #{user.google_access_token}", 'Content-type': 'application/json' } |
| 28 | + rest_resource = RestClient::Resource.new(delete_url, :headers => headers) |
| 29 | + rest_resource.delete |
| 30 | + end |
| 31 | + |
| 32 | + # Get presentation_id from Slide or Presentation URL |
| 33 | + def get_presentation_id(slide_url) |
| 34 | + starting_index = slide_url.index("/d/") |
| 35 | + # Following string matching seems to be lengthy, But is faster the Regular Exp. |
| 36 | + ending_index = slide_url.index("/present#slide") || slide_url.index("/present?slide") || slide_url.index("/edit#slide") || slide_url.index("/edit?slide") |
| 37 | + return nil unless ending_index and starting_index |
| 38 | + presentation_id = slide_url[starting_index + 3..ending_index-1] |
| 39 | + end |
| 40 | + |
| 41 | + # Extract slide_id from google presentation/slide url |
| 42 | + def get_slide_id( slide_url ) |
| 43 | + idx = slide_url.index( "=id." ) |
| 44 | + return nil unless idx |
| 45 | + slide_url.slice( idx + 4..-1 ) |
| 46 | + end |
| 47 | + |
| 48 | + # Returns true if page contains text matching known messages of Unauthorization |
| 49 | + def unauthorized?(browser_text) |
| 50 | + !!(browser_text.match(/unauthorized/i) || browser_text.match(/You have been signed out/i)) |
| 51 | + end |
| 52 | + |
| 53 | + # You can see google presentation in 2 modes. |
| 54 | + # This returns string :present if slide url contains `/present?slide_id`||`/present#slide_id` |
| 55 | + # Or returns string :edit if slide url contains `/edit#slide_id`||`/edit?slide_id` |
| 56 | + def slide_url_mode(slide_url) |
| 57 | + return :edit if slide_url.match(/\/edit(#|\?)/) |
| 58 | + return :presentation if slide_url.match(/\/present(#|\?)/) |
| 59 | + end |
| 60 | + |
| 61 | + # Identifying whether it is a SlideUrl in presentation mode or Edit mode and toggles the state |
| 62 | + # toggle_edit_present_slide_url('edit_url') #=> present_url |
| 63 | + # toggle_edit_present_slide_url('present_url') #=> edit_url |
| 64 | + # Warning : Do not use single regex for matching, Performance issues may occur |
| 65 | + def toggle_edit_present_slide_url(slide_url) |
| 66 | + # Identifying whether it is a SlideUrl in presentation mode or Edit mode |
| 67 | + present_matchers = slide_url.match("/present#slide") |
| 68 | + |
| 69 | + case slide_url.match(/\/(present|edit)(#|\?)/).to_s |
| 70 | + when "/present#" |
| 71 | + return slide_url.gsub("/present#", "/edit#") |
| 72 | + when "/present?" |
| 73 | + # return slide_url.gsub("/present?", "/edit#") |
| 74 | + uri = Addressable::URI.parse(slide_url.gsub('/present?', '/edit?')) |
| 75 | + query_values = uri.query_values || {} |
| 76 | + uri.fragment = "slide=" + query_values.delete('slide') if query_values.present? |
| 77 | + uri.query_values = query_values.present? ? query_values : nil |
| 78 | + return uri.to_s |
| 79 | + when "/edit#", "/edit?" |
| 80 | + return slide_url.gsub(/\/edit(#|\?)/, "/present?") |
| 81 | + else |
| 82 | + return nil |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + end |
| 88 | +end |
0 commit comments