Class: CloudDoor::OneDriveApi

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_door/onedrive_api.rb

Constant Summary collapse

AUTH_BASE =

domain for auth

'https://login.live.com/'
AUTH_FORMAT =

URL for auth

AUTH_BASE +
'oauth20_authorize.srf?client_id=%s&scope=%s&response_type=code&redirect_uri=%s'
TOKEN_URL =

URL for get token

AUTH_BASE + 'oauth20_token.srf'
ACTION_BASE =

domain for action

'https://apis.live.net/v5.0/'
USER_FORMAT =

URL for get user info

ACTION_BASE + 'me?access_token=%s'
DIR_FORMAT =

URL for get directory

ACTION_BASE + '%s/files?access_token=%s'
FILE_FORMAT =

URL for get file info

ACTION_BASE + '%s?access_token=%s'
DOWNLOAD_FORMAT =

URL for download file

ACTION_BASE + '%s/content?suppress_redirects=true&access_token=%s'
UPLOAD_FORMAT =

URL for upload file

ACTION_BASE + '%s/files?access_token=%s'
DELETE_FORMAT =

URL for delete file

ACTION_BASE + '%s?access_token=%s'
MKDIR_FORMAT =

URL for make directory

ACTION_BASE + '%s'
UPDATE_SCOPE =

update scope

'wl.skydrive_update,wl.offline_access'
LOG_FILE =

log_file

'./log/request.log'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token) ⇒ OneDriveApi



37
38
39
# File 'lib/cloud_door/onedrive_api.rb', line 37

def initialize(access_token)
  @access_token = access_token
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



7
8
9
# File 'lib/cloud_door/onedrive_api.rb', line 7

def access_token
  @access_token
end

Class Method Details

.make_auth_url(client_id, redirect_url) ⇒ Object



110
111
112
# File 'lib/cloud_door/onedrive_api.rb', line 110

def make_auth_url(client_id, redirect_url)
  AUTH_FORMAT % [client_id, UPDATE_SCOPE, redirect_url]
end

Instance Method Details

#request_delete(file_id) ⇒ Object



94
95
96
97
# File 'lib/cloud_door/onedrive_api.rb', line 94

def request_delete(file_id)
  url = DELETE_FORMAT % [file_id, @access_token]
  send_request(:delete, url)
end

#request_dir(file_id) ⇒ Object



70
71
72
73
# File 'lib/cloud_door/onedrive_api.rb', line 70

def request_dir(file_id)
  url = DIR_FORMAT % [file_id, @access_token]
  send_request(:get, url)
end

#request_download(file_id) ⇒ Object

Raises:



80
81
82
83
84
85
86
87
# File 'lib/cloud_door/onedrive_api.rb', line 80

def request_download(file_id)
  url  = DOWNLOAD_FORMAT % [file_id, @access_token]
  info = send_request(:get, url)
  key  = 'location'
  raise NoDataException if info.nil? || !info.is_a?(Hash) || !info.key?(key)
  file_url = info[key]
  open(file_url).read
end

#request_file(file_id) ⇒ Object



75
76
77
78
# File 'lib/cloud_door/onedrive_api.rb', line 75

def request_file(file_id)
  url = FILE_FORMAT % [file_id, @access_token]
  send_request(:get, url)
end

#request_get_token(code, client_id, client_secret, redirect_url) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cloud_door/onedrive_api.rb', line 41

def request_get_token(code, client_id, client_secret, redirect_url)
  post_body = {
    client_id:     client_id,
    client_secret: client_secret,
    redirect_uri:  redirect_url,
    code:          code,
    grant_type:    'authorization_code'
  }
  header = {content_type: 'application/x-www-form-urlencoded'}
  send_request(:post, TOKEN_URL, post_body, header)
end

#request_mkdir(name, parent_id) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/cloud_door/onedrive_api.rb', line 99

def request_mkdir(name, parent_id)
  url = MKDIR_FORMAT % parent_id
  body = JSON('name' => name)
  header = {
    'Authorization' => "Bearer #{@access_token}",
    'Content-Type'  => 'application/json'
  }
  send_request(:post, url, body, header)
end

#request_refresh_token(refresh_token, client_id, client_secret, redirect_url) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cloud_door/onedrive_api.rb', line 53

def request_refresh_token(refresh_token, client_id, client_secret, redirect_url)
  post_body = {
    client_id:     client_id,
    client_secret: client_secret,
    redirect_uri:  redirect_url,
    grant_type:    'refresh_token',
    refresh_token: refresh_token
  }
  header = {content_type: 'application/x-www-form-urlencoded'}
  send_request(:post, TOKEN_URL, post_body, header)
end

#request_upload(file_path, parent_id) ⇒ Object



89
90
91
92
# File 'lib/cloud_door/onedrive_api.rb', line 89

def request_upload(file_path, parent_id)
  url = UPLOAD_FORMAT % [parent_id, @access_token]
  send_request(:post_file, url, File.new(file_path, 'rb'))
end

#request_userObject



65
66
67
68
# File 'lib/cloud_door/onedrive_api.rb', line 65

def request_user
  url = USER_FORMAT % @access_token
  send_request(:get, url)
end