canvas_lms_api 0.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: 8e327db5daad3366209150d82923fb620bc1788b99f73185f5bdadf581bd10eb
4
+ data.tar.gz: 4f6f1a8aded691287101a3b1a0f7ea6abb271f7d719066d6957f5491443ee441
5
+ SHA512:
6
+ metadata.gz: '090d56fd98b5307b71f8b0f59cec7ca56bb960061087f261567534afc45d1af50a5245037ff3bbe493109d9a67b59ec5ed57edbbb4e5d0e43306ac95af9a3749'
7
+ data.tar.gz: c5c53cbac27ebd568993e69d18f015cd3f465e3b9c51ed29c5f35b54fba268e0ce2f0e70025371838b1326215c2cf5ed8e3ae007a806f03057526c02594346fe
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.0
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Christopher Bennell
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,54 @@
1
+ # CanvasLmsApi
2
+
3
+ A simple ruby API client for CanvasLMS.
4
+
5
+ ## Installation
6
+
7
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ ```bash
12
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
13
+ ```
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ ```bash
18
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ canvas_client = CanvasLmsApi::Client.new("https://your.canvas.com", "your API Key")
24
+
25
+ ## Todo
26
+
27
+ -[ ] Add YARD doc
28
+ -[ ] Remove Results.coalesce_results
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it ( https://github.com/christopher-b/canvas_lms_api/fork )
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create a new Pull Request
37
+
38
+ ## Development
39
+
40
+ 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.
41
+
42
+ 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).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/christopher-b/canvas_lms_api. 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/christopher-b/canvas_lms_api/blob/main/CODE_OF_CONDUCT.md).
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
51
+
52
+ ## Code of Conduct
53
+
54
+ Everyone interacting in the CanvasLms::Api project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/christopher-b/canvas_lms-api/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "canvas_lms_api/request_methods"
4
+ require "canvas_lms_api/request"
5
+ require "canvas_lms_api/results"
6
+ require "canvas_lms_api/uri"
7
+
8
+ # @TODO extract URI
9
+ # extract CRUD methods
10
+ # extract pagination?
11
+
12
+ module CanvasLmsApi
13
+ class Client
14
+ include CanvasLmsApi::RequestMethods
15
+
16
+ def initialize(prefix, api_key)
17
+ @prefix = prefix
18
+ @api_key = api_key
19
+ end
20
+
21
+ def request(endpoint, params = {})
22
+ uri = uri_for(endpoint, params[:query])
23
+ loop do
24
+ response = CanvasLmsApi::Request.new(
25
+ uri,
26
+ params[:method],
27
+ params[:payload]
28
+ ).call
29
+ results.collect(response.body)
30
+ next_page_uri = response.next_page
31
+
32
+ break unless next_page_uri
33
+ uri = uri_for(next_page_uri)
34
+ end
35
+
36
+ results.coalesce_results
37
+ end
38
+
39
+ private
40
+
41
+ def results
42
+ @results ||= CanvasLmsApi::Results.new
43
+ end
44
+
45
+ def uri_for(endpoint, params = nil)
46
+ CanvasLmsApi::Uri.new(@prefix, endpoint, params)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "rest-client"
5
+ require "canvas_lms_api/response"
6
+
7
+ module CanvasLmsApi
8
+ class Request
9
+ def initialize(uri, method = nil, payload = nil)
10
+ @uri = uri
11
+ @method = method
12
+ @payload = payload
13
+ end
14
+
15
+ def call
16
+ CanvasLmsApi::Response.new(rest_request.execute, self)
17
+ rescue RestClient::BadRequest => e
18
+ message = JSON.parse(e.response)["message"]
19
+ raise CanvasLmsApi::BadRequest.new(e.message + ": #{message}")
20
+ end
21
+
22
+ private
23
+
24
+ def rest_request
25
+ @rest_request ||= RestClient::Request.new(
26
+ url: @uri.to_s,
27
+ method: @method || :get,
28
+ payload: @payload,
29
+ headers: headers
30
+ # verify_ssl: false
31
+ )
32
+ end
33
+
34
+ def headers
35
+ {"Authorization" => "Bearer #{@api_key}"}
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CanvasLmsApi
4
+ module RequestMethods
5
+ def get(endpoint, query = nil)
6
+ request(endpoint, method: :get, query: query)
7
+ end
8
+
9
+ def post(endpoint, payload = nil)
10
+ request(endpoint, method: :post, payload: payload)
11
+ end
12
+
13
+ def put(endpoint, payload = nil, params = {})
14
+ request(endpoint, method: :put, payload: payload)
15
+ end
16
+
17
+ # def delete(endpoint, payload = nil)
18
+ # request(endpoint, method: :delete, payload: payload)
19
+ # end
20
+ end
21
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CanvasLmsApi
4
+ class Response
5
+ LINK_PATTERN = %r{^<https?://.*/api/v1/([^>]+)[^"]+"([^"]+)}
6
+
7
+ attr_reader :rest_response, :request
8
+
9
+ def initialize(rest_response, request)
10
+ @rest_response = rest_response
11
+ @request = request
12
+ end
13
+
14
+ def body
15
+ @body ||= JSON.parse(rest_response.body)
16
+ end
17
+
18
+ def next_page
19
+ @next_page ||= begin
20
+ return nil unless rest_response.headers[:link]
21
+
22
+ rest_response.headers[:link].split(",").each do |link|
23
+ link.match(LINK_PATTERN) do |match|
24
+ url = match[1]
25
+ rel = match[2]
26
+ return url if rel == "next"
27
+ end
28
+ end
29
+
30
+ # No "next" link found
31
+ nil
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rest-client"
4
+
5
+ # This is a wrapper around a result set, which allows us to collect
6
+ # results across paginated responses
7
+
8
+ module CanvasLmsApi
9
+ class Results
10
+ attr_accessor :value
11
+
12
+ def initialize
13
+ @value = nil
14
+ end
15
+
16
+ def collect(new_results)
17
+ if value.nil?
18
+ self.value = new_results
19
+ elsif new_results.is_a? Hash
20
+ new_results.each_pair do |k, v|
21
+ if value.key?(k) && value[k].is_a?(Array)
22
+ value[k].concat(v)
23
+ else
24
+ value[k] = v
25
+ end
26
+ end
27
+ else
28
+ value.try(:concat, new_results)
29
+ end
30
+ end
31
+
32
+ # This was a bad decision and I should work to remove it
33
+ def coalesce_results
34
+ if value.is_a? Array
35
+ value.one? ? value.first : value
36
+ else
37
+ value
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rack/utils"
4
+ require "active_support/core_ext/hash"
5
+
6
+ module CanvasLmsApi
7
+ class Uri
8
+ PER_PAGE = 999
9
+
10
+ attr_reader :prefix, :endpoint, :query_param
11
+
12
+ def initialize(prefix, endpoint, query_param)
13
+ @prefix = fix_prefix(prefix)
14
+ @endpoint = fix_endpoint(endpoint, @prefix)
15
+ @query_param = query_param
16
+ end
17
+
18
+ def uri
19
+ URI("#{prefix}#{endpoint}").tap do |u|
20
+ query = Rack::Utils.parse_nested_query u.query
21
+ query.merge! Hash(query_param)
22
+ query[:per_page] ||= PER_PAGE
23
+ u.query = query.to_query
24
+ end
25
+ end
26
+
27
+ def to_s
28
+ uri.to_s
29
+ end
30
+
31
+ private
32
+
33
+ def fix_prefix(prefix)
34
+ "#{prefix}/api/v1/"
35
+ end
36
+
37
+ def fix_endpoint(endpoint, prefix)
38
+ endpoint.delete_prefix(prefix)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CanvasLmsApi
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "canvas_lms_api/version"
4
+ require_relative "canvas_lms_api/client"
5
+ # require "canvas_lms_api/request_methods"
6
+ # require "canvas_lms_api/request"
7
+ # require "canvas_lms_api/response"
8
+ # require "canvas_lms_api/results"
9
+ # require "canvas_lms_api/uri"
10
+
11
+ require "canvas_lms_api/client"
12
+
13
+ module CanvasLmsApi
14
+ class BadRequest < StandardError
15
+ attr_accessor :request
16
+ end
17
+
18
+ class << self
19
+ attr_writer :logger
20
+ def logger
21
+ @logger ||= logger.new("/dev/null").tap do |log|
22
+ log.progname = name
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,6 @@
1
+ module CanvasLms
2
+ module Api
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: canvas_lms_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Bennell
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-10-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.24'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.24'
83
+ description:
84
+ email:
85
+ - cbennell@hey.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".rspec"
91
+ - ".standard.yml"
92
+ - CODE_OF_CONDUCT.md
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/canvas_lms_api.rb
97
+ - lib/canvas_lms_api/client.rb
98
+ - lib/canvas_lms_api/request.rb
99
+ - lib/canvas_lms_api/request_methods.rb
100
+ - lib/canvas_lms_api/response.rb
101
+ - lib/canvas_lms_api/results.rb
102
+ - lib/canvas_lms_api/uri.rb
103
+ - lib/canvas_lms_api/version.rb
104
+ - sig/canvas_lms/api.rbs
105
+ homepage: https://github.com/christopher-b/canvas_lms_api
106
+ licenses:
107
+ - MIT
108
+ metadata:
109
+ homepage_uri: https://github.com/christopher-b/canvas_lms_api
110
+ source_code_uri: https://github.com/christopher-b/canvas_lms_api
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: 3.0.0
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubygems_version: 3.3.7
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: A simple API client for CanvasLMS.
130
+ test_files: []