learnworlds 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +28 -0
- data/CHANGELOG.md +8 -0
- data/CODE_OF_CONDUCT.md +85 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +70 -0
- data/LICENSE.txt +21 -0
- data/README.md +104 -0
- data/Rakefile +12 -0
- data/lib/learn_worlds/client.rb +41 -0
- data/lib/learn_worlds/collection.rb +23 -0
- data/lib/learn_worlds/configuration.rb +22 -0
- data/lib/learn_worlds/learn_worlds_error.rb +11 -0
- data/lib/learn_worlds/object.rb +18 -0
- data/lib/learn_worlds/objects/user.rb +4 -0
- data/lib/learn_worlds/resource.rb +56 -0
- data/lib/learn_worlds/resources/authentication_resource.rb +45 -0
- data/lib/learn_worlds/resources/user_resource.rb +28 -0
- data/lib/learn_worlds/version.rb +5 -0
- data/lib/learn_worlds.rb +33 -0
- data/sig/learnworlds.rbs +4 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: dec047b90f49e77a404d68f80a7622c5421d0183d3cc2d8e95fda932ca6c7690
|
4
|
+
data.tar.gz: 1861ccc8603b47775f8aac4ff1d7b49ae2b0fa201867c06a6e760d75b50e5095
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 04ee2d52a92ea6318e4eeb8c36ce25d340efe669f51fd54afb438ed833d6c90466689558a67a8241904fd4e70ff1689c67f34e7bcb76431392bfa9e86f9c35f1
|
7
|
+
data.tar.gz: 875ab74e4416b3ab4e8304508eb70730e81f3456d4b178cda93d925966ffe513ba6c8a2381dc60e773c082641eb44e4ece45e84a7cc6c6226214358c9490bb3f
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
NewCops: enable
|
4
|
+
|
5
|
+
Style/Documentation:
|
6
|
+
Enabled: false
|
7
|
+
|
8
|
+
Style/FrozenStringLiteralComment:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
Style/QuotedSymbols:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Style/StringLiterals:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Style/OpenStructUse:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Style/StringLiteralsInInterpolation:
|
21
|
+
Enabled: true
|
22
|
+
EnforcedStyle: double_quotes
|
23
|
+
|
24
|
+
Lint/SymbolConversion:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Layout/LineLength:
|
28
|
+
Max: 120
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at nuno_lopes@whitesmith.co
|
43
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
44
|
+
|
45
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
46
|
+
|
47
|
+
## Enforcement Guidelines
|
48
|
+
|
49
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
50
|
+
|
51
|
+
### 1. Correction
|
52
|
+
|
53
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
54
|
+
|
55
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
56
|
+
|
57
|
+
### 2. Warning
|
58
|
+
|
59
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
60
|
+
|
61
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
62
|
+
|
63
|
+
### 3. Temporary Ban
|
64
|
+
|
65
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
66
|
+
|
67
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
68
|
+
|
69
|
+
### 4. Permanent Ban
|
70
|
+
|
71
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
72
|
+
|
73
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
74
|
+
|
75
|
+
## Attribution
|
76
|
+
|
77
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
78
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
79
|
+
|
80
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
81
|
+
|
82
|
+
[homepage]: https://www.contributor-covenant.org
|
83
|
+
|
84
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
85
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
learnworlds (0.1.0)
|
5
|
+
faraday (~> 2.3)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.2)
|
11
|
+
coderay (1.1.3)
|
12
|
+
diff-lcs (1.5.0)
|
13
|
+
faraday (2.3.0)
|
14
|
+
faraday-net_http (~> 2.0)
|
15
|
+
ruby2_keywords (>= 0.0.4)
|
16
|
+
faraday-net_http (2.0.3)
|
17
|
+
json (2.6.2)
|
18
|
+
method_source (1.0.0)
|
19
|
+
parallel (1.22.1)
|
20
|
+
parser (3.1.2.0)
|
21
|
+
ast (~> 2.4.1)
|
22
|
+
pry (0.14.1)
|
23
|
+
coderay (~> 1.1)
|
24
|
+
method_source (~> 1.0)
|
25
|
+
rainbow (3.1.1)
|
26
|
+
rake (13.0.6)
|
27
|
+
regexp_parser (2.5.0)
|
28
|
+
rexml (3.2.5)
|
29
|
+
rspec (3.11.0)
|
30
|
+
rspec-core (~> 3.11.0)
|
31
|
+
rspec-expectations (~> 3.11.0)
|
32
|
+
rspec-mocks (~> 3.11.0)
|
33
|
+
rspec-core (3.11.0)
|
34
|
+
rspec-support (~> 3.11.0)
|
35
|
+
rspec-expectations (3.11.0)
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
37
|
+
rspec-support (~> 3.11.0)
|
38
|
+
rspec-mocks (3.11.1)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.11.0)
|
41
|
+
rspec-support (3.11.0)
|
42
|
+
rubocop (1.31.1)
|
43
|
+
json (~> 2.3)
|
44
|
+
parallel (~> 1.10)
|
45
|
+
parser (>= 3.1.0.0)
|
46
|
+
rainbow (>= 2.2.2, < 4.0)
|
47
|
+
regexp_parser (>= 1.8, < 3.0)
|
48
|
+
rexml (>= 3.2.5, < 4.0)
|
49
|
+
rubocop-ast (>= 1.18.0, < 2.0)
|
50
|
+
ruby-progressbar (~> 1.7)
|
51
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
52
|
+
rubocop-ast (1.18.0)
|
53
|
+
parser (>= 3.1.1.0)
|
54
|
+
ruby-progressbar (1.11.0)
|
55
|
+
ruby2_keywords (0.0.5)
|
56
|
+
unicode-display_width (2.2.0)
|
57
|
+
|
58
|
+
PLATFORMS
|
59
|
+
ruby
|
60
|
+
x86_64-darwin-19
|
61
|
+
|
62
|
+
DEPENDENCIES
|
63
|
+
learnworlds!
|
64
|
+
pry (~> 0.14.1)
|
65
|
+
rake (~> 13.0)
|
66
|
+
rspec (~> 3.0)
|
67
|
+
rubocop (~> 1.21)
|
68
|
+
|
69
|
+
BUNDLED WITH
|
70
|
+
2.3.11
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Nuno Correia
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
# Learnworlds
|
2
|
+
|
3
|
+
Wrapper for the Learn Worlds API
|
4
|
+
|
5
|
+
**IMPORTANT!:** This gem is still on a early state and not all features are built in yet. Feel free to add features or if you need our help please reach us at nuno_lopes@whitesmith.co
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
10
|
+
|
11
|
+
$ bundle add learnworlds
|
12
|
+
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
|
+
|
15
|
+
$ gem install learnworlds
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
### Authentication with Client Credentials grant
|
20
|
+
|
21
|
+
To authenticate with client credentials grant you need to
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
client = LearnWorlds::Client.new(
|
25
|
+
client_id: 'xxxxxxxxxxx',
|
26
|
+
client_secret: 'xxxxxxxxx',
|
27
|
+
base_url: 'https://yourschool.mylearnworlds.com'
|
28
|
+
)
|
29
|
+
|
30
|
+
client.authentication.authenticate('client_credentials')
|
31
|
+
|
32
|
+
```
|
33
|
+
|
34
|
+
If you already have the accesse token you can initiate the client in the following way
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
client = LearnWorlds::Client.new(
|
38
|
+
access_token: 'xxxxxxxx',
|
39
|
+
base_url: 'https://yourschool.mylearnworlds.com'
|
40
|
+
)
|
41
|
+
```
|
42
|
+
|
43
|
+
you can also save some lines of code if you set the folloing env vars `LEARN_WORLDS_CLIENT_ID`, `LEARN_WORLDS_CLIENT_SECRET`, `LEARN_WORLDS_BASE_URL`
|
44
|
+
|
45
|
+
Once that is done, you can initialize the client with
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
client = LearnWorlds::Client.new
|
49
|
+
```
|
50
|
+
|
51
|
+
Everytime you call `authenticate('client_credentials')` a new request will be made to Learn Worlds.
|
52
|
+
If you want to avoid that you can define custom getters and setters for the access token via the configuration.
|
53
|
+
|
54
|
+
You can use that to add logic to persist or get the access token on your side and you can also add some logic to verify if the access token is still valid.
|
55
|
+
|
56
|
+
For example:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
LearnWorlds.configure do |config|
|
60
|
+
|
61
|
+
# return nil if access_token is invalid and you want to proceed with the authentication process
|
62
|
+
config.retrieve_access_token_method = ->() { Rails.cache.fetch("learnworlds_access_token") }
|
63
|
+
|
64
|
+
config.persist_access_token_method = ->(access_token) { Rails.cache.write('learnworlds_access_token', access_token) } }
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
### User Resource
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
# lists all users
|
72
|
+
client.user.list
|
73
|
+
|
74
|
+
# creates a user
|
75
|
+
client.user.create(email: 'test@test.com', username: 'test')
|
76
|
+
|
77
|
+
# updates a user
|
78
|
+
client.user.update(user_id: 'user_id', username: 'updated_username')
|
79
|
+
|
80
|
+
# finds a user
|
81
|
+
client.user.find(user_id: 'user_id')
|
82
|
+
|
83
|
+
# enrolls a user on a course
|
84
|
+
client.user.enroll(user_id: 'user_id', product_id: 'course_id', product_type: 'course', price: 0)
|
85
|
+
```
|
86
|
+
|
87
|
+
|
88
|
+
## Development
|
89
|
+
|
90
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
91
|
+
|
92
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
93
|
+
|
94
|
+
## Contributing
|
95
|
+
|
96
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/whitesmith/learnworlds. 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/whitesmith/learnworlds/blob/main/CODE_OF_CONDUCT.md).
|
97
|
+
|
98
|
+
## License
|
99
|
+
|
100
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
101
|
+
|
102
|
+
## Code of Conduct
|
103
|
+
|
104
|
+
Everyone interacting in the Learnworlds project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/whitesmith/learnworlds/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require "faraday"
|
2
|
+
|
3
|
+
require_relative "learn_worlds_error"
|
4
|
+
require_relative "object"
|
5
|
+
|
6
|
+
module LearnWorlds
|
7
|
+
class Client
|
8
|
+
attr_reader :base_url, :client_id, :client_secret
|
9
|
+
attr_accessor :access_token
|
10
|
+
|
11
|
+
def initialize(client_id: nil, client_secret: nil, base_url: nil, access_token: nil)
|
12
|
+
@client_id = client_id || ENV.fetch('"LEARN_WORLDS_CLIENT_ID"')
|
13
|
+
@client_secret = client_secret || ENV.fetch('LEARN_WORLDS_CLIENT_SECRET')
|
14
|
+
@base_url = base_url || ENV.fetch('LEARN_WORLDS_BASE_URL')
|
15
|
+
@access_token = access_token
|
16
|
+
end
|
17
|
+
|
18
|
+
def user
|
19
|
+
UserResource.new(self)
|
20
|
+
end
|
21
|
+
|
22
|
+
def authentication
|
23
|
+
AuthenticationResource.new(self)
|
24
|
+
end
|
25
|
+
|
26
|
+
def connection
|
27
|
+
@connection ||= Faraday.new(base_url) do |conn|
|
28
|
+
conn.request :json
|
29
|
+
conn.response :json, content_type: "application/json"
|
30
|
+
conn.headers = required_headers
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def required_headers
|
35
|
+
headers = { 'Lw-Client': client_id }
|
36
|
+
headers.merge({ Authorization: "Bearer #{access_token}" }) if access_token
|
37
|
+
|
38
|
+
headers
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module LearnWorlds
|
2
|
+
class Collection
|
3
|
+
attr_reader :data, :total, :next_cursor, :prev_cursor
|
4
|
+
|
5
|
+
def self.from_response(response, key:, type:)
|
6
|
+
body = response.body
|
7
|
+
puts body.class
|
8
|
+
new(
|
9
|
+
data: body[key].map { |attrs| type.new(attrs) },
|
10
|
+
total: body.dig("meta", "totalItems"),
|
11
|
+
current_page: body.dig("meta", "page"),
|
12
|
+
total_pages: body.dig("meta", "totalPages").to_i
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(data:, total:, current_page:, total_pages:)
|
17
|
+
@data = data
|
18
|
+
@total = total
|
19
|
+
@current_page = current_page
|
20
|
+
@total_pages = total_pages
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module LearnWorlds
|
2
|
+
class Configuration
|
3
|
+
attr_reader :retrieve_access_token_method, :persist_access_token_method
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@retrieve_access_token_method = nil
|
7
|
+
@persist_access_token_method = nil
|
8
|
+
end
|
9
|
+
|
10
|
+
def retrieve_access_token_method=(lambda)
|
11
|
+
raise ArgumentError, "The key_name must be a lambda" unless lambda.is_a?(Proc)
|
12
|
+
|
13
|
+
@retrieve_access_token_method = lambda
|
14
|
+
end
|
15
|
+
|
16
|
+
def persist_access_token_method=(lambda)
|
17
|
+
raise ArgumentError, "The key_name must be a lambda" unless lambda.is_a?(Proc)
|
18
|
+
|
19
|
+
@persist_access_token_method = lambda
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
3
|
+
module LearnWorlds
|
4
|
+
class Object
|
5
|
+
def initialize(attributes)
|
6
|
+
@attributes = OpenStruct.new(attributes)
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(method, *args, &block)
|
10
|
+
attribute = @attributes.send(method, *args, &block)
|
11
|
+
attribute.is_a?(Hash) ? Object.new(attribute) : attribute
|
12
|
+
end
|
13
|
+
|
14
|
+
def respond_to_missing?(_method, _include_private = false)
|
15
|
+
true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module LearnWorlds
|
2
|
+
class Resource
|
3
|
+
attr_reader :client
|
4
|
+
|
5
|
+
def initialize(client)
|
6
|
+
@client = client
|
7
|
+
end
|
8
|
+
|
9
|
+
def get_request(url, params = {}, headers = {})
|
10
|
+
handle_response client.connection.get(url, params, default_headers.merge(headers))
|
11
|
+
end
|
12
|
+
|
13
|
+
def post_request(url, body = {}, headers = {})
|
14
|
+
handle_response client.connection.post(url, body, default_headers.merge(headers))
|
15
|
+
end
|
16
|
+
|
17
|
+
def patch_request(url, body = {}, headers = {})
|
18
|
+
handle_response client.connection.patch(url, body, default_headers.merge(headers))
|
19
|
+
end
|
20
|
+
|
21
|
+
def put_request(url, body = {}, headers = {})
|
22
|
+
handle_response client.connection.put(url, body, default_headers.merge(headers))
|
23
|
+
end
|
24
|
+
|
25
|
+
def delete_request(url, params = {}, headers = {})
|
26
|
+
handle_response client.connection.delete(url, params, default_headers.merge(headers))
|
27
|
+
end
|
28
|
+
|
29
|
+
def handle_response(response)
|
30
|
+
response_body = response.body
|
31
|
+
response_body = JSON.parse(response_body) if response_body.instance_of?(String)
|
32
|
+
handle_errors(response_body)
|
33
|
+
|
34
|
+
response
|
35
|
+
end
|
36
|
+
|
37
|
+
def handle_errors(response_body)
|
38
|
+
error = response_body["errors"]&.first || {}
|
39
|
+
|
40
|
+
error_message = error["message"] || response_body["error"]
|
41
|
+
error_context = error["context"]
|
42
|
+
error_code = error["code"]
|
43
|
+
|
44
|
+
return if error_message.nil?
|
45
|
+
|
46
|
+
raise LearnWorldsError.new(message: error_message, context: error_context, code: error_code)
|
47
|
+
end
|
48
|
+
|
49
|
+
def default_headers
|
50
|
+
headers = { 'Lw-Client': client.client_id }
|
51
|
+
headers.merge!({ Authorization: "Bearer #{client.access_token}" }) if client.access_token
|
52
|
+
|
53
|
+
headers
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module LearnWorlds
|
2
|
+
class AuthenticationResource < Resource
|
3
|
+
AUTHENTICATE_ENDPOINT = "/admin/api/oauth2/access_token".freeze
|
4
|
+
CLIENT_CREDENTIALS_GRANT_TYPE = "client_credentials".freeze
|
5
|
+
|
6
|
+
def authenticate(method)
|
7
|
+
custom_access_token_retrieval
|
8
|
+
|
9
|
+
# everything went well don't need to continue
|
10
|
+
return if client.access_token
|
11
|
+
|
12
|
+
case method
|
13
|
+
when CLIENT_CREDENTIALS_GRANT_TYPE
|
14
|
+
authenticate_with_secret
|
15
|
+
end
|
16
|
+
|
17
|
+
# custom method to store the access token until it expires so we don't need to make uneccessary calls
|
18
|
+
LearnWorlds.configuration.persist_access_token_method&.call(client.access_token)
|
19
|
+
end
|
20
|
+
|
21
|
+
# custom retrieve method can be used if you store the access token on your side until it expires for example
|
22
|
+
def custom_access_token_retrieval
|
23
|
+
retrieve_access_token_method = LearnWorlds.configuration.retrieve_access_token_method
|
24
|
+
|
25
|
+
return unless retrieve_access_token_method
|
26
|
+
|
27
|
+
retrieved_access_token = retrieve_access_token_method.call
|
28
|
+
client.access_token = retrieved_access_token
|
29
|
+
end
|
30
|
+
|
31
|
+
def authenticate_with_secret
|
32
|
+
# get the access token using the api
|
33
|
+
response = post_request(
|
34
|
+
AUTHENTICATE_ENDPOINT,
|
35
|
+
{
|
36
|
+
client_id: client.client_id,
|
37
|
+
client_secret: client.client_secret,
|
38
|
+
grant_type: CLIENT_CREDENTIALS_GRANT_TYPE
|
39
|
+
}
|
40
|
+
)
|
41
|
+
response_body = Object.new(response.body)
|
42
|
+
client.access_token = response_body.tokenData.access_token
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module LearnWorlds
|
2
|
+
class UserResource < Resource
|
3
|
+
ENDPOINT = "/admin/api/v2/users".freeze
|
4
|
+
|
5
|
+
def list(**params)
|
6
|
+
response = get_request(ENDPOINT, params)
|
7
|
+
Collection.from_response(response, key: "data", type: User)
|
8
|
+
end
|
9
|
+
|
10
|
+
def create(**attributes)
|
11
|
+
User.new(post_request(ENDPOINT, attributes).body)
|
12
|
+
end
|
13
|
+
|
14
|
+
def find(user_id:)
|
15
|
+
User.new(get_request("#{ENDPOINT}/#{user_id}").body)
|
16
|
+
end
|
17
|
+
|
18
|
+
def update(user_id:, **params)
|
19
|
+
put_request("#{ENDPOINT}/#{user_id}", params)
|
20
|
+
true
|
21
|
+
end
|
22
|
+
|
23
|
+
def enroll(user_id:, **attributes)
|
24
|
+
post_request("#{ENDPOINT}/#{user_id}/enrollment", attributes)
|
25
|
+
true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/learn_worlds.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "learn_worlds/version"
|
4
|
+
|
5
|
+
module LearnWorlds
|
6
|
+
autoload :Users, "learn_worlds/users"
|
7
|
+
autoload :Object, "learn_worlds/object"
|
8
|
+
autoload :Client, "learn_worlds/client"
|
9
|
+
autoload :Collection, "learn_worlds/collection"
|
10
|
+
autoload :Configuration, "learn_worlds/configuration"
|
11
|
+
|
12
|
+
autoload :User, "learn_worlds/objects/user"
|
13
|
+
|
14
|
+
autoload :Resource, "learn_worlds/resource"
|
15
|
+
autoload :UserResource, "learn_worlds/resources/user_resource"
|
16
|
+
autoload :AuthenticationResource, "learn_worlds/resources/authentication_resource"
|
17
|
+
|
18
|
+
class << self
|
19
|
+
# Instantiate the Configuration singleton
|
20
|
+
# or return it.
|
21
|
+
def configuration
|
22
|
+
@configuration ||= Configuration.new
|
23
|
+
end
|
24
|
+
|
25
|
+
# This is the configure block definition.
|
26
|
+
# The configuration method will return the
|
27
|
+
# Configuration singleton, which is then yielded
|
28
|
+
# to the configure block.
|
29
|
+
def configure
|
30
|
+
yield(configuration)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/sig/learnworlds.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: learnworlds
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nuno Correia
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-07-01 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: '2.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.3'
|
27
|
+
description: A wrapper around LearnWorlds API
|
28
|
+
email:
|
29
|
+
- nuno_lopes@whitesmith.co
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".rspec"
|
35
|
+
- ".rubocop.yml"
|
36
|
+
- CHANGELOG.md
|
37
|
+
- CODE_OF_CONDUCT.md
|
38
|
+
- Gemfile
|
39
|
+
- Gemfile.lock
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- lib/learn_worlds.rb
|
44
|
+
- lib/learn_worlds/client.rb
|
45
|
+
- lib/learn_worlds/collection.rb
|
46
|
+
- lib/learn_worlds/configuration.rb
|
47
|
+
- lib/learn_worlds/learn_worlds_error.rb
|
48
|
+
- lib/learn_worlds/object.rb
|
49
|
+
- lib/learn_worlds/objects/user.rb
|
50
|
+
- lib/learn_worlds/resource.rb
|
51
|
+
- lib/learn_worlds/resources/authentication_resource.rb
|
52
|
+
- lib/learn_worlds/resources/user_resource.rb
|
53
|
+
- lib/learn_worlds/version.rb
|
54
|
+
- sig/learnworlds.rbs
|
55
|
+
homepage: https://github.com/whitesmith/learnworlds
|
56
|
+
licenses:
|
57
|
+
- MIT
|
58
|
+
metadata:
|
59
|
+
homepage_uri: https://github.com/whitesmith/learnworlds
|
60
|
+
source_code_uri: https://github.com/whitesmith/learnworlds
|
61
|
+
changelog_uri: https://github.com/whitesmith/learnworlds/blob/main/README.md
|
62
|
+
rubygems_mfa_required: 'true'
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: 2.6.0
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubygems_version: 3.1.2
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: A wrapper around LearnWorlds API
|
82
|
+
test_files: []
|