Class: OneDrive::V1

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, redirect_uri, scope = 'files.read') ⇒ V1

Returns a new instance of V1.



16
17
18
19
20
# File 'lib/one_drive.rb', line 16

def initialize(client_id,redirect_uri,scope='files.read')
  @client_id = client_id
  @scope = scope
  @redirect_uri = redirect_uri
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



13
14
15
# File 'lib/one_drive.rb', line 13

def client_id
  @client_id
end

#codeObject

Returns the value of attribute code.



13
14
15
# File 'lib/one_drive.rb', line 13

def code
  @code
end

#current_driveObject

Returns the value of attribute current_drive.



13
14
15
# File 'lib/one_drive.rb', line 13

def current_drive
  @current_drive
end

#current_itemObject

Returns the value of attribute current_item.



13
14
15
# File 'lib/one_drive.rb', line 13

def current_item
  @current_item
end

#drivesObject

Returns the value of attribute drives.



13
14
15
# File 'lib/one_drive.rb', line 13

def drives
  @drives
end

#expires_inObject

Returns the value of attribute expires_in.



13
14
15
# File 'lib/one_drive.rb', line 13

def expires_in
  @expires_in
end

#itemObject

Returns the value of attribute item.



13
14
15
# File 'lib/one_drive.rb', line 13

def item
  @item
end

#itemsObject

Returns the value of attribute items.



13
14
15
# File 'lib/one_drive.rb', line 13

def items
  @items
end

#my_driveObject

Returns the value of attribute my_drive.



13
14
15
# File 'lib/one_drive.rb', line 13

def my_drive
  @my_drive
end

#recent_driveObject

Returns the value of attribute recent_drive.



13
14
15
# File 'lib/one_drive.rb', line 13

def recent_drive
  @recent_drive
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



13
14
15
# File 'lib/one_drive.rb', line 13

def redirect_uri
  @redirect_uri
end

#scopeObject

Returns the value of attribute scope.



13
14
15
# File 'lib/one_drive.rb', line 13

def scope
  @scope
end

#special_driveObject

Returns the value of attribute special_drive.



13
14
15
# File 'lib/one_drive.rb', line 13

def special_drive
  @special_drive
end

#special_itemsObject

Returns the value of attribute special_items.



13
14
15
# File 'lib/one_drive.rb', line 13

def special_items
  @special_items
end

#tokenObject

Returns the value of attribute token.



13
14
15
# File 'lib/one_drive.rb', line 13

def token
  @token
end

#token_typeObject

Returns the value of attribute token_type.



13
14
15
# File 'lib/one_drive.rb', line 13

def token_type
  @token_type
end

Instance Method Details

#base_api_urlObject



21
22
23
# File 'lib/one_drive.rb', line 21

def base_api_url
  "https://graph.microsoft.com/v1.0"
end

#code_urlObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/one_drive.rb', line 30

def code_url
  # Get code
  # https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={client_id}&scope={scope}
  # &response_type=code&redirect_uri={redirect_uri}
  "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=#{@client_id}&scope=#{@scope}&response_type=code&redirect_uri=#{@redirect_uri}"

  #Get a new access token or refresh token
  # POST https://login.microsoftonline.com/common/oauth2/v2.0/token
  # Content-Type: application/x-www-form-urlencoded
  #
  # client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}
  # &refresh_token={refresh_token}&grant_type=refresh_token
end

#download(options = {}) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/one_drive.rb', line 163

def download options ={}
  # Download
  # GET /drives/{drive-id}/items/{item-id}/content
  # GET /groups/{group-id}/drive/items/{item-id}/content
  # GET /me/drive/root:/{item-path}:/content
  # GET /me/drive/items/{item-id}/content
  # GET /sites/{siteId}/drive/items/{item-id}/content
  # GET /users/{userId}/drive/items/{item-id}/content
  # GET /drive/items/{item-id}/content?format={format}
  # GET /drive/root:/{path and filename}:/content?format={format}
  url = "https://graph.microsoft.com/v1.0"
  if options.include? :drive_id
    url = url + "/drives/#{options[:drive_id]}/"
  elsif options.include? :group_id
    url = url + "/groups/#{options[:group_id]}/drive/"
  elsif options.include? :site_id
    url = url + "/sites/#{options[:site_id]}/drive/"
  elsif options.include? :user_id
    url = url + "/users/#{options[:user_id]}/drive/"
  else
    url = url + "/me/drive/"
  end
  if options.include? :item_id
    url = url + "items/#{options[:item_id]}/content"
  elsif options.include? :item_path
    url = url + "root:/#{options[:item_path]}:/content"
  else
    return "OneDrive URL : Item id is not valid"
  end
  if options.include? :format
    url = url + "?format=#{options[:format]}"
  end
  @item = (HTTParty.get(url,headers: set_headers).body)
end

#get_children_of_special_folder(name) ⇒ Object

Get children of a special folder



69
70
71
72
# File 'lib/one_drive.rb', line 69

def get_children_of_special_folder name
  url = base_api_url + "/me/drive/special/#{name}/children"
  @special_items = JSON.parse(HTTParty.get(url,headers: set_headers).body)
end

#get_drive(drive_id) ⇒ Object

Get a drive by ID



44
45
46
47
# File 'lib/one_drive.rb', line 44

def get_drive drive_id
  url = base_api_url + "/drives/#{drive_id}"
  @drives = JSON.parse(HTTParty.get(url,headers: set_headers).body)
end

#get_drives(type = nil, id = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/one_drive.rb', line 84

def get_drives type = nil,id=nil
  # GET /me/drives
  # GET /users/{userId}/drives
  # GET /sites/{siteId}/drives
  # GET /groups/{groupId}/drives
  case type
  when 'users'
    url = "https://graph.microsoft.com/v1.0/users/#{id}/drives"
  when 'sites'
    url = "https://graph.microsoft.com/v1.0/sites/#{id}/drives"
  when 'groups'
    url = "https://graph.microsoft.com/v1.0/groups/#{id}/drives"
  else
    url = "https://graph.microsoft.com/v1.0/me/drives"
  end
  @drives = JSON.parse(HTTParty.get(url,headers: set_headers).body)
end

#get_item(options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/one_drive.rb', line 111

def get_item options={}
  url = "https://graph.microsoft.com/v1.0"
  # Get item
  # GET /drives/{drive-id}/items/{item-id}
  # GET /drives/{drive-id}/root:/{item-path}
  # GET /groups/{group-id}/drive/items/{item-id}
  # GET /groups/{group-id}/drive/root:/{item-path}
  # GET /me/drive/items/{item-id}
  # GET /me/drive/root:/{item-path}
  # GET /sites/{siteId}/drive/items/{itemId}
  # GET /sites/{siteId}/drive/root:/{item-path}
  # GET /users/{userId}/drive/items/{itemId}
  # GET /users/{userId}/drive/root:/{item-path}
  if options.include? :drive_id
    url = "/drives/#{options[:drive_id]}/"
  elsif options.include? :group_id
    url = "/groups/#{options[:group_id]}/"
  elsif options.include? :site_id
    url = "/sites/#{options[:site_id]}/"
  elsif options.include? :user_id
    url = "/users/#{options[:user_id]}/"
  else
    url = "/me/drive/"
  end
  if options.include? :item_id
    url = url + "items/#{options[:item_id]}"
  elsif options.include? :item_path
    url = url + "root:/#{options[:item_path]}"
  else
    return "OneDrive URL is not valid"
  end
  @items = JSON.parse(HTTParty.get(url,headers: set_headers).body)
end

#get_library_for_site(site_id) ⇒ Object

Get the document library for a site



59
60
61
62
# File 'lib/one_drive.rb', line 59

def get_library_for_site site_id
  url = base_api_url + "/sites/#{site_id}/drive"
  @drives = JSON.parse(HTTParty.get(url,headers: set_headers).body)
end

#get_list(options = {}) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/one_drive.rb', line 197

def get_list options={}
  url = "https://graph.microsoft.com/v1.0"
  p '------------------------'
  # Find Children
  # GET /drives/{drive-id}/items/{item-id}/children
  # GET /groups/{group-id}/drive/items/{item-id}/children
  # GET /me/drive/items/{item-id}/children
  # GET /sites/{site-id}/drive/items/{item-id}/children
  # GET /users/{user-id}/drive/items/{item-id}/children
  if options.include? :drive_id
    url = url + "/drives/#{options[:drive_id]}/"
  elsif options.include? :group_id
    url = url + "/groups/#{options[:group_id]}/"
  elsif options.include? :site_id
    url = url + "/sites/#{options[:site_id]}/"
  elsif options.include? :user_id
    url = url + "/users/#{options[:user_id]}/"
  else
    url = url + "/me/drive/"
  end
  if options.include? :item_id
    url = url + "items/#{options[:item_id]}/children"
  else
    return "OneDrive URL : Item id is not valid"
  end
  p url
  @items = JSON.parse(HTTParty.get(url,headers: set_headers).body)
end

#get_my_driveObject

Get a current user’s OneDrive



54
55
56
57
# File 'lib/one_drive.rb', line 54

def get_my_drive
  url = base_api_url + "/me/drive"
  @my_drive = JSON.parse(HTTParty.get(url,headers: set_headers).body)
end

#get_special_folder_by_name(name) ⇒ Object

Get a special folder by name



64
65
66
67
# File 'lib/one_drive.rb', line 64

def get_special_folder_by_name name
  url = base_api_url + "/me/drive/special/#{name}"
  @special_drive = JSON.parse(HTTParty.get(url,headers: set_headers).body)
end

#get_users_drive(id_or_user_principal_name) ⇒ Object

Get a user’s OneDrive



49
50
51
52
# File 'lib/one_drive.rb', line 49

def get_users_drive id_or_user_principal_name
  url = base_api_url + "/users/#{id_or_user_principal_name}/drive"
  @drives = JSON.parse(HTTParty.get(url,headers: set_headers).body)
end

#items_shared_with_meObject

List items shared with the signed-in user



74
75
76
77
# File 'lib/one_drive.rb', line 74

def items_shared_with_me
  url = base_api_url + "/me/drive/sharedWithMe"
  @shared_items = JSON.parse(HTTParty.get(url,headers: set_headers).body)
end

#recentObject

GET /drives/remoteItem-driveId/items/remoteItem-id



80
81
82
83
# File 'lib/one_drive.rb', line 80

def recent
  url = base_api_url + '/me/drive/recent'
  @recent_drive = JSON.parse(HTTParty.get(url,headers: set_headers).body)
end

#search(options = {}) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/one_drive.rb', line 144

def search options ={}
  if options.include? :drive_id
    # GET /drives/{drive-id}/root/search(q='{search-text}')
    url = base_api_url + "/drives/#{options[:drive_id]}/root/search(q='#{options[:search_text]}')"
  elsif options.include? :group_id
    # GET /groups/{group-id}/drive/root/search(q='{search-text}')
    url = base_api_url + "/groups/#{options[:group_id]}/drive/root/search(q='#{options[:search_text]}')"
  elsif options.include? :site_id
    # GET /sites/{site-id}/drive/root/search(q='{search-text}')
    url = base_api_url + "/sites/#{options[:site_id]}/drive/root/search(q='#{options[:search_text]}')"
  elsif options.include? :user_id
    # GET /users/{user-id}/drive/root/search(q='{search-text}')
    url = base_api_url + "/users/#{options[:user_id]}/drive/root/search(q='#{options[:search_text]}')"
  else
    # GET /me/drive/root/search(q='{search-text}')
    url = base_api_url + "/me/drive/root/search(q='#{options[:search_text]}')"
  end
  @items = JSON.parse(HTTParty.get(url,headers: set_headers).body)
end

#set_code(code) ⇒ Object



108
109
110
# File 'lib/one_drive.rb', line 108

def set_code code
  @code = code
end

#set_headersObject



225
226
227
# File 'lib/one_drive.rb', line 225

def set_headers
  {"Content-Type"=> 'application/json',"Authorization"=>"bearer #{@token}"}
end

#set_token(token, expires_in = 3600, token_type = 'bearer') ⇒ Object



101
102
103
104
105
106
107
# File 'lib/one_drive.rb', line 101

def set_token token,expires_in=3600,token_type='bearer'
  # &token_type=bearer&expires_in=3600&scope=Files.Read
  @token = token
  @expires_in = expires_in
  @token_type = token_type
  true
end

#token_urlObject



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

def token_url
  # OAuth URI token
  # GET https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={client_id}&scope={scope}
  #   &response_type=token&redirect_uri={redirect_uri}
  "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=#{@client_id}&scope=#{@scope}&response_type=token&redirect_uri=#{@redirect_uri}"
end