canlii 0.1.1

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: 471bb65f4244f6dff6818d538d4d90b702437466a1484aeed6710aae9aee1821
4
+ data.tar.gz: a15b1f8ad672f1a9e8a5e69836dc073d9fb7ed02fdc2af987c88a80e5042c0e7
5
+ SHA512:
6
+ metadata.gz: a0317eb6d7611e5d536f824904b30f3ba4613f6e12b4aefa3f7247e7fca0371abb609ee7295d2291860823e92cbc4ed9cc817433bd60c3bcdc5895fa4f3aab8e
7
+ data.tar.gz: 77ead1f2d01b5abb9447d771f2a095a007698a9a2885bccab6e24c0f5605b7058ae4eaa342943161d179a21e07c2208093a2d640b4e13c4e4c1b16f69ca7599c
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-06-30
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Ajay Krishnan
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,119 @@
1
+ # CanLII Ruby
2
+
3
+ A lightweight Ruby client for accessing Canadian legal information via the CanLII API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'canlii'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install canlii
20
+
21
+ ## Configuration
22
+
23
+ ### Basic Configuration
24
+
25
+ ```ruby
26
+ require 'canlii'
27
+
28
+ CanLII.configure do |config|
29
+ config.api_key = ENV["CANLII_API_KEY"]
30
+ config.logger = Logger.new(STDOUT)
31
+ config.timeout = 30 # Optional: timeout in seconds (default: 30)
32
+ end
33
+ ```
34
+
35
+ ### Rails Configuration
36
+
37
+ In `config/initializers/canlii.rb`:
38
+
39
+ ```ruby
40
+ CanLII.configure do |config|
41
+ config.api_key = ENV["CANLII_API_KEY"]
42
+ config.logger = Rails.logger
43
+ config.timeout = 30 # Optional: timeout in seconds (default: 30)
44
+ end
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ ### List All Databases
50
+
51
+ ```ruby
52
+ databases = CanLII::Database.all
53
+ databases.each do |db|
54
+ puts "#{db.database_id}: #{db.name} (#{db.jurisdiction})"
55
+ end
56
+ ```
57
+
58
+ ### Browse Cases
59
+
60
+ ```ruby
61
+ # Recent Supreme Court cases
62
+ cases = CanLII::Case.browse("csc-scc", limit: 10)
63
+ cases.each do |c|
64
+ puts "#{c.citation}: #{c.title}"
65
+ end
66
+
67
+ # Cases from last 30 days
68
+ recent = CanLII::Case.browse("onca",
69
+ published_after: 30.days.ago,
70
+ limit: 100
71
+ )
72
+ ```
73
+
74
+ ### Find Specific Case
75
+
76
+ ```ruby
77
+ # Find returns nil if not found
78
+ case_detail = CanLII::Case.find("csc-scc", "2024scc1")
79
+
80
+ # Find! raises error if not found
81
+ case_detail = CanLII::Case.find!("csc-scc", "2024scc1")
82
+ ```
83
+
84
+ ### Language Support
85
+
86
+ ```ruby
87
+ # Temporary language switch
88
+ CanLII.with_language("fr") do
89
+ databases = CanLII::Database.all # Returns French names
90
+ cases = CanLII::Case.browse("qcca") # Quebec cases
91
+ end
92
+
93
+ # Permanent language switch
94
+ CanLII.configuration.language = "fr"
95
+ ```
96
+
97
+ ### Using Custom Clients (Advanced)
98
+
99
+ ```ruby
100
+ # For testing or using multiple API keys
101
+ mock_client = MyMockClient.new
102
+
103
+ CanLII::Case.with_client(mock_client) do
104
+ case_detail = CanLII::Case.find("on", "2024onca1")
105
+ # This will use mock_client instead of the default HTTP client
106
+ end
107
+ ```
108
+
109
+ ## Development
110
+
111
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
112
+
113
+ ## Contributing
114
+
115
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ajaynomics/canlii-ruby.
116
+
117
+ ## License
118
+
119
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CanLII
4
+ class Base
5
+ include ActiveModel::Model
6
+ include ActiveModel::Attributes
7
+
8
+ class << self
9
+ def with_client(client = nil)
10
+ old_client = Thread.current[:canlii_client]
11
+
12
+ if client
13
+ Thread.current[:canlii_client] = client
14
+ yield client
15
+ else
16
+ CanLII.configuration.validate!
17
+ yield current_client
18
+ end
19
+ ensure
20
+ Thread.current[:canlii_client] = old_client
21
+ end
22
+
23
+ private
24
+
25
+ def current_client
26
+ Thread.current[:canlii_client] || Client.new
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CanLII
4
+ class Case < Base
5
+ attribute :database_id, :string
6
+ attribute :case_id, :string
7
+ attribute :title, :string
8
+ attribute :citation, :string
9
+ attribute :url, :string
10
+ attribute :decision_date, :date
11
+ attribute :language, :string
12
+
13
+ class << self
14
+ def browse(database_id, **options)
15
+ with_client do |client|
16
+ params = build_browse_params(options)
17
+ response = client.get("/caseBrowse/#{language}/#{database_id}", params)
18
+
19
+ return [] if response.is_a?(Array)
20
+
21
+ cases = response["cases"] || []
22
+ cases.map { |data| new_from_browse(data) }
23
+ end
24
+ end
25
+
26
+ def find(database_id, case_id)
27
+ with_client do |client|
28
+ response = client.get("/caseBrowse/#{language}/#{database_id}/#{case_id}")
29
+ new_from_detail(response)
30
+ end
31
+ rescue NotFoundError
32
+ nil
33
+ end
34
+
35
+ def find!(database_id, case_id)
36
+ find(database_id, case_id) ||
37
+ raise(NotFoundError, "Case not found: #{database_id}/#{case_id}")
38
+ end
39
+
40
+ private
41
+
42
+ def new_from_browse(data)
43
+ case_id_obj = data["caseId"]
44
+ case_id_value = case_id_obj.is_a?(Hash) ? case_id_obj["en"] : case_id_obj
45
+
46
+ new(
47
+ database_id: data["databaseId"],
48
+ case_id: case_id_value,
49
+ title: data["title"],
50
+ citation: data["citation"],
51
+ decision_date: data["decisionDate"]
52
+ )
53
+ end
54
+
55
+ def new_from_detail(data)
56
+ new(
57
+ database_id: data["databaseId"],
58
+ case_id: data["caseId"],
59
+ title: data["title"],
60
+ citation: data["citation"],
61
+ url: data["url"],
62
+ decision_date: data["decisionDate"],
63
+ language: data["language"]
64
+ )
65
+ end
66
+
67
+ def build_browse_params(options)
68
+ params = {}
69
+ params[:offset] = options[:offset] || 0
70
+ params[:resultCount] = options[:limit] || 20
71
+
72
+ params[:decisionDateAfter] = options[:published_after].to_s if options[:published_after]
73
+
74
+ params[:decisionDateBefore] = options[:published_before].to_s if options[:published_before]
75
+
76
+ params
77
+ end
78
+
79
+ def language
80
+ CanLII.configuration.language
81
+ end
82
+ end
83
+
84
+ def to_s
85
+ citation || title || "#{database_id}/#{case_id}"
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CanLII
4
+ class Client
5
+ def get(path, params = {})
6
+ params = params.merge(api_key: config.api_key, language: config.language)
7
+
8
+ http_client = HTTP
9
+ http_client = http_client.timeout(config.timeout) if config.timeout
10
+
11
+ response = http_client.get(build_url(path), params: params)
12
+ handle_response(response)
13
+ end
14
+
15
+ private
16
+
17
+ def build_url(path)
18
+ "#{config.base_url}#{path}"
19
+ end
20
+
21
+ def handle_response(response)
22
+ case response.status
23
+ when 200..299
24
+ JSON.parse(response.body.to_s)
25
+ when 401, 403
26
+ raise AuthenticationError, "Invalid API key"
27
+ when 404
28
+ raise NotFoundError, "Resource not found"
29
+ when 429
30
+ raise RateLimitError, "Rate limit exceeded"
31
+ when 500..599
32
+ raise ResponseError, "Server error: HTTP #{response.status}"
33
+ else
34
+ raise ResponseError, "HTTP #{response.status}: #{response.body}"
35
+ end
36
+ rescue HTTP::TimeoutError
37
+ raise TimeoutError, "Request timed out after #{config.timeout} seconds"
38
+ rescue HTTP::Error => e
39
+ raise ConnectionError, "Network error: #{e.message}"
40
+ rescue JSON::ParserError => e
41
+ raise ResponseError, "Invalid JSON response: #{e.message}"
42
+ end
43
+
44
+ def config
45
+ CanLII.configuration
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CanLII
4
+ class Configuration
5
+ attr_accessor :api_key, :base_url, :language, :logger, :timeout
6
+
7
+ def initialize
8
+ @base_url = "https://api.canlii.org/v1"
9
+ @language = "en"
10
+ @api_key = ENV.fetch("CANLII_API_KEY", nil)
11
+ @logger = Logger.new($stdout)
12
+ @timeout = 30 # Default 30 seconds
13
+ end
14
+
15
+ def validate!
16
+ raise Error, "API key is required" if api_key.nil? || api_key.to_s.strip.empty?
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CanLII
4
+ class Database < Base
5
+ attribute :database_id, :string
6
+ attribute :name, :string
7
+ attribute :jurisdiction, :string
8
+
9
+ class << self
10
+ def all
11
+ with_client do |client|
12
+ response = client.get("/caseBrowse/#{language}")
13
+ databases = response["caseDatabases"] || []
14
+
15
+ databases.map do |data|
16
+ new(
17
+ database_id: data["databaseId"],
18
+ name: data["name"],
19
+ jurisdiction: data["jurisdiction"]
20
+ )
21
+ end
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def language
28
+ CanLII.configuration.language
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CanLII
4
+ # Base error class for all CanLII errors
5
+ class Error < StandardError; end
6
+
7
+ # Raised when API key is invalid or missing
8
+ class AuthenticationError < Error; end
9
+
10
+ # Raised when requested resource doesn't exist
11
+ class NotFoundError < Error; end
12
+
13
+ # Raised when API rate limit is exceeded
14
+ class RateLimitError < Error; end
15
+
16
+ # Raised when request times out
17
+ class TimeoutError < Error; end
18
+
19
+ # Raised when connection fails
20
+ class ConnectionError < Error; end
21
+
22
+ # Raised for other HTTP errors
23
+ class ResponseError < Error; end
24
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CanLII
4
+ module Rails
5
+ class Railtie < ::Rails::Railtie
6
+ initializer "canlii.logger" do |_app|
7
+ CanLII.configuration.logger = ::Rails.logger
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CanLII
4
+ VERSION = "0.1.1"
5
+ end
data/lib/canlii.rb ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_model"
4
+ require "http"
5
+ require "json"
6
+ require "logger"
7
+
8
+ require_relative "canlii/version"
9
+ require_relative "canlii/errors"
10
+ require_relative "canlii/configuration"
11
+ require_relative "canlii/client"
12
+ require_relative "canlii/base"
13
+ require_relative "canlii/database"
14
+ require_relative "canlii/case"
15
+
16
+ module CanLII
17
+ class << self
18
+ def configuration
19
+ @configuration ||= Configuration.new
20
+ end
21
+
22
+ def configure
23
+ yield(configuration)
24
+ end
25
+
26
+ def with_language(language)
27
+ old_language = configuration.language
28
+ configuration.language = language
29
+ yield
30
+ ensure
31
+ configuration.language = old_language
32
+ end
33
+ end
34
+ end
35
+
36
+ require_relative "canlii/rails/railtie" if defined?(Rails::Railtie)
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: canlii
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ajay Krishnan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-07-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '6.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '6.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: http
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: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '13.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '13.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rails-omakase
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ description: A lightweight Ruby client for accessing Canadian legal information via
112
+ the CanLII API
113
+ email:
114
+ - 50063680+ajaynomics@users.noreply.github.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - CHANGELOG.md
120
+ - LICENSE.txt
121
+ - README.md
122
+ - lib/canlii.rb
123
+ - lib/canlii/base.rb
124
+ - lib/canlii/case.rb
125
+ - lib/canlii/client.rb
126
+ - lib/canlii/configuration.rb
127
+ - lib/canlii/database.rb
128
+ - lib/canlii/errors.rb
129
+ - lib/canlii/rails/railtie.rb
130
+ - lib/canlii/version.rb
131
+ homepage: https://github.com/ajaynomics/canlii-ruby
132
+ licenses:
133
+ - MIT
134
+ metadata:
135
+ homepage_uri: https://github.com/ajaynomics/canlii-ruby
136
+ source_code_uri: https://github.com/ajaynomics/canlii-ruby
137
+ changelog_uri: https://github.com/ajaynomics/canlii-ruby/blob/main/CHANGELOG.md
138
+ rubygems_mfa_required: 'true'
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: 3.0.0
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubygems_version: 3.5.16
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Ruby client for the CanLII API
158
+ test_files: []