Class: Microsoft::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/microsoft/graph.rb,
lib/microsoft/graph/batch.rb,
lib/microsoft/graph/version.rb,
lib/microsoft/graph/body_formatter.rb

Defined Under Namespace

Classes: Batch, BodyFormatter, Error, InstanceParser, JSONStruct

Constant Summary collapse

GRAPH_HOST =
"https://graph.microsoft.com"
BODY_METHODS =
%w[POST PUT PATCH].freeze
ALLOWED_METHODS =
[*BODY_METHODS, "GET", "DELETE"].freeze
VERSION =
"0.1.1"

Instance Method Summary collapse

Constructor Details

#initialize(token: nil, error_handler: method(:error_handler), version: "1.0") ⇒ Graph

Returns a new instance of Graph.



15
16
17
18
19
20
21
# File 'lib/microsoft/graph.rb', line 15

def initialize(token: nil, error_handler: method(:error_handler), version: "1.0")
  @token = token
  @parser = URI::Parser.new
  @body_formatter = Microsoft::Graph::BodyFormatter.new
  @error_handler = error_handler
  @version = version
end

Instance Method Details

#batch(token: @token) {|batch| ... } ⇒ Object

Yields:



73
74
75
76
77
# File 'lib/microsoft/graph.rb', line 73

def batch(token: @token)
  batch = Batch.new(self, token: token)
  yield batch
  batch.call
end

#call(endpoint, token: @token, method: "GET", headers: {}, params: nil, body: nil) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/microsoft/graph.rb', line 38

def call(endpoint, token: @token, method: "GET", headers: {}, params: nil, body: nil)
  method = method.upcase
  raise ArgumentError, "`#{method}` is not a valid HTTP method." unless ALLOWED_METHODS.include?(method)

  url = URI.join(GRAPH_HOST, @parser.escape("v#{@version}/#{endpoint.gsub(%r{^/}, "")}"))
  headers = headers.merge(
    Authorization: "Bearer #{token}",
    Accept: "application/json"
  )
  headers[:"Content-Type"] = "application/json" if BODY_METHODS.include?(method)

  response = HTTParty.send(
    method.downcase,
    url,
    headers: headers,
    query: params,
    body: @body_formatter.call(body, method: method).to_json,
    parser: InstanceParser
  )

  case response.code
  when 200...400
    response.parsed_response
  when 400...600
    error = Error.new(
      "Received status code: #{response.code}. Check the `response` attribute for more details.",
      response.parsed_response,
      response.code
    )
    @error_handler.call(error)
  else
    raise "Unknown status code: #{response.code}"
  end
end

#deleteObject



32
# File 'lib/microsoft/graph.rb', line 32

def delete(*); end

#error_handler(error) ⇒ Object



79
80
81
# File 'lib/microsoft/graph.rb', line 79

def error_handler(error)
  raise error
end

#getObject

stubs



24
# File 'lib/microsoft/graph.rb', line 24

def get(*); end

#patchObject



30
# File 'lib/microsoft/graph.rb', line 30

def patch(*); end

#postObject



26
# File 'lib/microsoft/graph.rb', line 26

def post(*); end

#putObject



28
# File 'lib/microsoft/graph.rb', line 28

def put(*); end