Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ RSpec/MultipleMemoizedHelpers:
Max: 10

Metrics/MethodLength:
Max: 12
Enabled: false

Metrics/ParameterLists:
Max: 8
Enabled: false

Metrics/CyclomaticComplexity:
Max: 9

Metrics/PerceivedComplexity:
Max: 9

123 changes: 99 additions & 24 deletions lib/ms_graph_rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@
require 'multi_json'

require_relative 'ms_graph_rest/version'
require_relative 'ms_graph_rest/mails'
require_relative 'ms_graph_rest/error'
require_relative 'ms_graph_rest/users'
require_relative 'ms_graph_rest/subscriptions'
require_relative 'ms_graph_rest/calendar_create_event'
require_relative 'ms_graph_rest/calendar_update_event'
require_relative 'ms_graph_rest/calendar_cancel_event'
require_relative 'ms_graph_rest/calendar_get_schedule'
require_relative 'ms_graph_rest/calendar_groups'
require_relative 'ms_graph_rest/calendar_view'
require_relative 'ms_graph_rest/calendars'
require_relative 'ms_graph_rest/calendar'
require_relative 'ms_graph_rest/find_event'
require_relative 'ms_graph_rest/online_meetings'

require_relative 'ms_graph_rest/error'
require_relative 'ms_graph_rest/find_rooms'
require_relative 'ms_graph_rest/groups'
require_relative 'ms_graph_rest/mails'
require_relative 'ms_graph_rest/messages'
require_relative 'ms_graph_rest/photos'
require_relative 'ms_graph_rest/groups'
require_relative 'ms_graph_rest/places'
require_relative 'ms_graph_rest/planner_tasks'
require_relative 'ms_graph_rest/todo_lists'
require_relative 'ms_graph_rest/subscriptions'
require_relative 'ms_graph_rest/todo_list_tasks'
require_relative 'ms_graph_rest/todo_lists'
require_relative 'ms_graph_rest/users'

class Faraday::FileReadAdapter < Faraday::Adapter
def self.folder=(val)
Expand Down Expand Up @@ -70,23 +82,24 @@ def self.fake_folder=(val)
end

class BaseConnection
attr_reader :access_token
attr_reader :access_token, :version

def initialize(access_token:)
def initialize(access_token:, version: 'v1.0')
@access_token = access_token.to_str.clone.freeze
@version = version
end
end

class FaradayConnection < BaseConnection
attr_reader :faraday_adapter

def initialize(access_token:, faraday_adapter:)
super(access_token: access_token)
def initialize(access_token:, faraday_adapter:, version: 'v1.0')
super(access_token: access_token, version: version)
@faraday_adapter = faraday_adapter
end

def conn
@conn ||= Faraday.new(url: 'https://graph.microsoft.com/v1.0/',
@conn ||= Faraday.new(url: "https://graph.microsoft.com/#{@version}/",
headers: { 'Content-Type' => 'application/json' }) do |c|
c.use Faraday::Response::RaiseError
c.authorization :Bearer, access_token
Expand All @@ -96,24 +109,28 @@ def conn
end
end

def get_raw(path, params)
conn.get(path, params)
def get_raw(path, params, headers = {})
conn.get(path, params, headers)
rescue Faraday::Error => e
raise MsGraphRest.wrap_request_error(e)
end

def get(path, params)
response = get_raw(path, params)
# @param consistencylevel [String] "eventual"
def get(path, params, consistencylevel: nil, headers: {})
if consistencylevel
headers["consistencylevel"] = consistencylevel
end
response = get_raw(path, params, headers)
parse_response(response)
end

def post(path, body)
response = conn.post(path, body.to_json)
def post(path, body, headers: {})
response = conn.post(path, body.to_json, headers)
parse_response(response)
end

def patch(path, body)
response = conn.patch(path, body.to_json)
def patch(path, body, headers: {})
response = conn.patch(path, body.to_json, headers)
parse_response(response)
end

Expand All @@ -124,7 +141,12 @@ def delete(path)
private

def parse_response(response)
MultiJson.load(response.body)
body = response.body
if body.empty?
true
else
MultiJson.load(response.body)
end
rescue MultiJson::ParseError => e
raise MsGraphRest::ParseError.new(e.message, response.body)
end
Expand All @@ -133,10 +155,12 @@ def parse_response(response)
class Client
attr_reader :connection

def initialize(access_token:, faraday_adapter: Faraday.default_adapter)
@connection = FaradayConnection.new(access_token: access_token, faraday_adapter: faraday_adapter)
def initialize(access_token:, faraday_adapter: Faraday.default_adapter, version: 'v1.0')
@connection = FaradayConnection.new(access_token: access_token, faraday_adapter: faraday_adapter,
version: version)
end

# @return Users
def users
Users.new(client: connection)
end
Expand All @@ -145,16 +169,67 @@ def subscriptions
Subscriptions.new(client: connection)
end

# @return MsGraphRest::Mails
def mails
Mails.new(client: connection)
end

# @return MsGraphRest::Photos
def photos
Photos.new(client: connection)
end

def calendar_view(path = '/me/calendar/')
CalendarView.new(path, client: connection)
def find_event
FindEvent.new(client: connection)
end

# @return MsGraphRest::CalendarView
def calendar_view
CalendarView.new(client: connection)
end

# @return MsGraphRest::CalendarGetSchedule
def calendar_get_schedule
CalendarGetSchedule.new(client: connection)
end

# @return MsGraphRest::OnlineMeetings
def online_meetings
OnlineMeetings.new(client: connection)
end

# @return MsGraphRest::CalendarCreateEvent
def calendar_create_event
CalendarCreateEvent.new(client: connection)
end

# @return MsGraphRest::CalendarUpdateEvent
def calendar_update_event
CalendarUpdateEvent.new(client: connection)
end

# @return MsGraphRest::CalendarCancelEvent
def calendar_cancel_event
CalendarCancelEvent.new(client: connection)
end

# @return MsGraphRest::CalendarCreateEvent
def calendar_groups
CalendarGroups.new(client: connection)
end

# @return MsGraphRest::CalendarCreateEvent
def calendars
Calendars.new(client: connection)
end

def calendar
Calendar.new(client: connection)
end

# @return MsGraphRest::Places
def places
Places.new(client: connection)
end

def messages(path = 'me')
Expand Down
31 changes: 31 additions & 0 deletions lib/ms_graph_rest/calendar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'camel_snake_struct'

module MsGraphRest
class Calendar < ChainableAction
class Response < ResponseWithPagination
end
Response.example('value' => nil, "@odata.context" => "", "@odata.nextLink" => "")

attr_reader :client, :path, :query

# rubocop:disable Lint/MissingSuper
def initialize(client:, query: {})
@client = client
@query = query
end
# rubocop:enable Lint/MissingSuper

def get(calendar_id:, user_id: nil, calendar_group: nil)
path =
case [user_id.nil?, calendar_group.nil?]
when [true, true] then "me/calendars/#{calendar_id}"
when [false, true] then "users/#{user_id}/calendars/#{calendar_id}"
when [true, false] then "me/calendarGroups/#{calendar_group}/calendars/#{calendar_id}"
when [false, false] then "users/#{user_id}/calendarGroups/#{calendar_group}/calendars/#{calendar_id}"
end

Response.new(client.get(path, {}))
end
end
end

41 changes: 41 additions & 0 deletions lib/ms_graph_rest/calendar_cancel_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module MsGraphRest
class CalendarCancelEvent < ModifyingAction
Response.example("value" => [], "@odata.context" => "", "@odata.nextLink" => "")

# Issues getSchedule. If user_id is given, uses the
# /users/ID/calendar/getSchedule, otherwise me/calendar/getSchedule endpoint
# @return Response
# @param id [String] MSOffice Event ID
# @param user_id [String] Optional user id that is used for the request
# @param comment [String] Optional comment
# @param calendar_id [String] Optional calendar id
def cancel(id:, user_id: nil, comment: nil, calendar_id: nil)
# POST /me/events/{id}/cancel
# POST /users/{id | userPrincipalName}/events/{id}/cancel
# POST /groups/{id}/events/{id}/cancel
#
# POST /me/calendar/events/{id}/cancel
# POST /users/{id | userPrincipalName}/calendar/events/{id}/cancel
# POST /groups/{id}/calendar/events/{id}/cancel
#
# POST /me/calendars/{id}/events/{id}/cancel
# POST /users/{id | userPrincipalName}/calendars/{id}/events/{id}/cancel
#
# POST /me/calendarGroups/{id}/calendars/{id}/events/{id}/cancel
# POST /users/{id | userPrincipalName}/calendarGroups/{id}/calendars/{id}/events/{id}/cancel
path = case [user_id.present?, user_id.present?]
when [false, true]
"me/calendars/#{calendar_id}/events/#{id}/cancel"
when [true, false]
"users/#{user_id}/events/#{id}/cancel"
when [true, true]
"users/#{user_id}/calendars/#{calendar_id}/events/#{id}/cancel"
else
"me/events/#{id}/cancel"
end
body = { comment: comment }.compact

client.post(path, body)
end
end
end
83 changes: 83 additions & 0 deletions lib/ms_graph_rest/calendar_create_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require 'camel_snake_struct'
require_relative 'modifying_action'
require_relative 'response'

module MsGraphRest
class CalendarCreateEvent < ModifyingAction
Response.example("value" => [], "@odata.context" => "", "@odata.nextLink" => "")

# Issues getSchedule. If user_id is given, uses the
# /users/ID/calendar/getSchedule, otherwise me/calendar/getSchedule endpoint
# @return Response
# @param start_time [Time] From Date Time
# @param end_time [Time] To Date Time
# @param subject [String]
# @param body [String]
# @param content_type [String] HTML or TEXT
# @param user_id [String] Optional user id that is used for the request
# @param importance [String] normal low high
# @param all_day [Boolean]
# @param draft [Boolean]
# @param allow_new_time_proposals [Boolean]
# @param attendees [Array]
# @param show_as [String] busy, tentative
# @param location [Hash]
def create(
subject:,
body:,
start_time:,
end_time:,
location:,
attendees:,
allow_new_time_proposals:,
user_id: nil,
calendar_id: nil,
all_day: false,
draft: false,
show_as: 'busy',
sensitivity: 'normal',
importance: 'normal',
content_type: "HTML"
)
start_time = start_time.iso8601 if start_time.respond_to?(:iso8601)
end_time = end_time.iso8601 if end_time.respond_to?(:iso8601)

body = {
subject: subject,
body: {
content: body,
contentType: content_type,
},
sensitivity: sensitivity,
importance: importance,
start: {
dateTime: start_time,
timeZone: 'UTC'
},
location: location,
end: {
dateTime: end_time,
timeZone: 'UTC'
},
isAllDay: all_day,
showAs: show_as,
isDraft: draft,
allowNewTimeProposals: allow_new_time_proposals,
attendees: attendees,
}.compact

path = case [user_id.present?, calendar_id.present?]
when [true, true]
"users/#{user_id}/calendars/#{calendar_id}/events"
when [false, true]
"me/calendars/#{calendar_id}/events"
when [true, false]
"users/#{user_id}/events"
else
"me/events"
end

Response.new(client.post(path, body))
end
end
end
Loading