Skip to content

Commit 1850468

Browse files
author
Encore Shao
committed
fact: combine requests to get data with pagination
1 parent ba1840f commit 1850468

20 files changed

Lines changed: 427 additions & 136 deletions

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
- get mailbox `client.messages`
1212
- get calenders `client.calenders`
1313

14-
## [0.1.1] - (2022-10-21)
14+
## [0.1.2] - (2022-10-21)
1515

1616
- Integrate REST API to get mailbox with pagination
17-
- get mailbox data with next link `client.messages_with_nextlink`
17+
- get mailbox data with next link `client.messages({next_link: 'xxx'})`
18+
- get mailbox data with next link `client.calenders({next_link: 'xxx'})`
19+
20+
## [0.1.3] - (2022-10-26)
21+
22+
- Integrate REST API to get contacts
23+
- get profile `client.contacts`
24+
- get contacts data with next link `client.contacts({next_link: 'xxx'})`

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
ruby-office365 (0.1.2)
4+
ruby-office365 (0.1.3)
55
faraday
66
faraday_middleware
77

README.md

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,103 @@ end
3333

3434
After configuring a `client`, you can do the following things.
3535

36+
**response**
37+
38+
- `results`: wrap all data into results
39+
- `next_link`: get the new link for the next page of data
40+
41+
**to JSON**
42+
43+
- `as_json`: convert office365 object to JSON format
44+
3645
**Get Profile (as the authenticated user)**
3746

3847
```ruby
39-
client.me
48+
irb(main):004:0> response = client.me
49+
irb(main):010:0> response.display_name
50+
=> "Hello World"
51+
irb(main):004:0> response.as_json
52+
=> {
53+
:odata_context=>"https://graph.microsoft.com/v1.0/$metadata#users/$entity",
54+
:display_name=>"Hello World",
55+
:surname=>"Hello",
56+
:given_name=>"World",
57+
:id=>"d7e954e0b50095ad",
58+
:user_principal_name=>"hello.world@mail.com",
59+
:business_phones=>[],
60+
:job_title=>nil,
61+
:mail=>nil,
62+
:mobile_phone=>nil,
63+
:office_location=>nil,
64+
:preferred_language=>nil
65+
}
4066
```
4167

42-
**Get all calendars**
68+
**Get my calendars**
4369

4470
```ruby
45-
client.calendars
71+
irb(main):005:0> client.calendars
72+
irb(main):005:0> client.calendars[:results]
73+
irb(main):005:0> client.calendars[:next_link]
4674
```
4775

48-
**Get all mailbox**
76+
**Get my mails**
4977

5078
```ruby
51-
client.messages
52-
client.messages({ filter: "createdDateTime lt 2022-01-01" })
53-
client.messages_with_nextlink
54-
client.messages_with_nextlink(next_link: true, params: { filter: "createdDateTime lt 2022-01-01" })
79+
irb(main):005:0> client.messages
80+
irb(main):005:0> client.messages[:results]
81+
irb(main):005:0> client.messages({ filter: "createdDateTime lt 2022-01-01" })
82+
irb(main):005:0> client.messages({ filter: "createdDateTime lt 2022-01-01", next_link: 'https://....' })
5583
```
5684

57-
**Get all contact**
85+
**Get my contacts**
5886

5987
```ruby
60-
client.contacts
88+
irb(main):018:0> response = client.contacts
89+
irb(main):020:0> client.contacts[:results][0].display_name
90+
=> "Encore S."
91+
irb(main):018:0> response[:results][0].as_json
92+
=> {
93+
:odata_etag=>"W/\"EQAAABYAAACbUc86NQthQ7+Mvj19ecwVAABjabQj\"",
94+
:id=>"AQMkADAwATM3ZmYAZS00ZTU5LWY3NwBjLTAwAi0wMAoARgAAA4QFHqPHk4JJj7ZVaRPCKk4HAJtRzzo1C2FDv4y_PX15zBUAAAIBDgAAAJtRzzo1C2FDv4y_PX15zBUAAABja1I_AAAA",
95+
:created_date_time=>"2022-10-24T02:48:56Z",
96+
:last_modified_date_time=>"2022-10-24T02:48:57Z",
97+
:change_key=>"EQAAABYAAACbUc86NQthQ7+Mvj19ecwVAABjabQj",
98+
:categories=>[],
99+
:parent_folder_id=>"AQMkADAwATM3ZmYAZS00ZTU5LWY3NwBjLTAwAi0wMAoALgAAA4QFHqPHk4JJj7ZVaRPCKk4BAJtRzzo1C2FDv4y_PX15zBUAAAIBDgAAAA==",
100+
:birthday=>nil,
101+
:file_as=>"",
102+
:display_name=>"Name S.",
103+
:given_name=>"Name",
104+
:initials=>nil,
105+
:middle_name=>nil,
106+
:nick_name=>nil,
107+
:surname=>"S.",
108+
:title=>nil,
109+
:yomi_given_name=>nil,
110+
:yomi_surname=>nil,
111+
:yomi_company_name=>"",
112+
:generation=>nil,
113+
:im_addresses=>[],
114+
:job_title=>"",
115+
:company_name=>nil,
116+
:department=>"",
117+
:office_location=>"",
118+
:profession=>nil,
119+
:business_home_page=>"",
120+
:assistant_name=>"",
121+
:manager=>"",
122+
:home_phones=>[],
123+
:mobile_phone=>"",
124+
:business_phones=>[],
125+
:spouse_name=>"",
126+
:personal_notes=>"",
127+
:children=>[],
128+
:email_addresses=>[{"name"=>"name@google.com", "address"=>"name@google.com"}],
129+
:home_address=>{},
130+
:business_address=>{},
131+
:other_address=>{}
132+
}
61133
```
62134

63135
## Copyright

lib/office365/models.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ module Models
99
autoload :User, "office365/models/user"
1010
autoload :Owner, "office365/models/owner"
1111
autoload :EmailAddress, "office365/models/email_address"
12+
autoload :Contact, "office365/models/contact"
1213
end
1314
end

lib/office365/models/contact.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
module Office365
4+
module Models
5+
class Contact < Base
6+
end
7+
end
8+
end

lib/office365/rest/calendar.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
# frozen_string_literal: true
22

3+
require_relative "./concerns/base"
4+
35
module Office365
46
module REST
57
module Calendar
6-
def calendars
7-
calendars_uri = ["/", Office365::API_VERSION, "/me/calendars"].join
8-
response = Request.new(access_token, debug: debug).get(calendars_uri)
8+
include Concerns::Base
9+
10+
# params: args => { next_link: (nil / next_page_url) }
11+
# response { results: [], next_link: '...' }
12+
def calendars(args = {})
13+
response = message_response(args: args.merge(base_uri: "/me/calendars"))
914

10-
response["value"].map { |v| Models::Calendar.new(v) }
15+
{
16+
results: response["value"].map { |v| Models::Calendar.new(v) },
17+
next_link: response["@odata.nextLink"]
18+
}
1119
end
1220
end
1321
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
module Office365
4+
module REST
5+
module Concerns
6+
module Base
7+
private
8+
9+
def message_response(args: {})
10+
next_link = args.delete(:next_link)
11+
12+
request_uri = if next_link
13+
next_link
14+
else
15+
base_uri = args.delete(:base_uri)
16+
17+
req_uri = ["/", Office365::API_VERSION, base_uri].join
18+
req_uri += ["?", args.to_query].join if args.any?
19+
req_uri
20+
end
21+
22+
Request.new(access_token, debug: debug).get(request_uri)
23+
end
24+
end
25+
end
26+
end
27+
end

lib/office365/rest/contact.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
# frozen_string_literal: true
22

3+
require_relative "./concerns/base"
4+
35
module Office365
46
module REST
57
module Contact
6-
def contacts
7-
contacts_uri = ["/", Office365::API_VERSION, "/me/contacts"].join
8-
response = Request.new(access_token, debug: debug).get(contacts_uri)
8+
include Concerns::Base
9+
10+
# params: args => { next_link: (nil / next_page_url) }
11+
# response { results: [], next_link: '...' }
12+
def contacts(args = {})
13+
response = message_response(args: args.merge(base_uri: "/me/contacts"))
914

10-
response["value"].map { |v| Models::User.new(v) }
15+
{
16+
results: response["value"].map { |v| Models::Contact.new(v) },
17+
next_link: response["@odata.nextLink"]
18+
}
1119
end
1220
end
1321
end

lib/office365/rest/mailbox.rb

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,22 @@
11
# frozen_string_literal: true
22

3+
require_relative "./concerns/base"
4+
35
module Office365
46
module REST
57
module Mailbox
6-
def messages(params = {})
7-
message_response(params: params)["value"].map { |v| Models::Mailbox.new(v) }
8-
end
8+
include Concerns::Base
99

10-
# response = client.messages_with_nextlink
11-
# response[:results].each -> data save
12-
# if response[:next_link].present? client.messages_with_nextlink(response[:next_link])
13-
def messages_with_nextlink(next_link: nil, params: {})
14-
response = message_response(next_link: next_link, params: params)
10+
# params: args => { next_link: (nil / next_page_url) }
11+
# response { results: [], next_link: '...' }
12+
def messages(args = {})
13+
response = message_response(args: args.merge(base_uri: "/me/messages"))
1514

1615
{
1716
results: response["value"].map { |v| Models::Mailbox.new(v) },
1817
next_link: response["@odata.nextLink"]
1918
}
2019
end
21-
22-
private
23-
24-
def message_response(params: {}, next_link: nil)
25-
messages_uri = ["/", Office365::API_VERSION, "/me/messages"].join
26-
if params.any?
27-
messages_uri = [messages_uri, "?", params.to_query].join
28-
elsif next_link
29-
messages_uri = next_link
30-
end
31-
32-
Request.new(access_token, debug: debug).get(messages_uri)
33-
end
3420
end
3521
end
3622
end

lib/office365/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Office365
4-
VERSION = "0.1.2"
4+
VERSION = "0.1.3"
55
end

0 commit comments

Comments
 (0)