Class: Skydrive::Client

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper
Defined in:
lib/skydrive/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
# File 'lib/skydrive/client.rb', line 15

def initialize(options = {})
  options.each do |key, val|
    self.send("#{key}=", val) if self.respond_to?("#{key}=")
  end

  RestClient.log = Skydrive.logger
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



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

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



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

def client_secret
  @client_secret
end

#guidObject

Returns the value of attribute guid.



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

def guid
  @guid
end

#user_tokenObject

Returns the value of attribute user_token.



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

def user_token
  @user_token
end

Instance Method Details

#api_call(url, headers = {}) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/skydrive/client.rb', line 173

def api_call(url, headers = {})
  url.gsub!("https:/i", "https://i")
  uri = URI.escape(url)

  headers['Authorization'] = "Bearer #{self.user_token.access_token}" unless headers.has_key? 'Authorization'
  headers['Accept'] = "application/json; odata=verbose" unless headers.has_key? 'Accept'

  result = RestClient.get uri, headers do |response, request, result|
    log_restclient_response(response, request, result)
    parse_api_response(response)
  end
  result["d"] || result
end

#format_results(results) ⇒ Object



98
99
100
101
102
103
# File 'lib/skydrive/client.rb', line 98

def format_results(results)
  results["expires_in"] = results["expires_in"].to_i
  results["not_before"] = Time.at results["not_before"].to_i
  results["expires_on"] = Time.at results["expires_on"].to_i
  results
end

#get_folder_and_files(uri, folder = Skydrive::Folder.new) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/skydrive/client.rb', line 121

def get_folder_and_files(uri, folder = Skydrive::Folder.new)
  data = api_call(uri)

  folder.icon = "folder"
  folder.uri = uri
  folder.name = data['Name']
  folder.server_relative_url = data['ServerRelativeUrl']
  folder.parse_parent_uri
  folder.files = []
  folder.folders = []

  files = api_call(CGI::unescape(data['Files']['__deferred']['uri']))['results']
  files.each do |f|
    new_file = Skydrive::File.new
    new_file.uri = f['__metadata']['uri']
    new_file.file_size = number_to_human_size(f['Length'])
    new_file.name = f['Name']
    new_file.server_relative_url = f['ServerRelativeUrl']
    new_file.time_created = Date.parse(f['TimeCreated'])
    new_file.time_last_modified = Date.parse(f['TimeLastModified'])
    new_file.title = f['Title']
    new_file. = f['ContentTag']
    folder.files << new_file
  end

  sub_folders = api_call(CGI::unescape(data['Folders']['__deferred']['uri']))['results']
  sub_folders.each do |sf|

    # Non-recursively
    sub_folder = Skydrive::Folder.new
    sub_folder.parent_uri = uri
    sub_folder.icon = "folder"
    sub_folder.uri = sf['__metadata']['uri']
    sub_folder.name = sf['Name']
    sub_folder.server_relative_url = sf['ServerRelativeUrl']
    sub_folder.files = []
    sub_folder.folders = []

    #special exception for the special Forms folder in the root directory
    if !folder.parent_uri && sub_folder.name == 'Forms'
      next
    end

    # Recursively
    # sub_folder = get_folder_and_files(sf['__metadata']['uri'])

    folder.folders << sub_folder
  end

  return folder
end

#get_my_files_serviceObject



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

def get_my_files_service
  services = api_call('https://api.office.com/discovery/v1.0/me/services', {'Accept' => nil})
  services["value"].find{|v| v["capability"] = "MyFiles"}
end

#get_personal_url(service_endpoint_uri) ⇒ Object



74
75
76
# File 'lib/skydrive/client.rb', line 74

def get_personal_url(service_endpoint_uri)
  self.user_token.personal_url = api_call("#{service_endpoint_uri}/files/root/weburl", {'Accept' => nil})['value']
end

#get_realmObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/skydrive/client.rb', line 105

def get_realm
  #401 sharepoint challenge to get the realm
  resource = RestClient::Resource.new "#{personal_url}/_vti_bin/client.svc/",
                                      {headers: {'Authorization' => 'Bearer'}}
  www_authenticate = {}
  resource.get do |response, request, result|
    log_restclient_response(response, request, result)
    response.headers[:www_authenticate].scan(/[\w ]*="[^"]*"/).each do |attribute|
      attribute = attribute.split('=')
      www_authenticate[attribute.first] = attribute.last.delete('"')
    end
  end

  www_authenticate["Bearer realm"]
end

#get_userObject



187
188
189
# File 'lib/skydrive/client.rb', line 187

def get_user
  api_call("#{personal_url}/_api/SP.UserProfiles.PeopleManager/GetMyProperties")
end

#oauth_authorize_redirect_uri(redirect_uri, options = {}) ⇒ Object

URL used to authorize this app for a sharepoint tenant



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/skydrive/client.rb', line 36

def oauth_authorize_redirect_uri(redirect_uri, options = {})
  state = options[:state]
  redirect_params = {
      client_id: client_id,
      redirect_uri: redirect_uri,
      response_type: 'code',
      resource: 'https://api.office.com/discovery/'
  }
  "https://login.windows.net/common/oauth2/authorize?" +
      redirect_params.map{|k,v| "#{k}=#{CGI::escape(v)}"}.join('&') +
      (state ? "&state=#{state}" : "")
end

#personal_urlObject



23
24
25
# File 'lib/skydrive/client.rb', line 23

def personal_url
  user_token.personal_url
end

#refresh_tokenObject



31
32
33
# File 'lib/skydrive/client.rb', line 31

def refresh_token
  user_token.refresh_token
end

#request_oauth_token(code, redirect_url) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/skydrive/client.rb', line 49

def request_oauth_token(code, redirect_url)
  endpoint = 'https://login.windows.net/common/oauth2/token'
  options = {
      client_id: client_id,
      client_secret: client_secret,
      code: code,
      redirect_uri: redirect_url,
      resource: 'https://api.office.com/discovery/',
      grant_type: 'authorization_code',
  }

  RestClient.post endpoint, options do |response, request, result|
    log_restclient_response(response, request, result)
    results = format_results(parse_api_response(response))
    self.user_token.access_token = results['access_token']
    self.user_token.refresh_token = results['refresh_token']
    results
  end
end

#tokenObject



27
28
29
# File 'lib/skydrive/client.rb', line 27

def token
  user_token.access_token
end

#update_api_tokens(params) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/skydrive/client.rb', line 78

def update_api_tokens(params)
  endpoint = 'https://login.windows.net/common/oauth2/token'
  options = {
      client_id: client_id,
      client_secret: client_secret,
      grant_type: 'refresh_token',
      refresh_token: self.user_token.refresh_token
  }.merge(params)

  Rails.logger.info("#{refresh_token} | #{params[:refresh_token]} | #{options[:refresh_token]}")

  RestClient.post endpoint, options do |response, request, result|
    log_restclient_response(response, request, result)
    results = format_results(parse_api_response(response))
    self.user_token.access_token = results['access_token']
    self.user_token.refresh_token = results['refresh_token']
    results
  end
end