# Platform::Sdk

This gem is intended to provide clients and models for accessing the Platform APIs.

[![Ruby Gem](https://github.com/StrongMind/platform-ruby-sdk/actions/workflows/gem-push.yml/badge.svg)](https://github.com/StrongMind/platform-ruby-sdk/actions/workflows/gem-push.yml)

## Installation

First [configure your GitHub credentials](#configure-github-credentials)

Install the gem and add to the application's Gemfile by executing:

```
gem "strongmind-platform-sdk", "~> 3.19.1"
```

If bundler is not being used to manage dependencies, install the gem by executing:

    $ gem install strongmind-platform-sdk

## Usage

TODO: Write usage instructions here

## Configure GitHub credentials

- Create a GitHub PAT (personal access token) with the following scopes:
`delete:packages, write:packages, repo`

- Edit (or create) a `~/.gem.credentials` file to include the following, replacing TOKEN with your PAT from GitHub
```
---
:github: Bearer TOKEN
```

- Edit (or create) a `~/.gemrc` file and add the following:
```
—
:backtrace: false
:bulk_threshold: 1000
:sources:
- https://rubygems.org/
- https://USERNAME:TOKEN@rubygems.pkg.github.com/StrongMind/
:update_sources: true
:verbose: true  
```

- You will also need to configure your bundler to use your GitHub credentials:
```$ bundle config set —-global rubygems.pkg.github.com USERNAME:TOKEN```

NOTE: If your username has an `@` symbol, edit your `~/.bundle/config` file and replace the `@` with `%40`
```
---
BUNDLE_RUBYGEMS__PKG__GITHUB__COM: "USERNAME:TOKEN"
```

## Build/Publish Gem

To publish the gem, update the `version.rb` file and push the update to the main branch.  The github actions will publish the gem automatically.

If you would like to publish the gem from your local machine for development purposes, run the following commands:

```
gem build platform-sdk.gemspec
gem push --key github --host https://rubygems.pkg.github.com/StrongMind platform-sdk-0.0.0.gem
```

## Development

After checking out the repo, run `bin/setup` to install dependencies.

Migrate the database for the dummy app:
```
cd spec/dummy
RAILS_ENV=test rails db:migrate
```

Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/StrongMind/platform-ruby-sdk. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/platform-ruby-sdk/blob/main/CODE_OF_CONDUCT.md).

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

## Code of Conduct

Everyone interacting in the Platform::Ruby::Sdk project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/platform-ruby-sdk/blob/main/CODE_OF_CONDUCT.md).

## Data Pipeline Concern

The `DataPipelineable` concern is a shared concern that can be included in models
automatically to send data to the Data Pipeline on creates, updates, and destroys.

### Installation
Ensure that `platform-sdk` is required in the application.

One way to do this is to require the SDK via the `Gemfile`:

```ruby
gem 'strongmind-platform-sdk', require: 'platform_sdk'
```

This provides the application access to the `DataPipelineable` concern and RSpec shared examples.

### Usage

To use the `DataPipelineable` concern in a model, include the concern in the model:

```ruby
class MyModel < ApplicationRecord
  include PlatformSdk::ActiveRecord::DataPipelineable
end
```

For the concern to function, the model must have both a `pipeline_noun` and a `pipeline_meta` class method that returns the noun and metadata of the model, respectively.

This can be done once by defining the method(s) in the ApplicationRecord class:

```ruby
class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  def self.pipeline_noun
    "StrongMind.Central.#{name}"
  end

  def self.pipeline_meta
    {
      "source": "strongmind-central"
    }
  end
end
```

Or it can be defined in each model:

```ruby
class MyModel < ApplicationRecord
  include PlatformSdk::ActiveRecord::DataPipelineable

  def self.pipeline_noun
    "StrongMind.Central.#{name}"
  end

  def self.pipeline_meta
    {
      "source": "strongmind-central"
    }
  end
end
```

You can override the following methods in the model to customize the noun data:

```ruby
class MyModel < ApplicationRecord
  include PlatformSdk::ActiveRecord::DataPipelineable

  def pipeline_excluded_attributes
    [:column1, :column2]
  end

  def pipeline_additional_attributes
    {
      "foo": "important data",
      "bar": "more important data"
    }
  end
end
```

### Configuration
You will need the following ENV variables set in your application to send data to the Data Pipeline (found in bitwarden):
* DATA_PIPELINE_HOST
* DATA_PIPELINE_USERNAME
* DATA_PIPELINE_PASSWORD


### Testing

To test the `DataPipelineable` concern for a model, include the common shared examples in the model's spec file:

```ruby
RSpec.describe MyModel, type: :model do
  describe 'sends to the Data Pipeline' do
    it_behaves_like 'DataPipelineable'
  end
end
```

Make sure to stub the call to the Data Pipeline in `rails_helper.rb`

```ruby
  config.before(:each) do
    allow_any_instance_of(PlatformSdk::DataPipeline::Client).to receive(:post)
  end
```


## Setting up IRB for API Client Testing
Require needed dependencies:
`require 'platform_sdk'`


### OneRoster API Client Initialization Example
Open IRB and initialize dependencies:
* IDENTITY_CLIENT_ID
* IDENTITY_CLIENT_SECRET
* IDENTITY_BASE_URL
* STRONGMIND_APP_BASE_URL
* ONEROSTER_BASE_URL
* ID_MAPPER_DOMAIN
* ID_MAPPER_TOKEN

Create Identity Auth Client: 
`auth_client = PlatformSdk::Identity::AuthClient.new(IDENTITY_BASE_URL, IDENTITY_CLIENT_ID, IDENTITY_CLIENT_SECRET)`

Create OneRoster Client:
`one_roster_client = PlatformSdk::OneRoster::Client.new(ONEROSTER_BASE_URL, auth_client)`

### Additional Client Initialization Examples
Create Aws Client:
`aws_client = PlatformSdk::Aws::SecretsManagerClient.new(access_key_id: 'ACCESS_KEY_ID', secret_access_key: 'SECRET_ACCESS_KEY', region: 'REGION')`

Create PowerSchool Client:
`power_school_client = PlatformSdk::PowerSchool::Client.new(base_url: 'BASE_URL', bearer_token: 'BEARER_TOKEN')`

Create IdMapper Client:
`id_mapper_client = PlatformSdk::IdMapper::Client.new(domain: 'ID_MAPPER_DOMAIN', token: 'SECRET_TOKEN')`

Create Pencil Spaces Client:
`pencil_spaces_client = PlatformSdk::PencilSpaces::Client.new('BASE_URL', 'BEARER_TOKEN')`

Create Bynder Client:
`bynder_client = PlatformSdk::Bynder::Client.new('https://strongmind.getbynder.com/', 'API_TOKEN')`