rb1drv 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +8 -8
- data/.travis.yml +6 -6
- data/Gemfile +3 -3
- data/Gemfile.lock +36 -36
- data/README.md +78 -78
- data/lib/rb1drv/auth.rb +33 -33
- data/lib/rb1drv/onedrive.rb +20 -20
- data/lib/rb1drv/onedrive_404.rb +20 -20
- data/lib/rb1drv/onedrive_dir.rb +228 -225
- data/lib/rb1drv/onedrive_file.rb +79 -79
- data/lib/rb1drv/onedrive_item.rb +67 -68
- data/lib/rb1drv/sliced_io.rb +38 -38
- data/lib/rb1drv/version.rb +3 -3
- data/lib/rb1drv.rb +59 -59
- data/rb1drv.gemspec +28 -28
- metadata +2 -2
data/lib/rb1drv/onedrive_item.rb
CHANGED
@@ -1,68 +1,67 @@
|
|
1
|
-
module Rb1drv
|
2
|
-
class OneDriveItem
|
3
|
-
attr_reader :id, :name, :eTag, :size, :mtime, :ctime, :muser, :cuser, :parent_path, :remote_id, :remote_drive_id
|
4
|
-
protected
|
5
|
-
def initialize(od, api_hash)
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
@
|
12
|
-
@
|
13
|
-
@
|
14
|
-
@
|
15
|
-
@
|
16
|
-
@
|
17
|
-
@
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
#
|
22
|
-
#
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
#
|
51
|
-
#
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
#
|
62
|
-
#
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
end
|
1
|
+
module Rb1drv
|
2
|
+
class OneDriveItem
|
3
|
+
attr_reader :id, :name, :eTag, :size, :mtime, :ctime, :muser, :cuser, :parent_path, :remote_id, :remote_drive_id
|
4
|
+
protected
|
5
|
+
def initialize(od, api_hash)
|
6
|
+
@od = od
|
7
|
+
%w(id name eTag size).each do |key|
|
8
|
+
instance_variable_set("@#{key}", api_hash[key])
|
9
|
+
end
|
10
|
+
@remote_drive_id = api_hash.dig('remoteItem', 'parentReference', 'driveId')
|
11
|
+
@remote_id = api_hash.dig('remoteItem', 'id')
|
12
|
+
@mtime = Time.iso8601(api_hash.dig('lastModifiedDateTime'))
|
13
|
+
@ctime = Time.iso8601(api_hash.dig('createdDateTime'))
|
14
|
+
@muser = api_hash.dig('lastModifiedBy', 'user', 'displayName') || 'N/A'
|
15
|
+
@cuser = api_hash.dig('createdBy', 'user', 'displayName') || 'N/A'
|
16
|
+
@parent_path = api_hash.dig('parentReference', 'path')
|
17
|
+
@remote = api_hash.has_key?('remoteItem')
|
18
|
+
end
|
19
|
+
|
20
|
+
# Create subclass instance by checking the item type
|
21
|
+
#
|
22
|
+
# @return [OneDriveFile, OneDriveDir] instanciated drive item
|
23
|
+
def self.smart_new(od, item_hash)
|
24
|
+
if item_hash['remoteItem']
|
25
|
+
item_hash['remoteItem'].each do |key, value|
|
26
|
+
item_hash[key] ||= value
|
27
|
+
end
|
28
|
+
end
|
29
|
+
if item_hash['file']
|
30
|
+
OneDriveFile.new(od, item_hash)
|
31
|
+
elsif item_hash['folder']
|
32
|
+
OneDriveDir.new(od, item_hash)
|
33
|
+
elsif item_hash.dig('error', 'code') == 'itemNotFound'
|
34
|
+
OneDrive404.new
|
35
|
+
else
|
36
|
+
item_hash
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# @return [String] absolute path of current item
|
41
|
+
def absolute_path
|
42
|
+
if @parent_path
|
43
|
+
File.join(@parent_path, @name)
|
44
|
+
else
|
45
|
+
@name
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# TODO: API endpoint does not play well with remote files
|
50
|
+
#
|
51
|
+
# @return [String] api reference path of current object
|
52
|
+
def api_path
|
53
|
+
if remote?
|
54
|
+
"drives/#{@remote_drive_id}/items/#{@remote_id}"
|
55
|
+
else
|
56
|
+
"drive/items/#{@id}"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# TODO: API endpoint does not play well with remote files
|
61
|
+
#
|
62
|
+
# @return [Boolean] whether it's shared by others
|
63
|
+
def remote?
|
64
|
+
@remote
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/rb1drv/sliced_io.rb
CHANGED
@@ -1,38 +1,38 @@
|
|
1
|
-
# SlicedIO slices a large File into a small portion.
|
2
|
-
class SlicedIO
|
3
|
-
def initialize(io, from, to, &block)
|
4
|
-
@io = io
|
5
|
-
@from = from
|
6
|
-
@to = to
|
7
|
-
@block = block
|
8
|
-
rewind
|
9
|
-
end
|
10
|
-
|
11
|
-
def rewind
|
12
|
-
@io.seek(@from)
|
13
|
-
@current = 0
|
14
|
-
end
|
15
|
-
|
16
|
-
def size
|
17
|
-
@size ||= @to - @from + 1
|
18
|
-
end
|
19
|
-
|
20
|
-
def read(len)
|
21
|
-
return nil if @current >= size
|
22
|
-
len = [len, size - @current].min
|
23
|
-
# Notify before we read
|
24
|
-
@block.call(@current, size)
|
25
|
-
failed_count = 0
|
26
|
-
begin
|
27
|
-
@io.read(len)
|
28
|
-
rescue Errno::EIO
|
29
|
-
@io.seek(@from + @current)
|
30
|
-
sleep 1
|
31
|
-
failed_count += 1
|
32
|
-
retry unless failed_count > 5
|
33
|
-
raise
|
34
|
-
end
|
35
|
-
ensure
|
36
|
-
@current += len
|
37
|
-
end
|
38
|
-
end
|
1
|
+
# SlicedIO slices a large File into a small portion.
|
2
|
+
class SlicedIO
|
3
|
+
def initialize(io, from, to, &block)
|
4
|
+
@io = io
|
5
|
+
@from = from
|
6
|
+
@to = to
|
7
|
+
@block = block
|
8
|
+
rewind
|
9
|
+
end
|
10
|
+
|
11
|
+
def rewind
|
12
|
+
@io.seek(@from)
|
13
|
+
@current = 0
|
14
|
+
end
|
15
|
+
|
16
|
+
def size
|
17
|
+
@size ||= @to - @from + 1
|
18
|
+
end
|
19
|
+
|
20
|
+
def read(len)
|
21
|
+
return nil if @current >= size
|
22
|
+
len = [len, size - @current].min
|
23
|
+
# Notify before we read
|
24
|
+
@block.call(@current, size)
|
25
|
+
failed_count = 0
|
26
|
+
begin
|
27
|
+
@io.read(len)
|
28
|
+
rescue Errno::EIO
|
29
|
+
@io.seek(@from + @current)
|
30
|
+
sleep 1
|
31
|
+
failed_count += 1
|
32
|
+
retry unless failed_count > 5
|
33
|
+
raise
|
34
|
+
end
|
35
|
+
ensure
|
36
|
+
@current += len
|
37
|
+
end
|
38
|
+
end
|
data/lib/rb1drv/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Rb1drv
|
2
|
-
VERSION = "0.1.
|
3
|
-
end
|
1
|
+
module Rb1drv
|
2
|
+
VERSION = "0.1.8"
|
3
|
+
end
|
data/lib/rb1drv.rb
CHANGED
@@ -1,59 +1,59 @@
|
|
1
|
-
require 'excon'
|
2
|
-
require 'oauth2'
|
3
|
-
require "rb1drv/version"
|
4
|
-
|
5
|
-
module Rb1drv
|
6
|
-
# Base class to support oauth2 authentication and sending simple API requests.
|
7
|
-
#
|
8
|
-
# Call +#root+ or +#get+ to get an +OneDriveDir+ or +OneDriveFile+ to wotk with.
|
9
|
-
class OneDrive
|
10
|
-
attr_reader :oauth2_client, :logger, :access_token, :conn
|
11
|
-
# Instanciates with app id and secret.
|
12
|
-
def initialize(client_id, client_secret, callback_url, logger=nil)
|
13
|
-
@client_id = client_id
|
14
|
-
@client_secret = client_secret
|
15
|
-
@callback_url = callback_url
|
16
|
-
@logger = logger
|
17
|
-
@oauth2_client = OAuth2::Client.new client_id, client_secret,
|
18
|
-
authorize_url: 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
|
19
|
-
token_url: 'https://login.microsoftonline.com/common/oauth2/v2.0/token'
|
20
|
-
@conn = Excon.new('https://graph.microsoft.com/', persistent: true, idempotent: true)
|
21
|
-
@conn.logger = @logger if @logger
|
22
|
-
end
|
23
|
-
|
24
|
-
# Issues requests to API endpoint.
|
25
|
-
#
|
26
|
-
# @param uri [String] relative path of the API
|
27
|
-
# @param data [Hash] JSON data to be post
|
28
|
-
# @param verb [Symbol] HTTP request verb if data is given
|
29
|
-
#
|
30
|
-
# @return [Hash] response from API.
|
31
|
-
def request(uri, data=nil, verb=:post)
|
32
|
-
@logger.info(uri) if @logger
|
33
|
-
auth_check
|
34
|
-
query = {
|
35
|
-
path: File.join('v1.0/me/', URI.escape(uri)),
|
36
|
-
headers: {
|
37
|
-
'Authorization': "Bearer #{@access_token.token}"
|
38
|
-
}
|
39
|
-
}
|
40
|
-
if data
|
41
|
-
query[:body] = JSON.generate(data)
|
42
|
-
query[:headers]['Content-Type'] = 'application/json'
|
43
|
-
@logger.info(query[:body]) if @logger
|
44
|
-
verb = :post unless [:post, :put, :patch, :delete].include?(verb)
|
45
|
-
response = @conn.send(verb, query)
|
46
|
-
else
|
47
|
-
response = @conn.get(query)
|
48
|
-
end
|
49
|
-
JSON.parse(response.body)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
require 'rb1drv/auth'
|
55
|
-
require 'rb1drv/onedrive'
|
56
|
-
require 'rb1drv/onedrive_item'
|
57
|
-
require 'rb1drv/onedrive_dir'
|
58
|
-
require 'rb1drv/onedrive_file'
|
59
|
-
require 'rb1drv/onedrive_404'
|
1
|
+
require 'excon'
|
2
|
+
require 'oauth2'
|
3
|
+
require "rb1drv/version"
|
4
|
+
|
5
|
+
module Rb1drv
|
6
|
+
# Base class to support oauth2 authentication and sending simple API requests.
|
7
|
+
#
|
8
|
+
# Call +#root+ or +#get+ to get an +OneDriveDir+ or +OneDriveFile+ to wotk with.
|
9
|
+
class OneDrive
|
10
|
+
attr_reader :oauth2_client, :logger, :access_token, :conn
|
11
|
+
# Instanciates with app id and secret.
|
12
|
+
def initialize(client_id, client_secret, callback_url, logger=nil)
|
13
|
+
@client_id = client_id
|
14
|
+
@client_secret = client_secret
|
15
|
+
@callback_url = callback_url
|
16
|
+
@logger = logger
|
17
|
+
@oauth2_client = OAuth2::Client.new client_id, client_secret,
|
18
|
+
authorize_url: 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
|
19
|
+
token_url: 'https://login.microsoftonline.com/common/oauth2/v2.0/token'
|
20
|
+
@conn = Excon.new('https://graph.microsoft.com/', persistent: true, idempotent: true)
|
21
|
+
@conn.logger = @logger if @logger
|
22
|
+
end
|
23
|
+
|
24
|
+
# Issues requests to API endpoint.
|
25
|
+
#
|
26
|
+
# @param uri [String] relative path of the API
|
27
|
+
# @param data [Hash] JSON data to be post
|
28
|
+
# @param verb [Symbol] HTTP request verb if data is given
|
29
|
+
#
|
30
|
+
# @return [Hash] response from API.
|
31
|
+
def request(uri, data=nil, verb=:post)
|
32
|
+
@logger.info(uri) if @logger
|
33
|
+
auth_check
|
34
|
+
query = {
|
35
|
+
path: File.join('v1.0/me/', URI.escape(uri)),
|
36
|
+
headers: {
|
37
|
+
'Authorization': "Bearer #{@access_token.token}"
|
38
|
+
}
|
39
|
+
}
|
40
|
+
if data
|
41
|
+
query[:body] = JSON.generate(data)
|
42
|
+
query[:headers]['Content-Type'] = 'application/json'
|
43
|
+
@logger.info(query[:body]) if @logger
|
44
|
+
verb = :post unless [:post, :put, :patch, :delete].include?(verb)
|
45
|
+
response = @conn.send(verb, query)
|
46
|
+
else
|
47
|
+
response = @conn.get(query)
|
48
|
+
end
|
49
|
+
JSON.parse(response.body)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
require 'rb1drv/auth'
|
55
|
+
require 'rb1drv/onedrive'
|
56
|
+
require 'rb1drv/onedrive_item'
|
57
|
+
require 'rb1drv/onedrive_dir'
|
58
|
+
require 'rb1drv/onedrive_file'
|
59
|
+
require 'rb1drv/onedrive_404'
|
data/rb1drv.gemspec
CHANGED
@@ -1,28 +1,28 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "rb1drv/version"
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "rb1drv"
|
8
|
-
spec.version = Rb1drv::VERSION
|
9
|
-
spec.authors = ["Xinyue Lu"]
|
10
|
-
spec.email = ["i@7086.in"]
|
11
|
-
|
12
|
-
spec.summary = "Ruby OneDrive Library"
|
13
|
-
spec.homepage = "https://github.com/msg7086/rb1drv"
|
14
|
-
spec.license = "MIT"
|
15
|
-
|
16
|
-
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
-
f.match(%r{^(test|spec|features)/})
|
18
|
-
end
|
19
|
-
spec.bindir = "exe"
|
20
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
-
spec.require_paths = ["lib"]
|
22
|
-
spec.required_ruby_version = '>= 2.3'
|
23
|
-
|
24
|
-
spec.add_dependency "oauth2", "~> 1.4"
|
25
|
-
spec.add_dependency "excon", "~> 0.62"
|
26
|
-
spec.add_development_dependency "bundler", "~> 1.16"
|
27
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
-
end
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "rb1drv/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rb1drv"
|
8
|
+
spec.version = Rb1drv::VERSION
|
9
|
+
spec.authors = ["Xinyue Lu"]
|
10
|
+
spec.email = ["i@7086.in"]
|
11
|
+
|
12
|
+
spec.summary = "Ruby OneDrive Library"
|
13
|
+
spec.homepage = "https://github.com/msg7086/rb1drv"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
spec.required_ruby_version = '>= 2.3'
|
23
|
+
|
24
|
+
spec.add_dependency "oauth2", "~> 1.4"
|
25
|
+
spec.add_dependency "excon", "~> 0.62"
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rb1drv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Xinyue Lu
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oauth2
|