Class: EasyDrive::Client

Inherits:
Object
  • Object
show all
Includes:
Files
Defined in:
lib/easy_drive/client.rb

Constant Summary collapse

API_VERSION =
'v2'
CACHED_API_FILE =
"easy_drive-#{API_VERSION}.cache"
CREDENTIAL_STORE_FILE =
"easy_drive-oauth2.json"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Files

#copy, #get, #insert, #list

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ EasyDrive::Client

Initializes a new Client object

Parameters:

  • options (Hash) (defaults to: {})

Yields:

  • (_self)

Yield Parameters:



24
25
26
27
28
29
# File 'lib/easy_drive/client.rb', line 24

def initialize(options = {})
  options.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
  yield(self) if block_given?
end

Instance Attribute Details

#application_nameObject

Returns the value of attribute application_name.



17
18
19
# File 'lib/easy_drive/client.rb', line 17

def application_name
  @application_name
end

#clientObject (readonly)

Returns the value of attribute client.



18
19
20
# File 'lib/easy_drive/client.rb', line 18

def client
  @client
end

#driveObject (readonly)

Returns the value of attribute drive.



18
19
20
# File 'lib/easy_drive/client.rb', line 18

def drive
  @drive
end

Instance Method Details

#file_format_bug_fix(credential_store_file) ⇒ Object

Note:

refresh_token bug fix



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/easy_drive/client.rb', line 33

def file_format_bug_fix(credential_store_file)
  return if !File.exists?(credential_store_file)
  temp = nil 
  File.open(credential_store_file, "r") do |f| 
    temp = JSON.load(f)
    if temp["authorization_uri"].class != String
      temp["authorization_uri"] =
        "https://accounts.google.com/o/oauth2/auth"
    end 
    if temp["token_credential_uri"].class != String
      temp["token_credential_uri"] =
        "https://accounts.google.com/o/oauth2/token"
    end 
  end 
  File.open(credential_store_file, "w") do |f| 
    f.write(temp.to_json)
  end 
end

#setupObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/easy_drive/client.rb', line 52

def setup()
  client = Google::APIClient.new(:application_name => self.class.to_s,
      :application_version => version)

  file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE)
  if file_storage.authorization.nil?
    client_secrets = Google::APIClient::ClientSecrets.load
    flow = Google::APIClient::InstalledAppFlow.new(
      :client_id => client_secrets.client_id,
      :client_secret => client_secrets.client_secret,
      :scope => ['https://www.googleapis.com/auth/drive']
    )
    client.authorization = flow.authorize(file_storage)
  else
    client.authorization = file_storage.authorization
  end

  drive = nil
  if File.exists? CACHED_API_FILE
    File.open(CACHED_API_FILE) do |file|
      drive = Marshal.load(file)
    end
  else
    drive = client.discovered_api('drive', API_VERSION)
    File.open(CACHED_API_FILE, 'w') do |file|
      Marshal.dump(drive, file)
    end
  end

  @client = client
  @drive = drive

  return client, drive
end

#versionString

Returns:

  • (String)


88
89
90
# File 'lib/easy_drive/client.rb', line 88

def version
  "#{EasyDrive::Version}"
end