Class: M365ActiveStorage::M365

Inherits:
Object
  • Object
show all
Defined in:
lib/m365_active_storage/m365.rb

Overview

M365 SharePoint Interface

Provides high-level interface for interacting with Microsoft 365 SharePoint services. This class handles configuration loading, authentication, and basic SharePoint operations.

Responsibilities

  • Load and manage SharePoint configuration from config/storage.yml

  • Establish authenticated connections to Microsoft Graph API

  • Provide methods for site and drive discovery

  • Validate SharePoint endpoint connectivity

Example Usage

m365 = M365ActiveStorage::M365.new
site_id, drive_id = m365.config_site_and_drive_id

site = m365.find_site_by_id(site_id)
drive = m365.find_sharepoint_drive_by_id(site_id, drive_id)

if m365.ping?
  puts "SharePoint connectivity is healthy"
end

Configuration

Configuration is loaded from config/storage.yml. Required parameters:

  • ms_graph_url: The Microsoft Graph API base URL

  • ms_graph_version: The Graph API version (e.g., “v1.0”)

  • auth_host: The OAuth2 authentication endpoint

  • oauth_tenant: Your Azure AD tenant ID

  • oauth_app_id: Your Azure AD application ID

  • oauth_secret: Your Azure AD client secret

  • sharepoint_site_id: The target SharePoint site ID

  • sharepoint_drive_id: The target SharePoint drive ID

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeM365

Initialize the M365 interface and load configuration

Loads configuration from config/storage.yml and establishes authentication and HTTP handler instances.

Raises:

  • (KeyError)

    if required configuration parameters are missing

  • (RuntimeError)

    if storage.yml is not found



58
59
60
61
62
63
64
# File 'lib/m365_active_storage/m365.rb', line 58

def initialize
  @config = Configuration.new(**M365.load_configuration_from_storage_yml)
  @auth = Authentication.new(@config)
  @http = Http.new(@auth)
rescue KeyError => e
  raise e.message
end

Class Method Details

.load_configuration_from_storage_ymlHash

Load configuration from config/storage.yml file

Reads and parses the Rails storage configuration file, evaluating any ERB templates and extracting the SharePoint-specific configuration.

Returns:

  • (Hash)

    The SharePoint configuration parameters

Raises:

  • (RuntimeError)

    if storage.yml does not exist

  • (RuntimeError)

    if storage.yml does not contain a “sharepoint” key

  • (KeyError)

    if required configuration parameters are missing

See Also:



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/m365_active_storage/m365.rb', line 149

def self.load_configuration_from_storage_yml
  storage_yml = Rails.root.join("config", "storage.yml")
  raise "Missing storage.yml configuration file" unless File.exist?(storage_yml)

  erb_template = ERB.new(File.read(storage_yml))
  yaml_content = erb_template.result(binding)
  configuration = YAML.safe_load(yaml_content, permitted_classes: [Regexp], aliases: true)
  raise "Invalid sharepoint storage.yml configuration" unless configuration&.key?("sharepoint")

  configuration["sharepoint"]
end

Instance Method Details

#config_site_and_drive_idArray<String>

Returns the configured site ID and drive ID as an array

Examples:

site_id, drive_id = m365.config_site_and_drive_id

Returns:

  • (Array<String>)

    A two-element array containing [site_id, drive_id]



71
72
73
# File 'lib/m365_active_storage/m365.rb', line 71

def config_site_and_drive_id
  [config.site_id, config.drive_id]
end

#find_sharepoint_drive_by_id(site_id, drive_id) ⇒ Hash?

Find a SharePoint drive within a specific site

Retrieves information about a drive associated with a SharePoint site. Requires valid authentication token and valid site and drive IDs.

Examples:

drive = m365.find_sharepoint_drive_by_id("site123", "drive456")
if drive
  puts "Drive: #{drive['name']}"
end

Parameters:

  • site_id (String)

    The SharePoint site ID

  • drive_id (String)

    The SharePoint drive ID within that site

Returns:

  • (Hash, nil)

    A hash containing drive information, or nil if the drive is not found

Raises:

  • (StandardError)

    if authentication fails



111
112
113
114
115
116
117
118
# File 'lib/m365_active_storage/m365.rb', line 111

def find_sharepoint_drive_by_id(site_id, drive_id)
  auth.ensure_valid_token
  url = "#{auth.config.ms_graph_endpoint}/sites/#{site_id}/drives/#{drive_id}"
  response = http.get(url)
  return nil unless response.code.to_i == 200

  JSON.parse(response.body)
end

#find_site_by_id(site_id) ⇒ Hash?

Find a SharePoint site by its ID

Queries the Microsoft Graph API to retrieve information about a specific SharePoint site. Requires valid authentication token.

Examples:

site = m365.find_site_by_id("site123")
if site
  puts "Site: #{site['displayName']}"
end

Parameters:

  • site_id (String)

    The SharePoint site ID to retrieve

Returns:

  • (Hash, nil)

    A hash containing site information, or nil if the site is not found

Raises:

  • (StandardError)

    if authentication fails



88
89
90
91
92
93
94
95
# File 'lib/m365_active_storage/m365.rb', line 88

def find_site_by_id(site_id)
  auth.ensure_valid_token
  url = "#{auth.config.ms_graph_endpoint}/sites/#{site_id}"
  response = http.get(url)
  return nil unless response.code.to_i == 200

  JSON.parse(response.body)
end

#ping?Boolean

Check SharePoint endpoint connectivity and configuration validity

Performs a simple health check by querying the SharePoint sites endpoint. This validates that authentication is working and the SharePoint service is accessible.

Examples:

if m365.ping?
  puts "SharePoint is accessible"
else
  puts "Cannot reach SharePoint"
end

Returns:

  • (Boolean)

    true if the endpoint is accessible, false otherwise



132
133
134
135
136
137
# File 'lib/m365_active_storage/m365.rb', line 132

def ping?
  auth.ensure_valid_token
  url = "#{auth.config.ms_graph_endpoint}/sites/root/sites"
  response = http.get(url)
  response.code.to_i == 200
end