lean-microsoft-graph 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3a74cac3f33f72977f108ebd6df574cf7dfb8098dbd351d15dac1db3eafd138f
4
- data.tar.gz: 01437d52cd8b78a5d685d1ed5bd95580633acedb266fd9f42d50a239e84b4409
3
+ metadata.gz: a6b711f8e892323120c00c966cad10124b1d5948fd2b14a884e22c2d78c8cb60
4
+ data.tar.gz: 3d6042393cd5105e4b353416b1d2eb81f6e9e9e36a4d60416a5e2338930b0606
5
5
  SHA512:
6
- metadata.gz: 6eeb0692b9e32266cb13b6bfe61fd967f2c3d6a2842f4f373b2e6b071f46537d98bfc32a8a4223ace841f0bb20eb77080221eef5c62ef41d2bb8e2cc25f4010c
7
- data.tar.gz: a7352b5942627897c0f99ead3fd6f165a6a0e65aaf506f003a20eae93e2675399b0edc16165e9a0d3a7bc73fee9ea61b597c866acd2205a1be7dfe22c674b3d2
6
+ metadata.gz: 52242da5380b066fbc762a1ba637ac05e007264a8a2c249f9efab38db0e3e2577a274e8e43e04d631146caf3c6c1d00f13de111a9884a277818539542c746dcb
7
+ data.tar.gz: 75f7376697cf08b5af7e771b8af49a488a6dfd80ef70e5b92a6698acea26f4518147feea4838e40ad491bc7f39bc54c5cb83a4d15369f1238c081ab0290c24cf
data/CHANGELOG.md CHANGED
@@ -1,5 +1,38 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
6
+
7
+ ## [0.2.1] - 2023-11-09
8
+
9
+ ### Added
10
+
11
+ - Missing tests for resource class.
12
+ - Github action to run tests on push and pull requests.
13
+ - Github action to publish new versions to Rubygems.
14
+
15
+ ### Fixed
16
+
17
+ - Fix error message parser when handling responses.
18
+
19
+ ### Removed
20
+
21
+ - The .tool-versions file from repository. Now it is ignored.
22
+
23
+ ## [0.2.0] - 2023-11-04
24
+
25
+ ### Added
26
+
27
+ - Ability to count users with `client.users.count`.
28
+ - Ability to retrieve users per page with `client.users.get_all(per_page)`.
29
+ - Ability to retrieve users by reference with `client.users.get_all_by_reference('reference')`.
30
+
1
31
  ## [Unreleased]
2
32
 
3
33
  ## [0.1.0] - 2023-08-28
4
34
 
5
35
  - Initial release
36
+
37
+ [0.2.1]: https://github.com/betogrun/lean_microsoft_graph/releases/tag/v0.2.1
38
+ [0.2.0]: https://github.com/betogrun/lean_microsoft_graph/releases/tag/v0.2.0
data/Gemfile CHANGED
@@ -8,3 +8,5 @@ gemspec
8
8
  gem "rake", "~> 13.0"
9
9
 
10
10
  gem "minitest", "~> 5.0"
11
+
12
+ gem 'mocha', '~> 2.1'
data/Gemfile.lock ADDED
@@ -0,0 +1,54 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lean-microsoft-graph (0.2.1)
5
+ faraday (~> 1.10)
6
+ faraday_middleware (~> 1.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ faraday (1.10.3)
12
+ faraday-em_http (~> 1.0)
13
+ faraday-em_synchrony (~> 1.0)
14
+ faraday-excon (~> 1.1)
15
+ faraday-httpclient (~> 1.0)
16
+ faraday-multipart (~> 1.0)
17
+ faraday-net_http (~> 1.0)
18
+ faraday-net_http_persistent (~> 1.0)
19
+ faraday-patron (~> 1.0)
20
+ faraday-rack (~> 1.0)
21
+ faraday-retry (~> 1.0)
22
+ ruby2_keywords (>= 0.0.4)
23
+ faraday-em_http (1.0.0)
24
+ faraday-em_synchrony (1.0.0)
25
+ faraday-excon (1.1.0)
26
+ faraday-httpclient (1.0.1)
27
+ faraday-multipart (1.0.4)
28
+ multipart-post (~> 2)
29
+ faraday-net_http (1.0.1)
30
+ faraday-net_http_persistent (1.2.0)
31
+ faraday-patron (1.0.0)
32
+ faraday-rack (1.0.0)
33
+ faraday-retry (1.0.3)
34
+ faraday_middleware (1.2.0)
35
+ faraday (~> 1.0)
36
+ minitest (5.20.0)
37
+ mocha (2.1.0)
38
+ ruby2_keywords (>= 0.0.5)
39
+ multipart-post (2.3.0)
40
+ rake (13.0.6)
41
+ ruby2_keywords (0.0.5)
42
+
43
+ PLATFORMS
44
+ x86_64-darwin-21
45
+ x86_64-linux
46
+
47
+ DEPENDENCIES
48
+ lean-microsoft-graph!
49
+ minitest (~> 5.0)
50
+ mocha (~> 2.1)
51
+ rake (~> 13.0)
52
+
53
+ BUNDLED WITH
54
+ 2.2.33
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # Lean::Microsoft::Graph
1
+ # LeanMicrosoftGraph
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/lean/microsoft/graph`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Lean Microsoft Graph is a lightweight gem designed for Ruby developers who need to interact with the Microsoft Graph API but are constrained by the use of Faraday version 1. This gem bridges this gap, providing an interface with Microsoft Graph API.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,37 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ To start using the LeanMicrosoftGraph gem, you need to create a client instance by providing the tenant_id, client_id, and client_secret.
24
+
25
+ ### Initialize the client
26
+
27
+ ```ruby
28
+ client = LeanMicrosoftGraph::Client.new(
29
+ tenant_id: 'your_tenant_id',
30
+ client_id: 'your_client_id',
31
+ client_secret: 'your_client_secret'
32
+ )
33
+ ```
34
+
35
+ Replace 'your_tenant_id', 'your_client_id', and 'your_client_secret' with your actual Microsoft Azure credentials.
36
+
37
+ ### Count the number of users
38
+
39
+ ```ruby
40
+ count = client.users.count
41
+ puts "Number of users: #{count}"
42
+ ```
43
+
44
+ ### Retrieving all users
45
+
46
+ ```ruby
47
+ per_page = 10 # Set the number of users you want per page
48
+ users = client.users.get_all(per_page)
49
+
50
+ users.each do |user|
51
+ puts "User ID: #{user.id}"
52
+ end
53
+ ```
26
54
 
27
55
  ## Development
28
56
 
data/bin/console CHANGED
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require "bundler/setup"
5
- require "lean/microsoft/graph"
5
+ require "lean_microsoft_graph"
6
6
 
7
7
  # You can add fixtures and/or initialization code here to make experimenting
8
8
  # with your gem easier. You can also use a different console, if you like.
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LeanMicrosoftGraph
4
+ module Authentication
5
+ class Credentials
6
+ attr_reader :tenant_id, :client_id, :client_secret
7
+
8
+ def initialize(tenant_id, client_id, client_secret)
9
+ @tenant_id = tenant_id
10
+ @client_id = client_id
11
+ @client_secret = client_secret
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LeanMicrosoftGraph
4
+ module Authentication
5
+ class TokenRetriever
6
+ def initialize(token_resource)
7
+ @token_resource = token_resource
8
+ @token = nil
9
+ end
10
+
11
+ def to_proc
12
+ -> { retrieve }
13
+ end
14
+
15
+ def retrieve
16
+ return @token if @token && !@token.expired?
17
+
18
+ @token = @token_resource.renew
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LeanMicrosoftGraph
4
+ class Client
5
+ def initialize(tenant_id:, client_id:, client_secret:, adapter: Faraday.default_adapter, stubs: nil)
6
+ @tenant_id = tenant_id
7
+ @client_id = client_id
8
+ @client_secret = client_secret
9
+ @adapter = adapter
10
+ @stubs = stubs # Stubs for testing purpouses
11
+ end
12
+
13
+ def users
14
+ Resources::UsersResource.new(connection)
15
+ end
16
+
17
+ private
18
+
19
+ def connection
20
+ @connection ||= Faraday.new(url: 'https://graph.microsoft.com/v1.0') do |faraday|
21
+ faraday.request :authorization, 'Bearer', proc(&token_retriever)
22
+ faraday.request :url_encoded
23
+ faraday.request :retry, retry_statuses: [429]
24
+ faraday.adapter @adapter, @stubs
25
+ end
26
+ end
27
+
28
+ def token_retriever
29
+ @token_retriever ||= begin
30
+ token_resource = Resources::TokenResource.new(credentials, @adapter, @stubs)
31
+ Authentication::TokenRetriever.new(token_resource)
32
+ end
33
+ end
34
+
35
+ def credentials
36
+ Authentication::Credentials.new(@tenant_id, @client_id, @client_secret)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LeanMicrosoftGraph
4
+ class Error < StandardError; end
5
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LeanMicrosoftGraph
4
+ class Resource
5
+ def initialize(connection)
6
+ @connection = connection
7
+ end
8
+
9
+ def get_request(url, params = {}, headers = {}, &block)
10
+ handle_response(@connection.get(url, params, headers, &block))
11
+ end
12
+
13
+ def post_request(url, body = {}, headers = {}, &block)
14
+ handle_response(@connection.post(url, body, headers, &block))
15
+ end
16
+
17
+ private
18
+
19
+ def handle_response(response)
20
+ parsed_response = JSON.parse(response.body, symbolize_names: true)
21
+ return parsed_response if (200..299).include?(response.status)
22
+
23
+ error_message, error_code = parse_error(parsed_response)
24
+
25
+ case { status: response.status, code: error_code, message: error_message }
26
+ in { status: 400, code: code, message: message }
27
+ raise Error, "Bad request. Code: #{code}. Message: #{message}"
28
+ in { status: 401, code: code, message: message }
29
+ raise Error, "Unauthorized. Check permissions or authentication token. Code: #{code}. Message: #{message}"
30
+ in { status: 403, code: code, message: message }
31
+ raise Error, "Forbidden. Insufficient permissions. Code: #{code}. Message: #{message}"
32
+ in { status: 404, code: code, message: message }
33
+ raise Error, "Resource not found. Code: #{code}. Message: #{message}"
34
+ in { status: 429, code: code, message: message }
35
+ raise Error, "Too many requests. Rate limit exceeded. Code: #{code}. Message: #{message}"
36
+ in { status: 500, code: code, message: message }
37
+ raise Error, "Internal server error. Code: #{code}. Message: #{message}"
38
+ in { status: 503, code: code, message: message }
39
+ raise Error, "Service unavailable. Try again later. Code: #{code}. Message: #{message}"
40
+ else
41
+ raise Error, "An unknown error occurred with status: #{response.status}."
42
+ end
43
+ end
44
+
45
+ def parse_error(parsed_response)
46
+ if parsed_response[:error].is_a?(Hash)
47
+ error_code = parsed_response[:error][:code]
48
+ error_message = parsed_response[:error][:message]
49
+ else
50
+ error_code = parsed_response[:error_codes]
51
+ error_message = parsed_response[:error_description]
52
+ end
53
+ [error_message, error_code]
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LeanMicrosoftGraph
4
+ module Resources
5
+ class TokenResource
6
+ class Token
7
+ attr_reader :value, :expires_in
8
+
9
+ def initialize(value, expires_in)
10
+ @value = value
11
+ @expires_in = expires_in
12
+ @created_at = Time.now
13
+ end
14
+
15
+ def expires_at
16
+ @created_at + @expires_in
17
+ end
18
+
19
+ def expired?
20
+ Time.now > expires_at
21
+ end
22
+
23
+ def to_s
24
+ @value
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LeanMicrosoftGraph
4
+ module Resources
5
+ class TokenResource < ::LeanMicrosoftGraph::Resource
6
+ def initialize(credentials, adapter = Faraday.default_adapter, stubs = nil)
7
+ super(Faraday.new { |faraday| faraday.adapter(adapter, stubs)})
8
+ @credentials = credentials
9
+ end
10
+
11
+ def renew
12
+ url = "https://login.microsoftonline.com/#{@credentials.tenant_id}/oauth2/v2.0/token"
13
+ headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
14
+ body = {
15
+ 'client_id' => @credentials.client_id,
16
+ 'scope' => 'https://graph.microsoft.com/.default',
17
+ 'client_secret' => @credentials.client_secret,
18
+ 'grant_type' => 'client_credentials'
19
+ }
20
+ response = post_request(url, URI.encode_www_form(body), headers)
21
+
22
+ Token.new(response[:access_token], response[:expires_in].to_i)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LeanMicrosoftGraph
4
+ module Resources
5
+ class UsersResource
6
+ class User
7
+ def initialize(response)
8
+ @response = response
9
+ end
10
+
11
+ def id = @response[:id]
12
+
13
+ def display_name = @response[:displayName]
14
+
15
+ def mail = @response[:mail]
16
+
17
+ def user_principal_name = @response[:userPrincipalName]
18
+
19
+ def job_title = @response[:jobTitle]
20
+
21
+ def mobile_phone = @response[:mobilePhone]
22
+
23
+ def business_phones = @response[:businessPhones]
24
+
25
+ def given_name = @response[:givenName]
26
+
27
+ def surname = @response[:surname]
28
+
29
+ def preferred_language = @response[:preferredLanguage]
30
+
31
+ def office_location = @response[:officeLocation]
32
+
33
+ def to_h
34
+ {
35
+ id: id,
36
+ display_name: display_name,
37
+ mail: mail,
38
+ user_principal_name: user_principal_name,
39
+ job_title: job_title,
40
+ mobile_phone: mobile_phone,
41
+ business_phones: business_phones,
42
+ given_name: given_name,
43
+ surname: surname,
44
+ preferred_language: preferred_language,
45
+ office_location: office_location
46
+ }
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LeanMicrosoftGraph
4
+ module Resources
5
+ class UsersResource
6
+ class Users
7
+ include Enumerable
8
+
9
+ def initialize(response)
10
+ @response = response
11
+ end
12
+
13
+ def each(&block)
14
+ collection.each(&block)
15
+ end
16
+
17
+ def next_batch_reference = @response[:'@odata.nextLink']
18
+
19
+ private
20
+
21
+ def collection
22
+ @collection ||= @response[:value].map { |user| User.new(user) }
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LeanMicrosoftGraph
4
+ module Resources
5
+ class UsersResource < ::LeanMicrosoftGraph::Resource
6
+ def count
7
+ get_request('users/$count') do |req|
8
+ req.headers['ConsistencyLevel'] = 'eventual'
9
+ end
10
+ end
11
+
12
+ def get_all(per_page)
13
+ response = get_request('users') do |req|
14
+ req.params['$top'] = per_page
15
+ end
16
+
17
+ Users.new(response)
18
+ end
19
+
20
+ def get_all_by_reference(url)
21
+ response = get_request(url)
22
+
23
+ Users.new(response)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LeanMicrosoftGraph
4
+ VERSION = "0.2.1"
5
+ end
@@ -0,0 +1,17 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'json'
4
+
5
+ module LeanMicrosoftGraph
6
+ require 'lean_microsoft_graph/version'
7
+ require 'lean_microsoft_graph/error'
8
+ require 'lean_microsoft_graph/resource'
9
+ require 'lean_microsoft_graph/client'
10
+ require 'lean_microsoft_graph/authentication/credentials'
11
+ require 'lean_microsoft_graph/authentication/token_retriever'
12
+ require 'lean_microsoft_graph/resources/token_resource'
13
+ require 'lean_microsoft_graph/resources/token_resource/token'
14
+ require 'lean_microsoft_graph/resources/users_resource'
15
+ require 'lean_microsoft_graph/resources/users_resource/user'
16
+ require 'lean_microsoft_graph/resources/users_resource/users'
17
+ end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lean-microsoft-graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alberto Rocha
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-28 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2023-11-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
13
41
  description: This gem provides a set of methods to interact with Microsoft Graph API.
14
42
  email:
15
43
  - betogrun@gmail.com
@@ -19,13 +47,24 @@ extra_rdoc_files: []
19
47
  files:
20
48
  - CHANGELOG.md
21
49
  - Gemfile
50
+ - Gemfile.lock
22
51
  - LICENSE.txt
23
52
  - README.md
24
53
  - Rakefile
25
54
  - bin/console
26
55
  - bin/setup
27
- - lib/lean/microsoft/graph.rb
28
- - lib/lean/microsoft/graph/version.rb
56
+ - lib/lean_microsoft_graph.rb
57
+ - lib/lean_microsoft_graph/authentication/credentials.rb
58
+ - lib/lean_microsoft_graph/authentication/token_retriever.rb
59
+ - lib/lean_microsoft_graph/client.rb
60
+ - lib/lean_microsoft_graph/error.rb
61
+ - lib/lean_microsoft_graph/resource.rb
62
+ - lib/lean_microsoft_graph/resources/token_resource.rb
63
+ - lib/lean_microsoft_graph/resources/token_resource/token.rb
64
+ - lib/lean_microsoft_graph/resources/users_resource.rb
65
+ - lib/lean_microsoft_graph/resources/users_resource/user.rb
66
+ - lib/lean_microsoft_graph/resources/users_resource/users.rb
67
+ - lib/lean_microsoft_graph/version.rb
29
68
  homepage: https://github.com/betogrun/lean-microsoft-graph
30
69
  licenses:
31
70
  - MIT
@@ -47,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
86
  - !ruby/object:Gem::Version
48
87
  version: '0'
49
88
  requirements: []
50
- rubygems_version: 3.1.4
89
+ rubygems_version: 3.2.33
51
90
  signing_key:
52
91
  specification_version: 4
53
92
  summary: "'lean-microsoft-graph' is a gem is a simple interface to Microsoft Graph
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Lean
4
- module Microsoft
5
- module Graph
6
- VERSION = "0.1.0"
7
- end
8
- end
9
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "graph/version"
4
-
5
- module Lean
6
- module Microsoft
7
- module Graph
8
- class Error < StandardError; end
9
- # Your code goes here...
10
- end
11
- end
12
- end