Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: configure canvas wrapper #5

Merged
merged 19 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
5494d8f
feat: Integrate LMS API wrapper for Canvas interaction
Zzz212zzZ Feb 16, 2024
4f54d66
feat: Add BCourses controller and view for course listing (/bcourses)
Zzz212zzZ Feb 16, 2024
cb18cb1
feat: Securely store BCourse Dev API Token using Rails credentials
Zzz212zzZ Feb 16, 2024
1631127
chore: remove the authentication model and the helper file since they…
Zzz212zzZ Feb 20, 2024
681af7c
test: add test to the controller
Zzz212zzZ Feb 20, 2024
2034c1a
Remove unnecessary gem and conflict one
Zzz212zzZ Feb 20, 2024
801abcd
test refactor: bcourses_controller_spec test
Zzz212zzZ Feb 21, 2024
7d3b404
pull latest main branch, resolve conflict
Zzz212zzZ Feb 21, 2024
9c0bd35
Delete: Mailer/
Zzz212zzZ Feb 22, 2024
e7f87f7
Merge branch 'main' of https://github.com/cs169/flextensions into #18…
Zzz212zzZ Feb 23, 2024
77705b0
Pull the latest commit (CI test database) to pass CI
Zzz212zzZ Feb 26, 2024
20471a3
Merge branch 'main' into #187042260-configure-canvas-wrapper
Zzz212zzZ Feb 29, 2024
569acd8
Pull latest main, address conflict in Gemfile
Zzz212zzZ Mar 1, 2024
500022a
resolve conflict
Zzz212zzZ Mar 1, 2024
e7ea9a9
Add specific errors to be caught
Zzz212zzZ Mar 2, 2024
606a3b3
Make the url dynamic, instead of hardcoded, only in Prod Env is 'bcou…
Zzz212zzZ Mar 3, 2024
0365183
Refactor Credentials: update how bcourses controller get credentials …
Zzz212zzZ Mar 5, 2024
0b16e03
Refactor credential structure, modify url/api_key name
Zzz212zzZ Mar 5, 2024
ce10edd
Remove redundant byebug
Zzz212zzZ Mar 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ yarn-debug.log*
!/storage/.keep
/public/uploads

/config/master.key

/config/credentials/production.key
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ gem "tzinfo-data", platforms: %i[ windows jruby ]
# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false

gem "lms-api"



# Use Active Storage for file uploads [https://guides.rubyonrails.org/active_storage_overview.html]
# gem "activestorage", "~> 7.0.0"

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible [

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

Expand Down Expand Up @@ -68,6 +77,9 @@ end
group :development do
# Use console on exceptions pages [https://github.com/rails/web-console]
gem "web-console"

#for debug
gem 'byebug'

# Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
# gem "rack-mini-profiler"
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ GEM
autoprefixer-rails (>= 9.1.0)
popper_js (>= 2.11.8, < 3)
builder (3.2.4)
byebug (11.1.3)
capybara (3.40.0)
addressable
matrix
Expand Down Expand Up @@ -169,6 +170,9 @@ GEM
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
httparty (0.21.0)
mini_mime (>= 1.0.0)
multi_xml (>= 0.5.2)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
importmap-rails (2.0.1)
Expand All @@ -190,6 +194,9 @@ GEM
listen (3.8.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
lms-api (1.24.0)
activesupport (>= 3.0)
httparty
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand All @@ -206,6 +213,7 @@ GEM
minitest (5.22.2)
msgpack (1.7.2)
multi_test (1.1.0)
multi_xml (0.6.0)
mutex_m (0.2.0)
nenv (0.3.0)
net-imap (0.4.10)
Expand Down Expand Up @@ -378,6 +386,7 @@ PLATFORMS
DEPENDENCIES
bootsnap
bootstrap (~> 5.3.2)
byebug
cucumber-rails
cucumber-rails-training-wheels
database_cleaner
Expand All @@ -387,6 +396,7 @@ DEPENDENCIES
importmap-rails
jbuilder
jquery-rails
lms-api
json
pg
puma (>= 5.0)
Expand Down
18 changes: 18 additions & 0 deletions app/controllers/bcourses_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class BcoursesController < ApplicationController
require 'lms_api'

def index
canvas_url = "https://bcourses.berkeley.edu"
Zzz212zzZ marked this conversation as resolved.
Show resolved Hide resolved

# Assuming LMS::Canvas.new expects a token directly. Adjust as needed for actual API wrapper usage.
canvas_api_key = Rails.application.credentials.dig(:development, :canvas, :dev_api_key) # this will be obtained from omniauth in later iterations
api = LMS::Canvas.new(canvas_url, canvas_api_key)

# byebug
Zzz212zzZ marked this conversation as resolved.
Show resolved Hide resolved
# Fetch courses list
courses_url = "#{canvas_url}/api/v1/courses"
@courses = api.api_get_request(courses_url)
rescue StandardError => e
Zzz212zzZ marked this conversation as resolved.
Show resolved Hide resolved
@error = "Failed to fetch courses: #{e.message}"
end
end
11 changes: 11 additions & 0 deletions app/views/bcourses/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>Canvas Courses</h1>

<% if @error %>
<p>Error: <%= @error %></p>
<% else %>
<ul>
<% @courses.each do |course| %>
<li><%= course['name'] %></li> <!-- Adjust based on actual structure of API response -->
<% end %>
</ul>
<% end %>
2 changes: 1 addition & 1 deletion config/credentials.yml.enc
Zzz212zzZ marked this conversation as resolved.
Show resolved Hide resolved
Zzz212zzZ marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5X/tEitXqeVOyzu33tuVoVEGehkgipn/NwYQbfsHZIFt0gK0RyiOl9rs7fGpYiO5GFEjP8cDSMAAkzxUaJEgA5xQPSd/veuANzZpZAp5q1KSf25o2FEs8BqAYxfFqjmstCYnEVoxF4pVuFneSTaBqcS2h9Y9QHNIoqaYEVmI/26D5eWJFAM8ZLwH0PZgs4MeP7XWfriHWKoDNXU6uJ3koZKEYjeMPw5rv1ryhWEeZQ/Lf9mhg66v5vzL6aVC+vztc5QfUC9aBKPNQswwRL4Kex/I5QDS3X4s3LaHvk0toFgW/0HomqHM+GU3zOSyBNPBu2cwHxGjnI4sv0wnYAWzhNxfOOfrw4C9xiKmvnRzUjNU++5+i62wy/VPQu7Vz/nRHEMv38oIvkFVJeM7L43WwaVni15gxQ==--6fVPEZu8ZFUwW4mj--TvOdIbOW4HT1ngsRtW5RLg==
5X/tEitXqeVOyzu33tuVoVEGehkgipn/NwYQbfsHZIFt0gK0RyiOl9rs7fGpYiO5GFEjP8cDSMAAkzxUaJEgA5xQPSd/veuANzZpZAp5q1KSf25o2FEs8BqAYxfFqjmstCYnEVoxF4pVuFneSTaBqcS2h9Y9QHNIoqaYEVmI/26D5eWJFAM8ZLwH0PZgs4MeP7XWfriHWKoDNXU6uJ3koZKEYjeMPw5rv1ryhWEeZQ/Lf9mhg66v5vzL6aVC+vztc5QfUC9aBKPNQswwRL4Kex/I5QDS3X4s3LaHvk0toFgW/0HomqHM+GU3zOSyBNPBu2cwHxGjnI4sv0wnYAWzhNxfOOfrw4C9xiKmvnRzUjNU++5+i62wy/VPQu7Vz/nRHEMv38oIvkFVJeM7L43WwaVni15gxQ==--6fVPEZu8ZFUwW4mj--TvOdIbOW4HT1ngsRtW5RLg==
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Rails.application.routes.draw do
get 'bcourses/index'
get 'bcourses', to: 'bcourses#index'
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
Expand Down
39 changes: 39 additions & 0 deletions spec/controllers/bcourses_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'rails_helper'
require 'lms_api'

RSpec.describe "Bcourses", type: :request do
describe 'GET /index' do
let(:canvas_api_key) { 'test_api_key' }
let(:api_mock) { instance_double('LMS::Canvas') }
let(:courses) { [{'name' => 'Course 1'}, {'name' => 'Course 2'}] }

before do
allow(Rails.application.credentials).to receive(:dig).with(:development, :canvas, :dev_api_key).and_return(canvas_api_key)
allow(LMS::Canvas).to receive(:new).with("https://bcourses.berkeley.edu", canvas_api_key).and_return(api_mock)
allow(api_mock).to receive(:api_get_request).and_return(courses)
end

context 'when API call is successful' do
it 'renders the index template with courses' do
get bcourses_path
expect(response).to be_successful
puts response.body
expect(response.body).to include('Course 1')
expect(response.body).to include('Course 2')
end
end

context 'when API call fails' do
let(:error_message) { "Failed to fetch courses: Error" }
before do
allow(api_mock).to receive(:api_get_request).and_raise(StandardError, 'Error')
end

it 'renders the index template with an error message' do
get bcourses_path
expect(response).to be_successful
expect(response.body).to include(error_message)
end
end
end
end