openstax_cnx 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/.gitignore +15 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +57 -0
- data/LICENSE.txt +21 -0
- data/README.md +140 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/openstax/cnx/v1.rb +134 -0
- data/lib/openstax/cnx/v1/baked.rb +13 -0
- data/lib/openstax/cnx/v1/book.rb +59 -0
- data/lib/openstax/cnx/v1/book_part.rb +48 -0
- data/lib/openstax/cnx/v1/http_error.rb +3 -0
- data/lib/openstax/cnx/v1/page.rb +198 -0
- data/lib/openstax/cnx/version.rb +5 -0
- data/lib/openstax_cnx.rb +8 -0
- data/openstax_cnx.gemspec +48 -0
- metadata +179 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1290e2c41b0cfe6e5f18b3a8d2f5f423d1ecec4d661b589929514a790064683d
|
4
|
+
data.tar.gz: b86557dc8226f2179fcb21990bdc5b6a2c796308943b4aa9eca71a932b258e95
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f704b248e1e7f1710ff6d75af6595171437279131f3f478cd6305ae7797578ce67026b56a8ea37ce4349b63b34023cd1ce9db909d39f51c3725cccf315bdc031
|
7
|
+
data.tar.gz: 2139b21f10a4157e532ca4e92ed071cb2aa4cc3df0a1d7eff86f43e8033d1d07f542b1b36114b43f9acf7892566bc022514467e3c3b74729844059d13b7b3e14
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at jpslav@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
openstax_cnx (0.1.0)
|
5
|
+
nokogiri
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.6.0)
|
11
|
+
public_suffix (>= 2.0.2, < 4.0)
|
12
|
+
byebug (11.0.1)
|
13
|
+
crack (0.4.3)
|
14
|
+
safe_yaml (~> 1.0.0)
|
15
|
+
diff-lcs (1.3)
|
16
|
+
dotenv (2.7.2)
|
17
|
+
hashdiff (0.3.8)
|
18
|
+
mini_portile2 (2.4.0)
|
19
|
+
nokogiri (1.10.2)
|
20
|
+
mini_portile2 (~> 2.4.0)
|
21
|
+
public_suffix (3.0.3)
|
22
|
+
rake (10.5.0)
|
23
|
+
rspec (3.8.0)
|
24
|
+
rspec-core (~> 3.8.0)
|
25
|
+
rspec-expectations (~> 3.8.0)
|
26
|
+
rspec-mocks (~> 3.8.0)
|
27
|
+
rspec-core (3.8.0)
|
28
|
+
rspec-support (~> 3.8.0)
|
29
|
+
rspec-expectations (3.8.2)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.8.0)
|
32
|
+
rspec-mocks (3.8.0)
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
34
|
+
rspec-support (~> 3.8.0)
|
35
|
+
rspec-support (3.8.0)
|
36
|
+
safe_yaml (1.0.5)
|
37
|
+
vcr (4.0.0)
|
38
|
+
webmock (3.5.1)
|
39
|
+
addressable (>= 2.3.6)
|
40
|
+
crack (>= 0.3.2)
|
41
|
+
hashdiff
|
42
|
+
|
43
|
+
PLATFORMS
|
44
|
+
ruby
|
45
|
+
|
46
|
+
DEPENDENCIES
|
47
|
+
bundler (~> 1.17)
|
48
|
+
byebug
|
49
|
+
dotenv
|
50
|
+
openstax_cnx!
|
51
|
+
rake (~> 10.0)
|
52
|
+
rspec (~> 3.0)
|
53
|
+
vcr
|
54
|
+
webmock
|
55
|
+
|
56
|
+
BUNDLED WITH
|
57
|
+
1.17.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 JP Slavinsky
|
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,140 @@
|
|
1
|
+
# OpenStax::Cnx
|
2
|
+
|
3
|
+
This gem provides a Ruby interface to OpenStax book content.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'openstax_cnx'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install openstax_cnx
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Books are made up of BookParts and Pages. Every Book as a single top-level "root" BookPart. BookParts can contain other BookParts and Pages. Many OpenStax books are organized as pages within chapters. For these books the Ruby objects hierarchy looks like:
|
24
|
+
|
25
|
+
```
|
26
|
+
Book
|
27
|
+
BookPart (the "root")
|
28
|
+
Page (the "Preface")
|
29
|
+
BookPart (Chapter 1)
|
30
|
+
Page (Introduction)
|
31
|
+
Page (Section 1.1)
|
32
|
+
etc...
|
33
|
+
BookPart (Chapter 2)
|
34
|
+
Page (Introduction)
|
35
|
+
etc...
|
36
|
+
etc...
|
37
|
+
```
|
38
|
+
|
39
|
+
Some OpenStax books organized chapters into units. The units are BookParts like the chapters are:
|
40
|
+
|
41
|
+
```
|
42
|
+
Book
|
43
|
+
BookPart (the "root")
|
44
|
+
Page (the "Preface")
|
45
|
+
BookPart (Unit 1)
|
46
|
+
Page (Unit Introduction)
|
47
|
+
BookPart (Chapter 1)
|
48
|
+
Page (Introduction)
|
49
|
+
Page (Section 1.1)
|
50
|
+
etc...
|
51
|
+
BookPart (Chapter 2)
|
52
|
+
Page (Introduction)
|
53
|
+
etc...
|
54
|
+
etc...
|
55
|
+
BookPart (Unit 2)
|
56
|
+
etc ...
|
57
|
+
```
|
58
|
+
|
59
|
+
This gem gives you Ruby objects to navigate this hierarchy and access the metadata and content at each level. You start by creating a `Book` object given that book's UUID.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
book = OpenStax::Cnx::V1.book(id: "031da8d3-b525-429c-80cf-6c8ed997733a")
|
63
|
+
```
|
64
|
+
|
65
|
+
Books have some metadata you can read out:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
book.title # => "College Physics"
|
69
|
+
book.baked # => "2019-03-20T14:24:26.164476-05:00"
|
70
|
+
book.url # => "https://archive.cnx.org/contents/031da8d3-b525-429c-80cf-6c8ed997733a"
|
71
|
+
```
|
72
|
+
|
73
|
+
To dig deeper, get the `root_book_part` and then is `parts`
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
book.root_book_part.is_root # => true
|
77
|
+
book.root_book_part.parts.count # => 40
|
78
|
+
book.root_book_part.parts.first.class # => OpenStax::Cnx::V1::Page
|
79
|
+
book.root_book_part.parts.first.title # => "Preface"
|
80
|
+
book.root_book_part.parts[2].class # => OpenStax::Cnx::V1::BookPart
|
81
|
+
book.root_book_part.parts[2].title # => "Kinematics"
|
82
|
+
book.root_book_part.parts[2].is_chapter? # => true
|
83
|
+
book.root_book_part.parts[2].parts.count # => 13
|
84
|
+
book.root_book_part.parts[2].parts[6].class # => OpenStax::Cnx::V1::Page
|
85
|
+
book.root_book_part.parts[2].parts[6].title # => "Problem-Solving Basics for One-Dimensional Kinematics"
|
86
|
+
book.root_book_part.parts[2].parts[6].baked_book_location # => ["2", "6"]
|
87
|
+
book.root_book_part.parts[2].parts[6].parsed_title # => {:text=>"Problem-Solving Basics for One-Dimensional Kinematics", :book_location=>["2", "6"]}
|
88
|
+
```
|
89
|
+
|
90
|
+
Note that a BookPart's parts array can contain both Pages and BookParts. The baked book location is the section number, e.g. above we see section 2.6.
|
91
|
+
|
92
|
+
You can run the above commands using the `bin/console` command in the gem directory.
|
93
|
+
|
94
|
+
### Configuration
|
95
|
+
|
96
|
+
You can override default gem configurations with the following:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
OpenStax::Cnx::V1.configure do |config|
|
100
|
+
# where to get the book content
|
101
|
+
config.archive_url_base = "https://someurl.com" # default is "https://archive.cnx.org"
|
102
|
+
# whether to ignore the book version history
|
103
|
+
config.ignore_history = false # default is true
|
104
|
+
# a logger
|
105
|
+
config.logger = Rails.logger # default is a null logger
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
You can temporarily override the Archive URL with:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
OpenStax::Cnx::V1.with_archive_url("https://archive-staging.cnx.org") do
|
113
|
+
# your code here, will see the archive-staging url
|
114
|
+
end
|
115
|
+
# the original archive URL will be reset by this point
|
116
|
+
```
|
117
|
+
|
118
|
+
This is useful in tests where the content may not be on the production system.
|
119
|
+
|
120
|
+
### What is `V1`?
|
121
|
+
|
122
|
+
The versions in this gem (e.g. the `V1` in `OpenStax::Cnx::V1::Book`) are not versions of the CNX API, but rather just different ways of building models to access the CNX API. We made a `V1` so that we could wildly change our mind later and add a `V2` without impact `V1` clients.
|
123
|
+
|
124
|
+
## Development
|
125
|
+
|
126
|
+
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.
|
127
|
+
|
128
|
+
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).
|
129
|
+
|
130
|
+
## Contributing
|
131
|
+
|
132
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/openstax_cnx. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
133
|
+
|
134
|
+
## License
|
135
|
+
|
136
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
137
|
+
|
138
|
+
## Code of Conduct
|
139
|
+
|
140
|
+
Everyone interacting in the OpenstaxCnx project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/openstax_cnx/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "openstax_cnx"
|
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,134 @@
|
|
1
|
+
require 'addressable/uri'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'logger'
|
4
|
+
require 'nokogiri'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
require_relative './v1/http_error'
|
8
|
+
|
9
|
+
require_relative './v1/book'
|
10
|
+
require_relative './v1/book_part'
|
11
|
+
require_relative './v1/page'
|
12
|
+
require_relative './v1/baked'
|
13
|
+
|
14
|
+
module OpenStax::Cnx::V1
|
15
|
+
|
16
|
+
def self.configure
|
17
|
+
yield configuration
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.configuration
|
21
|
+
@configuration ||= Configuration.new
|
22
|
+
end
|
23
|
+
|
24
|
+
class NullLogger < Logger
|
25
|
+
def initialize(*args); end
|
26
|
+
def add(*args, &block); end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Configuration
|
30
|
+
attr_reader :archive_url_base
|
31
|
+
attr_reader :webview_url_base
|
32
|
+
attr_accessor :ignore_history
|
33
|
+
attr_accessor :logger
|
34
|
+
|
35
|
+
def initialize
|
36
|
+
@logger = OpenStax::Cnx::V1::NullLogger.new
|
37
|
+
@ignore_history = true
|
38
|
+
self.archive_url_base = "https://archive.cnx.org"
|
39
|
+
end
|
40
|
+
|
41
|
+
def archive_url_base=(url)
|
42
|
+
uri = Addressable::URI.parse(url)
|
43
|
+
raise "Invalid CNX archive URL: #{url}" if uri.nil? || uri.host.nil?
|
44
|
+
|
45
|
+
uri.scheme = 'https'
|
46
|
+
@archive_url_base = uri.to_s
|
47
|
+
|
48
|
+
uri.host = uri.host.sub(/archive[\.-]?/, '')
|
49
|
+
@webview_url_base = uri.to_s
|
50
|
+
end
|
51
|
+
|
52
|
+
def webview_url_base=(url)
|
53
|
+
uri = Addressable::URI.parse(url)
|
54
|
+
raise "Invalid CNX webview URL: #{url}" if uri.nil? || uri.host.nil?
|
55
|
+
|
56
|
+
uri.scheme = 'https'
|
57
|
+
@webview_url_base = uri.to_s
|
58
|
+
|
59
|
+
uri.host = "archive-#{uri.host}".sub('archive-cnx', 'archive.cnx')
|
60
|
+
@archive_url_base = uri.to_s
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
class << self
|
66
|
+
|
67
|
+
def new_configuration
|
68
|
+
OpenStax::Cnx::V1::Configuration.new
|
69
|
+
end
|
70
|
+
|
71
|
+
def archive_url_base
|
72
|
+
configuration.archive_url_base
|
73
|
+
end
|
74
|
+
|
75
|
+
def webview_url_base
|
76
|
+
configuration.webview_url_base
|
77
|
+
end
|
78
|
+
|
79
|
+
def logger
|
80
|
+
configuration.logger
|
81
|
+
end
|
82
|
+
|
83
|
+
def with_archive_url(url)
|
84
|
+
begin
|
85
|
+
old_url = archive_url_base
|
86
|
+
self.configuration.archive_url_base = url
|
87
|
+
|
88
|
+
yield
|
89
|
+
ensure
|
90
|
+
self.configuration.archive_url_base = old_url
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def with_webview_url(url)
|
95
|
+
begin
|
96
|
+
old_url = webview_url_base
|
97
|
+
self.configuration.webview_url_base = url
|
98
|
+
|
99
|
+
yield
|
100
|
+
ensure
|
101
|
+
self.configuration.webview_url_base = old_url
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Archive url for the given path
|
106
|
+
# Forces /contents/ to be prepended to the path, unless the path begins with /
|
107
|
+
def archive_url_for(path)
|
108
|
+
Addressable::URI.join(configuration.archive_url_base, '/contents/', path).to_s
|
109
|
+
end
|
110
|
+
|
111
|
+
# Webview url for the given path
|
112
|
+
# Forces /contents/ to be prepended to the path, unless the path begins with /
|
113
|
+
def webview_url_for(path)
|
114
|
+
Addressable::URI.join(configuration.webview_url_base, '/contents/', path).to_s
|
115
|
+
end
|
116
|
+
|
117
|
+
def fetch(url)
|
118
|
+
begin
|
119
|
+
OpenStax::Cnx::V1.logger.debug { "Fetching #{url}" }
|
120
|
+
data = JSON.parse open(url, 'ACCEPT' => 'text/json').read
|
121
|
+
data.delete("history") if configuration.ignore_history
|
122
|
+
data
|
123
|
+
rescue OpenURI::HTTPError => exception
|
124
|
+
raise OpenStax::HTTPError, "#{exception.message} for URL #{url}"
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def book(options = {})
|
129
|
+
OpenStax::Cnx::V1::Book.new(options)
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module OpenStax::Cnx::V1::Baked
|
2
|
+
def self.parse_title(title)
|
3
|
+
return nil if title.nil?
|
4
|
+
|
5
|
+
part = Nokogiri::HTML.fragment(title)
|
6
|
+
text_node = part.css('.os-text')
|
7
|
+
if !text_node.empty?
|
8
|
+
{ text: text_node.text, book_location: part.css('.os-number').text.split('.') }
|
9
|
+
else
|
10
|
+
{ text: title, book_location: [] }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module OpenStax::Cnx::V1
|
2
|
+
class Book
|
3
|
+
|
4
|
+
def initialize(id: nil, hash: nil, title: nil, tree: nil, root_book_part: nil)
|
5
|
+
@id = id
|
6
|
+
@hash = hash
|
7
|
+
@title = title
|
8
|
+
@tree = tree
|
9
|
+
@root_book_part = root_book_part
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :id
|
13
|
+
|
14
|
+
def url
|
15
|
+
@url ||= OpenStax::Cnx::V1.archive_url_for(id)
|
16
|
+
end
|
17
|
+
|
18
|
+
def baked
|
19
|
+
@baked ||= hash.fetch('baked', nil)
|
20
|
+
end
|
21
|
+
|
22
|
+
def collated
|
23
|
+
@collated ||= hash.fetch('collated', nil)
|
24
|
+
end
|
25
|
+
|
26
|
+
def hash
|
27
|
+
@hash ||= OpenStax::Cnx::V1.fetch(url)
|
28
|
+
end
|
29
|
+
|
30
|
+
def uuid
|
31
|
+
@uuid ||= hash.fetch('id') { |key| raise "Book id=#{@id} is missing #{key}" }
|
32
|
+
end
|
33
|
+
|
34
|
+
def short_id
|
35
|
+
@short_id ||= hash.fetch('shortId', nil)
|
36
|
+
end
|
37
|
+
|
38
|
+
def version
|
39
|
+
@version ||= hash.fetch('version') { |key| raise "Book id=#{@id} is missing #{key}" }
|
40
|
+
end
|
41
|
+
|
42
|
+
def canonical_url
|
43
|
+
@canonical_url ||= OpenStax::Cnx::V1.archive_url_for("#{uuid}@#{version}")
|
44
|
+
end
|
45
|
+
|
46
|
+
def title
|
47
|
+
@title ||= hash.fetch('title') { |key| raise "Book id=#{@id} is missing #{key}" }
|
48
|
+
end
|
49
|
+
|
50
|
+
def tree
|
51
|
+
@tree ||= hash.fetch('tree') { |key| raise "Book id=#{@id} is missing #{key}" }
|
52
|
+
end
|
53
|
+
|
54
|
+
def root_book_part
|
55
|
+
@root_book_part ||= BookPart.new(hash: tree, is_root: true, book: self)
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module OpenStax::Cnx::V1
|
2
|
+
class BookPart
|
3
|
+
|
4
|
+
def initialize(hash: {}, is_root: false, book: nil)
|
5
|
+
@hash = hash
|
6
|
+
@is_root = is_root
|
7
|
+
@book = book
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :hash, :is_root, :book
|
11
|
+
|
12
|
+
def parsed_title
|
13
|
+
@parsed_title ||= OpenStax::Cnx::V1::Baked.parse_title(
|
14
|
+
hash.fetch('title') { |key| raise "#{self.class.name} id=#{@id} is missing #{key}" }
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
def baked_book_location
|
19
|
+
@baked_book_location ||= parsed_title[:book_location]
|
20
|
+
end
|
21
|
+
|
22
|
+
def title
|
23
|
+
@title ||= parsed_title[:text]
|
24
|
+
end
|
25
|
+
|
26
|
+
def contents
|
27
|
+
@contents ||= hash.fetch('contents') do |key|
|
28
|
+
raise "#{self.class.name} id=#{@id} is missing #{key}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def parts
|
33
|
+
@parts ||= contents.map do |hash|
|
34
|
+
if hash.has_key? 'contents'
|
35
|
+
self.class.new(hash: hash, book: book)
|
36
|
+
else
|
37
|
+
OpenStax::Cnx::V1::Page.new(hash: hash, book: book)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def is_chapter?
|
43
|
+
# A BookPart is a chapter if none of its children are BookParts
|
44
|
+
@is_chapter ||= parts.none? { |part| part.is_a?(self.class) }
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,198 @@
|
|
1
|
+
module OpenStax::Cnx::V1
|
2
|
+
class Page
|
3
|
+
|
4
|
+
# Start parsing here
|
5
|
+
ROOT_CSS = 'html > body'
|
6
|
+
|
7
|
+
# Find nodes that define relevant tags
|
8
|
+
LO_DEF_NODE_CSS = '.ost-learning-objective-def'
|
9
|
+
STD_DEF_NODE_CSS = '.ost-standards-def'
|
10
|
+
TEKS_DEF_NODE_CSS = '.ost-standards-teks'
|
11
|
+
APBIO_DEF_NODE_CSS = '.ost-standards-apbio'
|
12
|
+
|
13
|
+
STD_NAME_NODE_CSS = '.ost-standards-name'
|
14
|
+
STD_DESC_NODE_CSS = '.ost-standards-description'
|
15
|
+
|
16
|
+
# Find specific tags and extract the relevant parts
|
17
|
+
LO_REGEX = /ost-tag-lo-([\w+-]+)/
|
18
|
+
STD_REGEX = /ost-tag-std-([\w+-]+)/
|
19
|
+
TEKS_REGEX = /ost-tag-(teks-[\w+-]+)/
|
20
|
+
|
21
|
+
def self.feature_node(node, feature_ids)
|
22
|
+
feature_ids = [feature_ids].flatten
|
23
|
+
return if feature_ids.empty?
|
24
|
+
|
25
|
+
feature_id_css = feature_ids.map { |feature_id| "##{feature_id}" }.join(', ')
|
26
|
+
node.at_css(feature_id_css)
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize(hash: {}, id: nil, url: nil, title: nil, content: nil, book: nil)
|
30
|
+
@hash = hash
|
31
|
+
@id = id
|
32
|
+
@url = url
|
33
|
+
@parsed_title = OpenStax::Cnx::V1::Baked.parse_title(title)
|
34
|
+
@content = content
|
35
|
+
@book = book
|
36
|
+
end
|
37
|
+
|
38
|
+
attr_accessor :chapter_section
|
39
|
+
attr_reader :hash, :book
|
40
|
+
|
41
|
+
def id
|
42
|
+
@id ||= hash.fetch('id') { |key| raise "Page is missing #{key}" }
|
43
|
+
end
|
44
|
+
|
45
|
+
def url
|
46
|
+
@url ||= url_for(id)
|
47
|
+
end
|
48
|
+
|
49
|
+
def parsed_title
|
50
|
+
@parsed_title ||= OpenStax::Cnx::V1::Baked.parse_title(
|
51
|
+
hash.fetch('title') { |key| raise "#{self.class.name} id=#{@id} is missing #{key}" }
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
def baked_book_location
|
56
|
+
parsed_title[:book_location]
|
57
|
+
end
|
58
|
+
|
59
|
+
def title
|
60
|
+
parsed_title[:text]
|
61
|
+
end
|
62
|
+
|
63
|
+
def is_intro?
|
64
|
+
return @is_intro unless @is_intro.nil?
|
65
|
+
# CNX plans to implement a better way to identify chapter intro pages
|
66
|
+
# This is a hack to be used until that happens
|
67
|
+
@is_intro = title.start_with?('Introduction')
|
68
|
+
end
|
69
|
+
|
70
|
+
def full_hash
|
71
|
+
@full_hash ||= OpenStax::Cnx::V1.fetch(url)
|
72
|
+
end
|
73
|
+
|
74
|
+
def uuid
|
75
|
+
@uuid ||= full_hash.fetch('id') { |key| raise "Book id=#{@id} is missing #{key}" }
|
76
|
+
end
|
77
|
+
|
78
|
+
def short_id
|
79
|
+
@short_id ||= full_hash.fetch('shortId', nil)
|
80
|
+
end
|
81
|
+
|
82
|
+
def version
|
83
|
+
@version ||= full_hash.fetch('version') { |key| raise "Book id=#{@id} is missing #{key}" }
|
84
|
+
end
|
85
|
+
|
86
|
+
def canonical_url
|
87
|
+
@canonical_url ||= url_for("#{uuid}@#{version}")
|
88
|
+
end
|
89
|
+
|
90
|
+
def content
|
91
|
+
@content ||= full_hash.fetch('content') { |key| raise "Page id=#{@id} is missing #{key}" }
|
92
|
+
end
|
93
|
+
|
94
|
+
def doc
|
95
|
+
@doc ||= Nokogiri::HTML(content)
|
96
|
+
end
|
97
|
+
|
98
|
+
def root
|
99
|
+
@root ||= doc.at_css(ROOT_CSS)
|
100
|
+
end
|
101
|
+
|
102
|
+
def los
|
103
|
+
@los ||= tags.select{ |tag| tag[:type] == :lo }.map{ |tag| tag[:value] }
|
104
|
+
end
|
105
|
+
|
106
|
+
def aplos
|
107
|
+
@aplos ||= tags.select{ |tag| tag[:type] == :aplo }.map{ |tag| tag[:value] }
|
108
|
+
end
|
109
|
+
|
110
|
+
def tags
|
111
|
+
return @tags.values unless @tags.nil?
|
112
|
+
|
113
|
+
# Start with default cnxmod tag
|
114
|
+
cnxmod_value = "context-cnxmod:#{uuid}"
|
115
|
+
@tags = { cnxmod_value => { value: cnxmod_value, type: :cnxmod } }
|
116
|
+
|
117
|
+
# Extract tag name and description from .ost-standards-def and .os-learning-objective-def.
|
118
|
+
|
119
|
+
# LO tags
|
120
|
+
root.css(LO_DEF_NODE_CSS).each do |node|
|
121
|
+
klass = node.attr('class')
|
122
|
+
lo_value = LO_REGEX.match(klass).try(:[], 1)
|
123
|
+
next if lo_value.nil?
|
124
|
+
|
125
|
+
teks_value = TEKS_REGEX.match(klass).try(:[], 1)
|
126
|
+
description = node.content.strip
|
127
|
+
|
128
|
+
@tags[lo_value] = {
|
129
|
+
value: lo_value,
|
130
|
+
description: description,
|
131
|
+
teks: teks_value,
|
132
|
+
type: :lo
|
133
|
+
}
|
134
|
+
end
|
135
|
+
|
136
|
+
# Other standards
|
137
|
+
root.css(STD_DEF_NODE_CSS).each do |node|
|
138
|
+
klass = node.attr('class')
|
139
|
+
name = node.at_css(STD_NAME_NODE_CSS).try(:content).try(:strip)
|
140
|
+
description = node.at_css(STD_DESC_NODE_CSS).try(:content).try(:strip)
|
141
|
+
value = nil
|
142
|
+
|
143
|
+
if node.matches?(TEKS_DEF_NODE_CSS)
|
144
|
+
value = TEKS_REGEX.match(klass).try(:[], 1)
|
145
|
+
type = :teks
|
146
|
+
elsif node.matches?(APBIO_DEF_NODE_CSS)
|
147
|
+
value = LO_REGEX.match(klass).try(:[], 1)
|
148
|
+
type = :aplo
|
149
|
+
end
|
150
|
+
|
151
|
+
next if value.nil?
|
152
|
+
|
153
|
+
@tags[value] = {
|
154
|
+
value: value,
|
155
|
+
name: name,
|
156
|
+
description: description,
|
157
|
+
type: type
|
158
|
+
}
|
159
|
+
end
|
160
|
+
|
161
|
+
@tags.values
|
162
|
+
end
|
163
|
+
|
164
|
+
protected
|
165
|
+
|
166
|
+
def url_for(path)
|
167
|
+
book.nil? ? OpenStax::Cnx::V1.archive_url_for(path) : "#{book.canonical_url}:#{path}"
|
168
|
+
end
|
169
|
+
|
170
|
+
# add container div around note content for styling
|
171
|
+
def map_note_format(node)
|
172
|
+
note_selector = <<-eos
|
173
|
+
.note:not(.learning-objectives),
|
174
|
+
.example,
|
175
|
+
.grasp-check,
|
176
|
+
[data-type="note"],
|
177
|
+
[data-element-type="check-understanding"]
|
178
|
+
eos
|
179
|
+
|
180
|
+
note_selector = note_selector.gsub(/\s+/, "")
|
181
|
+
|
182
|
+
node.css(note_selector).each do |note|
|
183
|
+
note.set_attribute('data-tutor-transform', true)
|
184
|
+
body = Nokogiri::XML::Node.new('div', doc)
|
185
|
+
body.set_attribute('data-type', 'content')
|
186
|
+
|
187
|
+
content = note.css('>*:not([data-type=title])')
|
188
|
+
content.unlink()
|
189
|
+
|
190
|
+
body.children = content
|
191
|
+
note.add_child(body)
|
192
|
+
end
|
193
|
+
|
194
|
+
node
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
end
|
data/lib/openstax_cnx.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "openstax/cnx/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "openstax_cnx"
|
8
|
+
spec.version = OpenStax::Cnx::VERSION
|
9
|
+
spec.authors = ["JP Slavinsky"]
|
10
|
+
spec.email = ["jps@rice.edu"]
|
11
|
+
|
12
|
+
spec.summary = %q{Bindings to fetch and inspect CNX books}
|
13
|
+
spec.description = %q{Bindings to fetch and inspect CNX books}
|
14
|
+
spec.homepage = "https://github.com/openstax/cnx-ruby"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
23
|
+
spec.metadata["source_code_uri"] = "https://github.com/openstax/cnx-ruby"
|
24
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
25
|
+
else
|
26
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
27
|
+
"public gem pushes."
|
28
|
+
end
|
29
|
+
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
32
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
34
|
+
end
|
35
|
+
spec.bindir = "exe"
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
+
spec.require_paths = ["lib"]
|
38
|
+
|
39
|
+
spec.add_dependency "nokogiri"
|
40
|
+
|
41
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
42
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
43
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
44
|
+
spec.add_development_dependency "dotenv"
|
45
|
+
spec.add_development_dependency "webmock"
|
46
|
+
spec.add_development_dependency "vcr"
|
47
|
+
spec.add_development_dependency "byebug"
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: openstax_cnx
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- JP Slavinsky
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.17'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.17'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: dotenv
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: vcr
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: byebug
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: Bindings to fetch and inspect CNX books
|
126
|
+
email:
|
127
|
+
- jps@rice.edu
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".travis.yml"
|
135
|
+
- CODE_OF_CONDUCT.md
|
136
|
+
- Gemfile
|
137
|
+
- Gemfile.lock
|
138
|
+
- LICENSE.txt
|
139
|
+
- README.md
|
140
|
+
- Rakefile
|
141
|
+
- bin/console
|
142
|
+
- bin/setup
|
143
|
+
- lib/openstax/cnx/v1.rb
|
144
|
+
- lib/openstax/cnx/v1/baked.rb
|
145
|
+
- lib/openstax/cnx/v1/book.rb
|
146
|
+
- lib/openstax/cnx/v1/book_part.rb
|
147
|
+
- lib/openstax/cnx/v1/http_error.rb
|
148
|
+
- lib/openstax/cnx/v1/page.rb
|
149
|
+
- lib/openstax/cnx/version.rb
|
150
|
+
- lib/openstax_cnx.rb
|
151
|
+
- openstax_cnx.gemspec
|
152
|
+
homepage: https://github.com/openstax/cnx-ruby
|
153
|
+
licenses:
|
154
|
+
- MIT
|
155
|
+
metadata:
|
156
|
+
allowed_push_host: https://rubygems.org
|
157
|
+
homepage_uri: https://github.com/openstax/cnx-ruby
|
158
|
+
source_code_uri: https://github.com/openstax/cnx-ruby
|
159
|
+
post_install_message:
|
160
|
+
rdoc_options: []
|
161
|
+
require_paths:
|
162
|
+
- lib
|
163
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: '0'
|
168
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
169
|
+
requirements:
|
170
|
+
- - ">="
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: '0'
|
173
|
+
requirements: []
|
174
|
+
rubyforge_project:
|
175
|
+
rubygems_version: 2.7.3
|
176
|
+
signing_key:
|
177
|
+
specification_version: 4
|
178
|
+
summary: Bindings to fetch and inspect CNX books
|
179
|
+
test_files: []
|