Class: MSGraph::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/msgraph/client.rb,
lib/msgraph/client/version.rb

Overview

Simple client for Microsoft Graph.

Constant Summary collapse

VERSION =
'0.1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbolize_names: true) ⇒ Client

Create a new client object.

Parameters:

  • symbolize_names (Boolean) (defaults to: true)

    whether hash keys of API responses are symbolized.



18
19
20
# File 'lib/msgraph/client.rb', line 18

def initialize(symbolize_names: true)
  @symbolize_names = symbolize_names
end

Instance Attribute Details

#symbolize_namesBoolean

Returns whether hash keys of API responses are symbolized.

Returns:

  • (Boolean)

    whether hash keys of API responses are symbolized.



23
24
25
# File 'lib/msgraph/client.rb', line 23

def symbolize_names
  @symbolize_names
end

Instance Method Details

#request(command, path, token, data = nil, header = nil) ⇒ Object

Calls an API and returns the parsed response.

Parameters:

  • command (String, Symbol)

    the HTTP method used on the request.

  • path (String)

    the path of the calling endpoint, and query if necessary.

  • token (#to_s)

    an access token.

  • data (Object) (defaults to: nil)

    an object to be converted to JSON for the request body.

  • header (Hash) (defaults to: nil)

    optional headers.

Raises:



34
35
36
37
38
39
40
41
42
43
# 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