ruby-office365 0.1.7 → 0.1.9

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: '0490fef4bbf78efa42231c69e2b12fdb9f12ec37805c5e846225568e85cb03ba'
4
- data.tar.gz: a40082526d63f968875194b969146f787de82090207132c69b7d62c344719c6f
3
+ metadata.gz: 5ae4eff73f21d319447f7b789178a0befbc9f480631c72b9556e091f395c2905
4
+ data.tar.gz: b2eeb9e486a6cace523a577c1c08453f6a0061b05d7212302b4fc7cc918f6d7a
5
5
  SHA512:
6
- metadata.gz: 1a5220233fdf3095a0423a0433b54007ad251cc1bd3dfea051a6c2b2ef3e83586d3142198deda976ffa3425e9c6b97723e0e78af9a564872d2c42e1a9bba7a68
7
- data.tar.gz: 445c96c052a74d5ca866769182c98a3da62de4308f83d52db760b5d2bac1fbce3c7be02cdecdfde933ea0d0c3e1c98c264f3175bba68dfbd933c8e29b55f8d15
6
+ metadata.gz: f4f935c0fb140ef8e7758f6d2f481a9f38ca5acc256a58b1f9679c0efd1d37a60ec1bf06b22be7fd9812c909cde5162e0c4bcc9bd10b3cd1d3badc90e885132a
7
+ data.tar.gz: a19af99ccc99cacf0beaddca0fa5288d8d0e3020dc01dd6c4eef183a38f1dee0b9144011471d966b16f72a72a400dc6b8e9afc80aced0f9eea1ae08d289cad15
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-office365 (0.1.7)
4
+ ruby-office365 (0.1.9)
5
5
  faraday
6
6
  faraday_middleware
7
7
 
@@ -15,7 +15,7 @@ GEM
15
15
  crack (0.4.5)
16
16
  rexml
17
17
  diff-lcs (1.5.0)
18
- faraday (1.10.2)
18
+ faraday (1.10.3)
19
19
  faraday-em_http (~> 1.0)
20
20
  faraday-em_synchrony (~> 1.0)
21
21
  faraday-excon (~> 1.1)
@@ -33,7 +33,7 @@ GEM
33
33
  faraday-httpclient (1.0.1)
34
34
  faraday-multipart (1.0.4)
35
35
  multipart-post (~> 2)
36
- faraday-net_http (1.0.1)
36
+ faraday-net_http (1.0.2)
37
37
  faraday-net_http_persistent (1.2.0)
38
38
  faraday-patron (1.0.0)
39
39
  faraday-rack (1.0.0)
@@ -43,7 +43,7 @@ GEM
43
43
  hashdiff (1.0.1)
44
44
  json (2.6.2)
45
45
  method_source (1.0.0)
46
- multipart-post (2.2.3)
46
+ multipart-post (2.4.1)
47
47
  parallel (1.22.1)
48
48
  parser (3.1.2.1)
49
49
  ast (~> 2.4.1)
data/README.md CHANGED
@@ -100,6 +100,15 @@ irb(main):005:0> client.events[:results]
100
100
  irb(main):005:0> client.events[:next_link]
101
101
  ```
102
102
 
103
+ **Get my event by id**
104
+
105
+ ```ruby
106
+ irb(main):005:0> client.event('identifier')
107
+ irb(main):005:0> client.event('identifier')[:results]
108
+ ```
109
+
110
+ Results will return an array even if it is a single result.
111
+
103
112
  **Get my mails by access token**
104
113
 
105
114
  ```ruby
@@ -171,6 +180,37 @@ irb(main):005:0> response.refresh_token
171
180
  => "0.ARgA7EiQdLv1qECnFqPfrznKsT9ERYaGfG9Ki5WzQtEllj8YAJk.AgABAAEAAAD--DLA3VO7QrddgJg7WevrAgDs_wQA9P-Q1ODlBsrdZi-5s2mfLtEsavBgiEhGcz1KEf26fMrGFU3LM_og5l6wjSAtQ83XHLuje0_KYGol26_LGV_uH0F1MwCFR1N3ctwg4_...."
172
181
  ```
173
182
 
183
+ **Create Subscription: it will create a webhook for Office365**
184
+
185
+ ```ruby
186
+ args = {
187
+ changeType: "updated,deleted",
188
+ notificationUrl: "https://hello-world.com/office365/notifications",
189
+ lifecycleNotificationUrl: "https://hello-world.com/office365/lifecycle_notifications",
190
+ resource: "/me/{type}",
191
+ expirationDateTime: "2024-08-07T12:00:00.0000000Z",
192
+ clientState: "SecretClientState"
193
+ }
194
+
195
+ irb(main):005:0> subscription = client.create_subscription(args)
196
+ ```
197
+
198
+ will return the subscription object `Office365::Models::Subscription`
199
+
200
+ **Renew Subscription**
201
+
202
+ ```ruby
203
+ args = {
204
+ identifier: "subscription-identifier",
205
+ expirationDateTime: "2024-08-08T12:00:00.0000000Z"
206
+ }
207
+
208
+ irb(main):005:0> subscription = client.renew_subscription(args)
209
+ ```
210
+
211
+ will return the subscription object `Office365::Models::Subscription`
212
+
213
+
174
214
  ## Development
175
215
 
176
216
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Office365
4
+ module Models
5
+ class Subscription < Base
6
+ end
7
+ end
8
+ end
@@ -12,5 +12,6 @@ module Office365
12
12
  autoload :Contact, "office365/models/contact"
13
13
  autoload :AccessToken, "office365/models/access_token"
14
14
  autoload :Event, "office365/models/event"
15
+ autoload :Subscription, "office365/models/subscription"
15
16
  end
16
17
  end
@@ -5,6 +5,7 @@ require_relative "./mailbox"
5
5
  require_relative "./calendar"
6
6
  require_relative "./contact"
7
7
  require_relative "./event"
8
+ require_relative "./subscription"
8
9
  require_relative "./token"
9
10
 
10
11
  module Office365
@@ -16,6 +17,7 @@ module Office365
16
17
  include Office365::REST::Event
17
18
  include Office365::REST::Contact
18
19
  include Office365::REST::Token
20
+ include Office365::REST::Subscription
19
21
  end
20
22
  end
21
23
  end
@@ -7,8 +7,13 @@ module Office365
7
7
  private
8
8
 
9
9
  def wrap_results(args)
10
- kclass = args.delete(:kclass)
11
- response = get_request(args: args)
10
+ kclass = args.delete(:kclass)
11
+ identifier = args.delete(:identifier)
12
+
13
+ response = get_request(args: args)
14
+
15
+ # If we are getting a single item by id, return the result as an array
16
+ return { results: [kclass.new(response)].flatten } unless identifier.nil?
12
17
 
13
18
  {
14
19
  results: response["value"].map { |v| kclass.new(v) },
@@ -48,7 +53,7 @@ module Office365
48
53
  args[:orderby] = order_by if order_by
49
54
 
50
55
  request_uri = ["/", Office365::API_VERSION, base_uri].join
51
- request_uri += ["?", args.to_query].join if args.any?
56
+ request_uri += ["?", args.ms_hash_to_query].join if args.any?
52
57
  request_uri
53
58
  end
54
59
  end
@@ -6,11 +6,18 @@ module Office365
6
6
  module REST
7
7
  module Event
8
8
  include Concerns::Base
9
+ BASE_URI = "/me/events"
9
10
 
10
11
  # params: args => { next_link: (nil / next_page_url) }
11
12
  # response { results: [], next_link: '...' }
12
13
  def events(args = {})
13
- wrap_results(args.merge(kclass: Models::Event, base_uri: "/me/events"))
14
+ wrap_results(args.merge(kclass: Models::Event, base_uri: BASE_URI))
15
+ end
16
+
17
+ def event(identifier)
18
+ raise ArgumentError, "Identifier must be provided" if identifier.nil? || identifier.empty?
19
+
20
+ wrap_results(kclass: Models::Event, base_uri: [BASE_URI, identifier].join("/"), identifier: identifier)
14
21
  end
15
22
  end
16
23
  end
@@ -28,23 +28,35 @@ module Office365
28
28
  end
29
29
 
30
30
  def post(uri, args)
31
- req_url = URI(uri.start_with?("https") ? uri : (Office365::API_HOST + uri))
31
+ response = faraday_action(__method__, uri, args)
32
+ parse_respond(response)
33
+ end
32
34
 
33
- response = Faraday.new(url: [req_url.scheme, "://", req_url.hostname].join, headers: post_headers) do |faraday|
34
- faraday.adapter Faraday.default_adapter
35
- faraday.response :json
36
- faraday.response :logger, ::Logger.new($stdout), bodies: true if dev_developement?
37
- end.post(req_url.request_uri, args.to_query)
35
+ def patch(uri, args)
36
+ response = faraday_action(__method__, uri, args)
38
37
 
39
38
  parse_respond(response)
40
39
  end
41
40
 
42
41
  private
43
42
 
43
+ def faraday_action(method_name, uri, args)
44
+ req_url = URI(uri.start_with?("https") ? uri : (Office365::API_HOST + uri))
45
+ json_header = args.delete(:json_header)
46
+
47
+ faraday = Faraday.new(url: [req_url.scheme, "://", req_url.hostname].join, headers: json_header ? headers : post_headers) do |f|
48
+ f.adapter Faraday.default_adapter
49
+ f.response :json
50
+ f.response :logger, ::Logger.new($stdout), bodies: true if dev_developement?
51
+ end
52
+
53
+ faraday.send(method_name, req_url.request_uri, json_header ? args.to_json : args.ms_hash_to_query)
54
+ end
55
+
44
56
  def parse_respond(response)
45
57
  resp_body = response.body
46
58
 
47
- return resp_body if response.status == 200
59
+ return resp_body if [200, 201].include?(response.status)
48
60
 
49
61
  raise InvaliRequestError, resp_body["error_description"] if response.status == 400
50
62
  raise InvalidAuthenticationTokenError, resp_body.dig("error", "message") if response.status == 401
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Office365
4
+ module REST
5
+ module Subscription
6
+ def create_subscription(args = {})
7
+ raise ArgumentError, "Missing changeType" if args[:changeType].nil?
8
+ raise ArgumentError, "Missing notificationUrl" if args[:notificationUrl].nil?
9
+ raise ArgumentError, "Missing resource" if args[:resource].nil?
10
+ raise ArgumentError, "Missing expirationDateTime" if args[:expirationDateTime].nil?
11
+ raise ArgumentError, "Missing clientState" if args[:clientState].nil?
12
+
13
+ Models::Subscription.new(
14
+ Request.new(access_token, debug: debug).post("/v1.0/subscriptions", { json_header: true }.merge(args))
15
+ )
16
+ end
17
+
18
+ def renew_subscription(args = {})
19
+ raise ArgumentError, "Missing subscription identifier" if args[:identifier].nil?
20
+
21
+ identifier = args.delete(:identifier)
22
+
23
+ Models::Subscription.new(
24
+ Request.new(access_token, debug: debug).patch("/v1.0/subscriptions/#{identifier}", { json_header: true }.merge(args))
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
@@ -13,7 +13,7 @@ module Office365
13
13
  response_mode: "query",
14
14
  redirect_uri: redirect_uri,
15
15
  state: SecureRandom.hex
16
- }.to_query
16
+ }.ms_hash_to_query
17
17
 
18
18
  [base_uri, "?", azure_params].join
19
19
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cgi"
4
+
5
+ class Hash
6
+ def ms_hash_to_query
7
+ query_str = []
8
+
9
+ each_pair do |key, value|
10
+ query_str << "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
11
+ end
12
+ query_str.join("&")
13
+ end
14
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "office365/utils/string"
4
- require "office365/utils/to_query"
4
+ require "office365/utils/hash"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Office365
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.9"
5
5
  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.7
4
+ version: 0.1.9
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-11-11 00:00:00.000000000 Z
11
+ date: 2024-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -122,6 +122,7 @@ files:
122
122
  - lib/office365/models/event.rb
123
123
  - lib/office365/models/mailbox.rb
124
124
  - lib/office365/models/owner.rb
125
+ - lib/office365/models/subscription.rb
125
126
  - lib/office365/models/user.rb
126
127
  - lib/office365/rest.rb
127
128
  - lib/office365/rest/api.rb
@@ -132,11 +133,12 @@ files:
132
133
  - lib/office365/rest/event.rb
133
134
  - lib/office365/rest/mailbox.rb
134
135
  - lib/office365/rest/request.rb
136
+ - lib/office365/rest/subscription.rb
135
137
  - lib/office365/rest/token.rb
136
138
  - lib/office365/rest/user.rb
137
139
  - lib/office365/utils.rb
140
+ - lib/office365/utils/hash.rb
138
141
  - lib/office365/utils/string.rb
139
- - lib/office365/utils/to_query.rb
140
142
  - lib/office365/version.rb
141
143
  - ruby-office365.gemspec
142
144
  - sig/office365.rbs
@@ -163,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
165
  - !ruby/object:Gem::Version
164
166
  version: '0'
165
167
  requirements: []
166
- rubygems_version: 3.2.3
168
+ rubygems_version: 3.1.6
167
169
  signing_key:
168
170
  specification_version: 4
169
171
  summary: A simple ruby library to interact with Microsoft Graph and Office 365 API.
@@ -1,89 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "cgi"
4
-
5
- class Object
6
- # Alias of <tt>to_s</tt>.
7
- def to_param
8
- to_s
9
- end
10
-
11
- # Converts an object into a string suitable for use as a URL query string,
12
- # using the given <tt>key</tt> as the param name.
13
- def to_query(key)
14
- "$#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
15
- end
16
- end
17
-
18
- class NilClass
19
- # Returns +self+.
20
- def to_param
21
- self
22
- end
23
- end
24
-
25
- class TrueClass
26
- # Returns +self+.
27
- def to_param
28
- self
29
- end
30
- end
31
-
32
- class FalseClass
33
- # Returns +self+.
34
- def to_param
35
- self
36
- end
37
- end
38
-
39
- class Array
40
- # Calls <tt>to_param</tt> on all its elements and joins the result with
41
- # slashes. This is used by <tt>url_for</tt> in Action Pack.
42
- def to_param
43
- collect(&:to_param).join "/"
44
- end
45
-
46
- # Converts an array into a string suitable for use as a URL query string,
47
- # using the given +key+ as the param name.
48
- #
49
- # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
50
- def to_query(key)
51
- prefix = "#{key}[]"
52
-
53
- if empty?
54
- nil.to_query(prefix)
55
- else
56
- collect { |value| value.to_query(prefix) }.join "&"
57
- end
58
- end
59
- end
60
-
61
- class Hash
62
- # Returns a string representation of the receiver suitable for use as a URL
63
- # query string:
64
- #
65
- # {name: 'David', nationality: 'Danish'}.to_query
66
- # # => "name=David&nationality=Danish"
67
- #
68
- # An optional namespace can be passed to enclose key names:
69
- #
70
- # {name: 'David', nationality: 'Danish'}.to_query('user')
71
- # # => "user%5Bname%5D=David&user%5Bnationality%5D=Danish"
72
- #
73
- # The string pairs "key=value" that conform the query string
74
- # are sorted lexicographically in ascending order.
75
- #
76
- # This method is also aliased as +to_param+.
77
- def to_query(namespace = nil)
78
- query = collect do |key, value|
79
- unless (value.is_a?(Hash) || value.is_a?(Array)) && value.empty?
80
- value.to_query(namespace ? "#{namespace}[#{key}]" : key)
81
- end
82
- end.compact
83
-
84
- query.sort! unless namespace.to_s.include?("[]")
85
- query.join("&")
86
- end
87
-
88
- alias to_param to_query
89
- end