Class: Gyaazle::Client
- Inherits:
-
Object
- Object
- Gyaazle::Client
- Defined in:
- lib/gyaazle/client.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #authorize(code) ⇒ Object
- #authorize_url ⇒ Object
- #create_folder(name) ⇒ Object
- #credentials ⇒ Object
- #folder_id ⇒ Object
- #get(file_id) ⇒ Object
- #get_tokens(verifier) ⇒ Object
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
- #refresh_token! ⇒ Object
- #set_permissions(file_id, permissions = nil) ⇒ Object
- #upload(file, metadata = nil) ⇒ Object
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
5 6 7 8 |
# File 'lib/gyaazle/client.rb', line 5 def initialize(config) @config = config @client = HTTPClient.new end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
3 4 5 |
# File 'lib/gyaazle/client.rb', line 3 def client @client end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
3 4 5 |
# File 'lib/gyaazle/client.rb', line 3 def config @config end |
Instance Method Details
#authorize(code) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/gyaazle/client.rb', line 10 def (code) json = client.post_content("https://accounts.google.com/o/oauth2/token", { :code => code, :client_id => config.id, :client_secret => config.secret, :redirect_uri => "urn:ietf:wg:oauth:2.0:oob", :grant_type => "authorization_code", }) MultiJson.load(json, :symbolize_keys => true) end |
#authorize_url ⇒ Object
21 22 23 24 |
# File 'lib/gyaazle/client.rb', line 21 def url = "https://accounts.google.com/o/oauth2/auth?client_id=#{config.id}&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/drive" Nokogiri::HTML.parse(client.get(url).body).at('a').attributes["href"].to_s end |
#create_folder(name) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/gyaazle/client.rb', line 118 def create_folder(name) json = client.post_content( "https://www.googleapis.com/drive/v2/files", MultiJson.dump({ :title => name, :mimeType => "application/vnd.google-apps.folder", :parents => [{:id => "root"}], }), { "Authorization" => "#{credentials[:token_type]} #{credentials[:access_token]}", 'Content-Type' => 'application/json;charset=utf-8', } ) folder = MultiJson.load(json, :symbolize_keys => true) config.update(:folder_id => folder[:id]) folder[:id] end |
#credentials ⇒ Object
136 137 138 |
# File 'lib/gyaazle/client.rb', line 136 def credentials config.load end |
#folder_id ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/gyaazle/client.rb', line 106 def folder_id id = credentials[:folder_id] return create_folder("Gyaazle") unless id folder = get(id) if !folder[:id] || folder[:labels][:trashed] create_folder("Gyaazle") else id end end |
#get(file_id) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/gyaazle/client.rb', line 94 def get(file_id) json = client.get( "https://www.googleapis.com/drive/v2/files/#{file_id}", {}, { "Authorization" => "#{credentials[:token_type]} #{credentials[:access_token]}", } ).body MultiJson.load(json, :symbolize_keys => true) end |
#get_tokens(verifier) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/gyaazle/client.rb', line 26 def get_tokens(verifier) client.post_content("https://accounts.google.com/o/oauth2/token",{ :code => verifier, :client_id => config.id, :client_secret => config.secret, :redirect_uri => "urn:ietf:wg:oauth:2.0:oob", :grant_type => "authorization_code", }) end |
#refresh_token! ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/gyaazle/client.rb', line 36 def refresh_token! json = client.post("https://accounts.google.com/o/oauth2/token", { :refresh_token => credentials[:refresh_token], :client_id => config.id, :client_secret => config.secret, :grant_type => "refresh_token", }).body config.update(:access_token => MultiJson.load(json)["access_token"]) config.load end |
#set_permissions(file_id, permissions = nil) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/gyaazle/client.rb', line 74 def (file_id, = nil) json = MultiJson.dump( || credentials[:permissions] || { :role => "reader", :type => "#{"anyone"}", :value => "#{"me"}", :withLink => "true", :additionalRoles => ["commenter"], } ) client.post_content( "https://www.googleapis.com/drive/v2/files/#{file_id}/permissions", json, { "Authorization" => "#{credentials[:token_type]} #{credentials[:access_token]}", 'Content-Type' => 'application/json;charset=utf-8', } ) end |
#upload(file, metadata = nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/gyaazle/client.rb', line 47 def upload(file, = nil) body = [ { 'Content-Type' => 'application/json;charset=utf-8', :content => MultiJson.dump( || { :title => File.basename(file), :shared => "true", :parents => [:id => folder_id] }) }, { :content => File.read(file), }, ] response = client.post( 'https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart', body, { "Authorization" => "#{credentials[:token_type]} #{credentials[:access_token]}", "Content-Type" => "multipart/related; boundary=___#{Time.now.to_f}___", } ) # Note: deeplink is https://drive.google.com/uc?export=view&id={fileId} MultiJson.load(response.body, :symbolize_keys => true) end |