Skip to content
Open
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
17 changes: 12 additions & 5 deletions lib/microsoft/graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Graph
BODY_METHODS = %w[POST PUT PATCH].freeze
ALLOWED_METHODS = [*BODY_METHODS, "GET", "DELETE"].freeze

def initialize(token: nil, error_handler: method(:error_handler), version: "1.0")
def initialize(token: nil, error_handler: method(:error_handler), version: "v1.0")
@token = token
@parser = URI::Parser.new
@body_formatter = Microsoft::Graph::BodyFormatter.new
Expand All @@ -35,11 +35,11 @@ def delete(*); end
define_method(method.downcase) { |*args, **kwargs| call(*args, method: method, **kwargs) }
end

def call(endpoint, token: @token, method: "GET", headers: {}, params: nil, body: nil)
def call(endpoint, token: @token, method: "GET", headers: {}, params: nil, body: nil, debug_output: 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{^/}, "")}"))
url = URI.join(GRAPH_HOST, @parser.escape("#{@version}/#{endpoint.gsub(%r{^/}, "")}"))
headers = headers.merge(
Authorization: "Bearer #{token}",
Accept: "application/json"
Expand All @@ -52,7 +52,8 @@ def call(endpoint, token: @token, method: "GET", headers: {}, params: nil, body:
headers: headers,
query: params,
body: @body_formatter.call(body, method: method).to_json,
parser: InstanceParser
parser: InstanceParser,
debug_output: debug_output
)

case response.code
Expand Down Expand Up @@ -91,8 +92,14 @@ def initialize(message, response = nil, _code = nil)

class InstanceParser < HTTParty::Parser
def parse
return nil unless body
utf8_bom_in_body_encoding = UTF8_BOM.dup.force_encoding(@body.encoding)
@body.gsub!(/\A#{utf8_bom_in_body_encoding}/, "").force_encoding("UTF-8") if @body.start_with?(utf8_bom_in_body_encoding)
super
end

protected

def json
JSON.parse(body, object_class: JSONStruct)
end
end
Expand Down