cognitive_faculty 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e2d252f53af2287701ad74a9c9037a337cd476a7c173c0512dbcf04308d92d34
4
+ data.tar.gz: ef1d662f15d69ed61a52fc960822b8c3c6482aadbee9ea67026203ef6dcf386e
5
+ SHA512:
6
+ metadata.gz: 72ad0ca29b84c36ff88c985681a4772608634885dd0bb70a0842236225ee39bfcb469cbdfe747dbc9b089dbcddb94c21f4660ee6c5111ffb3ae99fa7547fb396
7
+ data.tar.gz: 75f267074dabbf2f6613f15e887905a353c74d191d78947da90aa47df1618c75d3b169e0bafaaf15358d8964fb97bc87b138a8b9868eb9cc3a7c7d0c5d4213f4
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,9 @@
1
+ # Common configuration.
2
+ Layout/SpaceBeforeFirstArg:
3
+ Enabled: false
4
+
5
+ LineLength:
6
+ Max: 120
7
+
8
+ StringLiterals:
9
+ EnforcedStyle: double_quotes
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 1.17.3
5
+ before_install: gem install bundler -v 1.17.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cognitive_faculty.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,92 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cognitive_faculty (1.1.0)
5
+ activemodel (= 5.0.0)
6
+ activesupport (= 5.0.0)
7
+ faraday_middleware (~> 0.12.2)
8
+ her (~> 0.8.0)
9
+ oauth2 (~> 1.4.0)
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ activemodel (5.0.0)
15
+ activesupport (= 5.0.0)
16
+ activesupport (5.0.0)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (~> 0.7)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ ast (2.4.0)
22
+ concurrent-ruby (1.1.6)
23
+ diff-lcs (1.3)
24
+ faraday (0.17.3)
25
+ multipart-post (>= 1.2, < 3)
26
+ faraday_middleware (0.12.2)
27
+ faraday (>= 0.7.4, < 1.0)
28
+ her (0.8.6)
29
+ activemodel (>= 3.0.0, <= 6.0.0)
30
+ activesupport (>= 3.0.0, <= 6.0.0)
31
+ faraday (>= 0.8, < 1.0)
32
+ multi_json (~> 1.7)
33
+ i18n (0.9.5)
34
+ concurrent-ruby (~> 1.0)
35
+ jaro_winkler (1.5.4)
36
+ jwt (2.2.1)
37
+ minitest (5.14.0)
38
+ multi_json (1.14.1)
39
+ multi_xml (0.6.0)
40
+ multipart-post (2.1.1)
41
+ oauth2 (1.4.4)
42
+ faraday (>= 0.8, < 2.0)
43
+ jwt (>= 1.0, < 3.0)
44
+ multi_json (~> 1.3)
45
+ multi_xml (~> 0.5)
46
+ rack (>= 1.2, < 3)
47
+ parallel (1.19.1)
48
+ parser (2.7.1.1)
49
+ ast (~> 2.4.0)
50
+ psych (3.1.0)
51
+ rack (2.2.2)
52
+ rainbow (3.0.0)
53
+ rake (10.5.0)
54
+ rspec (3.9.0)
55
+ rspec-core (~> 3.9.0)
56
+ rspec-expectations (~> 3.9.0)
57
+ rspec-mocks (~> 3.9.0)
58
+ rspec-core (3.9.1)
59
+ rspec-support (~> 3.9.1)
60
+ rspec-expectations (3.9.1)
61
+ diff-lcs (>= 1.2.0, < 2.0)
62
+ rspec-support (~> 3.9.0)
63
+ rspec-mocks (3.9.1)
64
+ diff-lcs (>= 1.2.0, < 2.0)
65
+ rspec-support (~> 3.9.0)
66
+ rspec-support (3.9.2)
67
+ rubocop (0.67.2)
68
+ jaro_winkler (~> 1.5.1)
69
+ parallel (~> 1.10)
70
+ parser (>= 2.5, != 2.5.1.1)
71
+ psych (>= 3.1.0)
72
+ rainbow (>= 2.2.2, < 4.0)
73
+ ruby-progressbar (~> 1.7)
74
+ unicode-display_width (>= 1.4.0, < 1.6)
75
+ ruby-progressbar (1.10.1)
76
+ thread_safe (0.3.6)
77
+ tzinfo (1.2.7)
78
+ thread_safe (~> 0.1)
79
+ unicode-display_width (1.5.0)
80
+
81
+ PLATFORMS
82
+ ruby
83
+
84
+ DEPENDENCIES
85
+ bundler (~> 1.17.3)
86
+ cognitive_faculty!
87
+ rake (~> 10.0)
88
+ rspec (~> 3.0)
89
+ rubocop (~> 0.67.0)
90
+
91
+ BUNDLED WITH
92
+ 1.17.3
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # CognitiveFaculty
2
+
3
+ A Ruby gem for accessing the Cognitive Faculty API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'cognitive_faculty'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install cognitive_faculty
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'cognitive_faculty'
25
+
26
+ CognitiveFaculty.configure do |c|
27
+ c.url = "https://platform.cognitivefaculty.com/api"
28
+ c.auth.url = "https://accounts.cognitivefaculty.com"
29
+ c.auth.client_id = "client id"
30
+ c.auth.client_secret = "secret"
31
+ c.auth.username = "username"
32
+ c.auth.password = "password"
33
+ end
34
+
35
+ CognitiveFaculty::Offer.all.each do |o|
36
+ puts o.name
37
+ end
38
+ ```
39
+
40
+ ## Development
41
+
42
+ 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.
43
+
44
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cognitive_faculty"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "cognitive_faculty/version"
4
+
5
+ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
6
+ spec.name = "cognitive_faculty"
7
+ spec.version = CognitiveFaculty::VERSION
8
+ spec.authors = ["Luiz Aoqui"]
9
+ spec.email = ["laoqui@ca.ibm.com"]
10
+
11
+ spec.summary = "Cognitive Faculty Ruby SDK"
12
+ spec.description = "Ruby gem for the Cognitive Faculty API."
13
+ spec.homepage = "https://cognitivefaculty.com"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.17.3"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "rubocop", "~> 0.67.0"
26
+
27
+ spec.add_dependency "activemodel", "= 5.0.0"
28
+ spec.add_dependency "activesupport", "= 5.0.0"
29
+ spec.add_dependency "faraday_middleware", "~> 0.12.2"
30
+ spec.add_dependency "her", "~> 0.8.0"
31
+ spec.add_dependency "oauth2", "~> 1.4.0"
32
+ end
@@ -0,0 +1,64 @@
1
+ require "her"
2
+ require "faraday"
3
+ require "faraday_middleware"
4
+
5
+ require "cognitive_faculty/version"
6
+ require "cognitive_faculty/configuration/configuration"
7
+
8
+ ##
9
+ # Cognitive Faculty API client for Ruby.
10
+ #
11
+ # Usage:
12
+ # CognitiveFaculty.configure do |c|
13
+ # c.url = "https://platform.cognitivefaculty.com/api"
14
+ # c.auth.url = "https://accounts.cognitivefaculty.com"
15
+ # c.auth.client_id = "client id"
16
+ # c.auth.client_secret = "secret"
17
+ # c.auth.username = "username"
18
+ # c.auth.password = "password"
19
+ # end
20
+ #
21
+ # CognitiveFaculty::Offer.all.each do |o|
22
+ # puts o.name
23
+ # end
24
+ #
25
+ module CognitiveFaculty
26
+ class << self
27
+ attr_accessor :configuration
28
+
29
+ private
30
+
31
+ def setup_her
32
+ api = Her::API.setup url: configuration.url do |c|
33
+ # Request
34
+ c.use AccessTokenMiddleware
35
+ c.use Faraday::Request::UrlEncoded
36
+
37
+ # Response
38
+ c.use Her::Middleware::DefaultParseJSON
39
+ c.use Faraday::Response::Logger, nil, body: true
40
+
41
+ # Adapter
42
+ c.use Faraday::Adapter::NetHttp
43
+ end
44
+ require "cognitive_faculty/models"
45
+ api
46
+ end
47
+
48
+ # Faraday middleware for adding the OIDC access token at each request.
49
+ # If the access token is expired, a new token will be requested.
50
+ class AccessTokenMiddleware < Faraday::Middleware
51
+ def call(env)
52
+ token = CognitiveFaculty.configuration.auth.access_token
53
+ env[:request_headers]["Authorization"] = "Bearer #{token}"
54
+ @app.call(env)
55
+ end
56
+ end
57
+ end
58
+
59
+ def self.configure
60
+ self.configuration ||= Configuration.new
61
+ yield(configuration)
62
+ setup_her
63
+ end
64
+ end
@@ -0,0 +1,41 @@
1
+ require "uri"
2
+ require "oauth2"
3
+
4
+ module CognitiveFaculty
5
+ class Configuration
6
+ ##
7
+ # Authorization configuration for Cognitive Faculty Platform API.
8
+ #
9
+ # Attributes:
10
+ # +url+: URL for authorization server.
11
+ # +client_id+: Client ID used to request an acces token.
12
+ # +client_secret+: Client secret used to request an access token.
13
+ # +username+: Username for requesting a token using the +password+ strategy.
14
+ # +password+: Password for requesting a token using the +password+ strategy.
15
+ # +realm+: Keycloak realm to use. Deafults to +master+.
16
+ # +token+: (read-only) OIDC access token.
17
+ class Auth
18
+ attr_accessor :url, :client_id, :client_secret, :username, :password,
19
+ :realm
20
+ attr_reader :token
21
+
22
+ def initialize
23
+ @realm = "master"
24
+ end
25
+
26
+ def access_token
27
+ update_token if token.nil? || token.expired?
28
+ token.token
29
+ end
30
+
31
+ def update_token
32
+ opts = {
33
+ site: url,
34
+ token_url: "/auth/realms/#{realm}/protocol/openid-connect/token"
35
+ }
36
+ oauth2 = OAuth2::Client.new(client_id, client_secret, opts)
37
+ @token = oauth2.password.get_token(username, password)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,17 @@
1
+ require "cognitive_faculty/configuration/auth"
2
+
3
+ module CognitiveFaculty
4
+ ##
5
+ # Configuration object for `CognitiveFaculty` API client.
6
+ #
7
+ # Attributes
8
+ # +auth+: CognitiveFaculty::Configuration::Auth object
9
+ # +url+: URL for the Cognitive Faculty Platform API
10
+ class Configuration
11
+ attr_accessor :auth, :url
12
+
13
+ def initialize
14
+ @auth = Auth.new
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ require "cognitive_faculty/models/code"
2
+ require "cognitive_faculty/models/offer"
3
+ require "cognitive_faculty/models/badge"
@@ -0,0 +1,38 @@
1
+ module CognitiveFaculty
2
+ ##
3
+ # Model for a Badge
4
+ class Badge
5
+ include Her::Model
6
+
7
+ primary_key :id
8
+
9
+ def issue(lp_certs_url, learning_path, opts = {})
10
+ opts["badge_template_id"] = id
11
+ opts["email"] = opts[:to][:email]
12
+ opts["first_name"] = opts[:to][:name].split(" ", 2).first
13
+ opts["last_name"] = opts[:to][:name].split(" ", 2).last
14
+ return false unless eligible?(lp_certs_url, learning_path, opts[:to])
15
+ self.class.post_raw("/api/badges", opts) do |parsed_data|
16
+ parsed_data[:data][:success]
17
+ end
18
+ end
19
+
20
+ def issued_badge(email)
21
+ self.class.get_raw("/api/badges/#{id}/issued?email=#{email}") do |data|
22
+ if data[:data][:issued]
23
+ data[:data][:badge]
24
+ else
25
+ false
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def eligible?(lp_certs_url, learning_path, user)
33
+ self.class.get_raw("#{lp_certs_url}/certificates/#{user[:username]}/#{learning_path[:slug]}/status") do |status|
34
+ status[:data][:eligible]
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,10 @@
1
+ module CognitiveFaculty
2
+ ##
3
+ # Model for an Offer Code
4
+ class Code
5
+ include Her::Model
6
+
7
+ parse_root_in_json true, format: :active_model_serializers
8
+ primary_key :code
9
+ end
10
+ end
@@ -0,0 +1,31 @@
1
+ module CognitiveFaculty
2
+ ##
3
+ # Model for an Offer
4
+ class Offer
5
+ include Her::Model
6
+
7
+ attr_accessor :description, :content
8
+
9
+ scope :active, (lambda do
10
+ has_batch_enabled = lambda do |offer|
11
+ offer.batches.select { |batch| batch["active"] }.size.positive?
12
+ end
13
+ all.select(&has_batch_enabled)
14
+ end)
15
+
16
+ # her < 1.0 can't parse codes properly because they under a root element
17
+ # has_many :codes
18
+
19
+ def claim(opts = {})
20
+ self.class.put_raw("/api/offers/#{id}/codes/claim", opts) do |parsed_data|
21
+ parsed_data[:data][:code]
22
+ end
23
+ end
24
+
25
+ def codes(opts = {})
26
+ self.class.get_raw("/api/offers/#{id}/codes", opts) do |parsed_data|
27
+ parsed_data[:data][:codes].map { |c| Code.new(c) }
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module CognitiveFaculty
2
+ VERSION = "1.1.0".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cognitive_faculty
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Luiz Aoqui
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.17.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.17.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.67.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.67.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: activemodel
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 5.0.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 5.0.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 5.0.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 5.0.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: faraday_middleware
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.12.2
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.12.2
111
+ - !ruby/object:Gem::Dependency
112
+ name: her
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.8.0
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.8.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: oauth2
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 1.4.0
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 1.4.0
139
+ description: Ruby gem for the Cognitive Faculty API.
140
+ email:
141
+ - laoqui@ca.ibm.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".rubocop.yml"
149
+ - ".travis.yml"
150
+ - Gemfile
151
+ - Gemfile.lock
152
+ - README.md
153
+ - Rakefile
154
+ - bin/console
155
+ - bin/setup
156
+ - cognitive_faculty.gemspec
157
+ - lib/cognitive_faculty.rb
158
+ - lib/cognitive_faculty/configuration/auth.rb
159
+ - lib/cognitive_faculty/configuration/configuration.rb
160
+ - lib/cognitive_faculty/models.rb
161
+ - lib/cognitive_faculty/models/badge.rb
162
+ - lib/cognitive_faculty/models/code.rb
163
+ - lib/cognitive_faculty/models/offer.rb
164
+ - lib/cognitive_faculty/version.rb
165
+ homepage: https://cognitivefaculty.com
166
+ licenses: []
167
+ metadata: {}
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ requirements: []
183
+ rubygems_version: 3.1.2
184
+ signing_key:
185
+ specification_version: 4
186
+ summary: Cognitive Faculty Ruby SDK
187
+ test_files: []