ruby-office365 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '09f37ce4e6f4c9db6d9e1f12d1b53508fc7dcd9b5efa204582a7164764905796'
4
- data.tar.gz: f5cdfb070ba62cfbd96e81f801e3e2dc8f5584115338ad85c92d9dae86710e6b
3
+ metadata.gz: 86ee47df4ac96c36024adb5c0595afa78889dc9659aebcdbdf01a59e407a0db9
4
+ data.tar.gz: 42ab9271c0d32b6035ad9caecc837137774bde57d0894fd49f34782d8c0f1f1b
5
5
  SHA512:
6
- metadata.gz: 8bb8b810b6aa38e89b2e906e3e7dffcfc63291edd0687dade41f41e7e9a7d3e1ff0f546f5e63ec5339fbe439ed75a06cbc48b11bfc38d8eb7dd45ee4535d6ebb
7
- data.tar.gz: 505e05289637208d656904933e93e8123f3fc0355988cc23edbab27830627f24bb894a441ad389405bccbd733cd91641138310c28eb80a54a26695e9ad1511f0
6
+ metadata.gz: 8dd5a3b6d974b91bf6c2846b1b9b3f433fcc1abe11d6dfc85f3fe3a7b023313db0079b6056a1e27d3d8a36506a434a4ef0e45f327f4d2dd25b0416f681ff5906
7
+ data.tar.gz: fc2997832814ec584184584546ca7443841914191056f5fd3c080017cf21b273a05f9dd5612b928ad7570fb3cbc202ff0cbaba01e86a62b250c409f1e87776b9
data/CHANGELOG.md CHANGED
@@ -23,7 +23,7 @@
23
23
  - get profile `client.contacts`
24
24
  - get contacts data with next link `client.contacts({next_link: 'xxx'})`
25
25
 
26
- ## [0.1.4] - (2022-10-26)
26
+ ## [0.1.5] - (2022-10-27)
27
27
 
28
28
  - Generate URLs for token and able to refresh token
29
29
  - get authorize URL `client.authorize_url`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-office365 (0.1.4)
4
+ ruby-office365 (0.1.5)
5
5
  faraday
6
6
  faraday_middleware
7
7
 
data/README.md CHANGED
@@ -135,7 +135,13 @@ irb(main):018:0> response[:results][0].as_json
135
135
  **Refresh User Token**
136
136
 
137
137
  ```ruby
138
- irb(main):005:0> client.refresh_token!
138
+ irb(main):005:0> response = client.refresh_token!
139
+ irb(main):005:0> response.scope
140
+ => "openid User.Read profile email"
141
+ irb(main):005:0> response.access_token
142
+ => "eyJ0eXAiOiJKV1QiLCJub25jZSI6ImFDYUladFJ6M3RSc3dFaktxUHdGbF9kVlFmbjJabG85Mjlkb2xaeFBhZm8iLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGxWMFRPSSIsImtpZCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGxWMFRPSSJ9..."
143
+ irb(main):005:0> response.refresh_token
144
+ => "0.ARgA7EiQdLv1qECnFqPfrznKsT9ERYaGfG9Ki5WzQtEllj8YAJk.AgABAAEAAAD--DLA3VO7QrddgJg7WevrAgDs_wQA9P-Q1ODlBsrdZi-5s2mfLtEsavBgiEhGcz1KEf26fMrGFU3LM_og5l6wjSAtQ83XHLuje0_KYGol26_LGV_uH0F1MwCFR1N3ctwg4_...."
139
145
  ```
140
146
 
141
147
  ## Copyright
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Office365
4
+ module Models
5
+ class AccessToken < Base
6
+ # attr_accessor :token_type, :scope, :expires_in, :ext_expires_in, :access_token, :refresh_token
7
+ end
8
+ end
9
+ end
@@ -10,5 +10,6 @@ module Office365
10
10
  autoload :Owner, "office365/models/owner"
11
11
  autoload :EmailAddress, "office365/models/email_address"
12
12
  autoload :Contact, "office365/models/contact"
13
+ autoload :AccessToken, "office365/models/access_token"
13
14
  end
14
15
  end
@@ -16,37 +16,41 @@ module Office365
16
16
  end
17
17
 
18
18
  def get(uri, args: {})
19
- r_uri = URI(uri.start_with?("https") ? uri : (Office365::API_HOST + uri))
19
+ req_url = URI(uri.start_with?("https") ? uri : (Office365::API_HOST + uri))
20
20
 
21
- response = Faraday.new(url: [r_uri.scheme, "://", r_uri.hostname].join, headers: headers) do |faraday|
21
+ response = Faraday.new(url: [req_url.scheme, "://", req_url.hostname].join, headers: headers) do |faraday|
22
22
  faraday.adapter Faraday.default_adapter
23
23
  faraday.response :json
24
24
  faraday.response :logger, ::Logger.new($stdout), bodies: true if dev_developement?
25
- end.get(r_uri.request_uri, *args)
25
+ end.get(req_url.request_uri, *args)
26
26
 
27
- resp_body = response.body
28
- return resp_body if response.status == 200
29
- raise InvalidAuthenticationTokenError, resp_body.dig("error", "message") if response.status == 401
30
-
31
- raise Error, resp_body["error"]
27
+ parse_respond(response)
32
28
  end
33
29
 
34
30
  def post(uri, args)
35
- response = Faraday.new(url: Office365::API_HOST, headers: headers) do |faraday|
31
+ req_url = URI(uri.start_with?("https") ? uri : (Office365::API_HOST + uri))
32
+
33
+ response = Faraday.new(url: [req_url.scheme, "://", req_url.hostname].join, headers: post_headers) do |faraday|
36
34
  faraday.adapter Faraday.default_adapter
37
35
  faraday.response :json
38
36
  faraday.response :logger, ::Logger.new($stdout), bodies: true if dev_developement?
39
- end.post(uri, args)
37
+ end.post(req_url.request_uri, args.to_query)
38
+
39
+ parse_respond(response)
40
+ end
40
41
 
42
+ private
43
+
44
+ def parse_respond(response)
41
45
  resp_body = response.body
46
+
42
47
  return resp_body if response.status == 200
43
48
  raise InvalidAuthenticationTokenError, resp_body.dig("error", "message") if response.status == 401
49
+ raise InvaliRequestError, resp_body["error_description"] if response.status == 400
44
50
 
45
51
  raise Error, resp_body["error"]
46
52
  end
47
53
 
48
- private
49
-
50
54
  def headers
51
55
  {
52
56
  "Content-Type" => "application/json",
@@ -57,6 +61,12 @@ module Office365
57
61
  def dev_developement?
58
62
  debug
59
63
  end
64
+
65
+ def post_headers
66
+ {
67
+ "Content-Type" => "application/x-www-form-urlencoded"
68
+ }
69
+ end
60
70
  end
61
71
  end
62
72
  end
@@ -23,13 +23,20 @@ module Office365
23
23
  end
24
24
 
25
25
  def refresh_token!
26
- post(token_url, {
27
- refresh_token: refresh_token,
28
- client_id: client_id,
29
- client_secret: client_secret,
30
- grant_type: "refresh_token",
31
- scope: Office365::SCOPE
32
- })
26
+ Models::AccessToken.new(
27
+ Request.new(nil, debug: debug).post(token_url, token_params)
28
+ )
29
+ end
30
+
31
+ private
32
+
33
+ def token_params
34
+ {
35
+ refresh_token: refresh_token,
36
+ client_id: client_id,
37
+ client_secret: client_secret,
38
+ grant_type: "refresh_token"
39
+ }
33
40
  end
34
41
  end
35
42
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Office365
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.5"
5
5
  end
data/lib/office365.rb CHANGED
@@ -8,13 +8,14 @@ require "office365/rest"
8
8
  require "office365/models"
9
9
 
10
10
  module Office365
11
+ class Error < StandardError; end
12
+ class InvalidAuthenticationTokenError < StandardError; end
13
+ class InvaliRequestError < StandardError; end
14
+
11
15
  API_HOST = "https://graph.microsoft.com"
12
16
  API_VERSION = "v1.0"
13
17
 
14
18
  LOGIN_HOST = "https://login.microsoftonline.com"
15
- SCOPE = "User.read Calendars.read Mail.ReadBasic Contacts.Read"
16
-
17
- class Error < StandardError; end
18
- class InvalidAuthenticationTokenError < StandardError; end
19
+ SCOPE = "offline_access User.read Calendars.read Mail.ReadBasic Contacts.Read"
19
20
  # Your code goes here...
20
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-office365
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Encore Shao
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-26 00:00:00.000000000 Z
11
+ date: 2022-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -113,6 +113,7 @@ files:
113
113
  - lib/office365.rb
114
114
  - lib/office365/client.rb
115
115
  - lib/office365/models.rb
116
+ - lib/office365/models/access_token.rb
116
117
  - lib/office365/models/calendar.rb
117
118
  - lib/office365/models/concerns/base.rb
118
119
  - lib/office365/models/contact.rb