class MSGraph::Client

Simple client for Microsoft Graph.

Constants

BASE_URL
VERSION

Attributes

symbolize_names[RW]

@return [Boolean] whether hash keys of API responses are symbolized.

Public Class Methods

new(symbolize_names: true) click to toggle source

Create a new client object.

@param [Boolean] symbolize_names whether hash keys of API responses are

symbolized.
# File lib/msgraph/client.rb, line 18
def initialize(symbolize_names: true)
  @symbolize_names = symbolize_names
end

Public Instance Methods

request(command, path, token, data = nil, header = nil) click to toggle source

Calls an API and returns the parsed response.

@param [String, Symbol] command the HTTP method used on the request. @param [String] path the path of the calling endpoint, and query if

necessary.

@param [#to_s] token an access token. @param [Object] data an object to be converted to JSON for the request

body.

@param [Hash] header optional headers.

# File lib/msgraph/client.rb, line 34
def request(command, path, token, data = nil, header = nil)
  req = Net::HTTPGenericRequest.new(
    command.to_s.upcase, !data.nil?, true, url(path), header
  )
  res = http_request(req, token, data)
  raise Error, res unless res.is_a? Net::HTTPSuccess
  raise Error, res unless res.content_type&.start_with? 'application/json'

  JSON.parse(res.body, symbolize_names: @symbolize_names)
end

Private Instance Methods

http() click to toggle source
# File lib/msgraph/client.rb, line 66
def http
  @http ||= Net::HTTP.new(BASE_URL.host, BASE_URL.port).tap do |http|
    http.use_ssl = true
    http.min_version = OpenSSL::SSL::TLS1_2_VERSION
  end
end
http_request(request, token, data = nil) click to toggle source
# File lib/msgraph/client.rb, line 54
def http_request(request, token, data = nil)
  request['Authorization'] = "Bearer #{token}"
  request['Accept'] = 'application/json'
  if data
    request.content_type = 'application/json; charset=utf-8'
    body = JSON.generate(data)
  else
    body = nil
  end
  http.request(request, body)
end
url(path) click to toggle source
# File lib/msgraph/client.rb, line 50
def url(path)
  BASE_URL.merge(path)
end