class Microsoft::Graph

Constants

ALLOWED_METHODS
BODY_METHODS
GRAPH_HOST
VERSION

Public Class Methods

new(token: nil, error_handler: method(:error_handler), version: "1.0") click to toggle source
# 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

Public Instance Methods

batch(token: @token) { |batch| ... } click to toggle source
# 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) click to toggle source
# 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
delete(*) click to toggle source
# File lib/microsoft/graph.rb, line 32
def delete(*); end
error_handler(error) click to toggle source
# File lib/microsoft/graph.rb, line 79
def error_handler(error)
  raise error
end
get(*) click to toggle source

stubs

# File lib/microsoft/graph.rb, line 24
def get(*); end
patch(*) click to toggle source
# File lib/microsoft/graph.rb, line 30
def patch(*); end
post(*) click to toggle source
# File lib/microsoft/graph.rb, line 26
def post(*); end
put(*) click to toggle source
# File lib/microsoft/graph.rb, line 28
def put(*); end