fetchworks 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +19 -0
- data/.standard.yml +3 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +113 -0
- data/LICENSE +8 -0
- data/README.md +88 -0
- data/Rakefile +10 -0
- data/fetchworks.gemspec +42 -0
- data/lib/fetchworks/partial_date.rb +56 -0
- data/lib/fetchworks/version.rb +5 -0
- data/lib/fetchworks.rb +199 -0
- data/sig/fetchworks.rbs +4 -0
- metadata +158 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: af1f3fec9f085e2ca7c23b9f5bb354f24ac22e2b0524fff2a3e93b4f6dc5bbd6
|
4
|
+
data.tar.gz: 59ec1f2eebbf3b9933cdfeb29bd703a8eb5054b828675c06af708085f1f8c340
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cb6ae78864bf29589bf07131418d9956d5f3b8a9945b0b3a6650f8f8ba187f2078edc6d05b9e42ef3def40df7ce039295dc1f217a1ce0135b78b216a1b5af9e7
|
7
|
+
data.tar.gz: ccb34a182083b9687478b755435389b10dc5aa2af02f18e27198ff9668e677bb7fe85836534726a5a1948b650284834aa26d439eef4cfb9a710ac1d2be81d341
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require:
|
2
|
+
- standard
|
3
|
+
- rubocop-rspec
|
4
|
+
|
5
|
+
inherit_gem:
|
6
|
+
standard: config/base.yml
|
7
|
+
|
8
|
+
Layout/EmptyLineBetweenDefs:
|
9
|
+
AllowAdjacentOneLineDefs: true
|
10
|
+
|
11
|
+
Layout/ArrayAlignment:
|
12
|
+
EnforcedStyle: with_first_element
|
13
|
+
|
14
|
+
RSpec/MultipleDescribes:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
AllCops:
|
18
|
+
NewCops: enable
|
19
|
+
|
data/.standard.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fetchworks (0.1.0)
|
5
|
+
open-uri
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.2)
|
11
|
+
base64 (0.1.1)
|
12
|
+
date (3.3.3)
|
13
|
+
debug (1.8.0)
|
14
|
+
irb (>= 1.5.0)
|
15
|
+
reline (>= 0.3.1)
|
16
|
+
diff-lcs (1.5.0)
|
17
|
+
io-console (0.6.0)
|
18
|
+
irb (1.8.1)
|
19
|
+
rdoc
|
20
|
+
reline (>= 0.3.8)
|
21
|
+
json (2.6.3)
|
22
|
+
language_server-protocol (3.17.0.3)
|
23
|
+
lint_roller (1.1.0)
|
24
|
+
open-uri (0.3.0)
|
25
|
+
stringio
|
26
|
+
time
|
27
|
+
uri
|
28
|
+
parallel (1.23.0)
|
29
|
+
parser (3.2.2.4)
|
30
|
+
ast (~> 2.4.1)
|
31
|
+
racc
|
32
|
+
psych (5.1.0)
|
33
|
+
stringio
|
34
|
+
racc (1.7.1)
|
35
|
+
rainbow (3.1.1)
|
36
|
+
rake (13.0.6)
|
37
|
+
rdoc (6.5.0)
|
38
|
+
psych (>= 4.0.0)
|
39
|
+
regexp_parser (2.8.1)
|
40
|
+
reline (0.3.9)
|
41
|
+
io-console (~> 0.5)
|
42
|
+
rexml (3.2.6)
|
43
|
+
rspec (3.12.0)
|
44
|
+
rspec-core (~> 3.12.0)
|
45
|
+
rspec-expectations (~> 3.12.0)
|
46
|
+
rspec-mocks (~> 3.12.0)
|
47
|
+
rspec-core (3.12.2)
|
48
|
+
rspec-support (~> 3.12.0)
|
49
|
+
rspec-expectations (3.12.3)
|
50
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
51
|
+
rspec-support (~> 3.12.0)
|
52
|
+
rspec-mocks (3.12.6)
|
53
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
54
|
+
rspec-support (~> 3.12.0)
|
55
|
+
rspec-support (3.12.1)
|
56
|
+
rubocop (1.56.4)
|
57
|
+
base64 (~> 0.1.1)
|
58
|
+
json (~> 2.3)
|
59
|
+
language_server-protocol (>= 3.17.0)
|
60
|
+
parallel (~> 1.10)
|
61
|
+
parser (>= 3.2.2.3)
|
62
|
+
rainbow (>= 2.2.2, < 4.0)
|
63
|
+
regexp_parser (>= 1.8, < 3.0)
|
64
|
+
rexml (>= 3.2.5, < 4.0)
|
65
|
+
rubocop-ast (>= 1.28.1, < 2.0)
|
66
|
+
ruby-progressbar (~> 1.7)
|
67
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
68
|
+
rubocop-ast (1.29.0)
|
69
|
+
parser (>= 3.2.1.0)
|
70
|
+
rubocop-capybara (2.19.0)
|
71
|
+
rubocop (~> 1.41)
|
72
|
+
rubocop-factory_bot (2.24.0)
|
73
|
+
rubocop (~> 1.33)
|
74
|
+
rubocop-performance (1.19.1)
|
75
|
+
rubocop (>= 1.7.0, < 2.0)
|
76
|
+
rubocop-ast (>= 0.4.0)
|
77
|
+
rubocop-rspec (2.24.1)
|
78
|
+
rubocop (~> 1.33)
|
79
|
+
rubocop-capybara (~> 2.17)
|
80
|
+
rubocop-factory_bot (~> 2.22)
|
81
|
+
ruby-progressbar (1.13.0)
|
82
|
+
standard (1.31.1)
|
83
|
+
language_server-protocol (~> 3.17.0.2)
|
84
|
+
lint_roller (~> 1.0)
|
85
|
+
rubocop (~> 1.56.2)
|
86
|
+
standard-custom (~> 1.0.0)
|
87
|
+
standard-performance (~> 1.2)
|
88
|
+
standard-custom (1.0.2)
|
89
|
+
lint_roller (~> 1.0)
|
90
|
+
rubocop (~> 1.50)
|
91
|
+
standard-performance (1.2.0)
|
92
|
+
lint_roller (~> 1.1)
|
93
|
+
rubocop-performance (~> 1.19.0)
|
94
|
+
stringio (3.0.8)
|
95
|
+
time (0.2.2)
|
96
|
+
date
|
97
|
+
unicode-display_width (2.5.0)
|
98
|
+
uri (0.12.2)
|
99
|
+
|
100
|
+
PLATFORMS
|
101
|
+
x86_64-linux
|
102
|
+
|
103
|
+
DEPENDENCIES
|
104
|
+
debug
|
105
|
+
fetchworks!
|
106
|
+
rake (~> 13.0)
|
107
|
+
rspec (~> 3.0)
|
108
|
+
rubocop
|
109
|
+
rubocop-rspec
|
110
|
+
standard (~> 1.3)
|
111
|
+
|
112
|
+
BUNDLED WITH
|
113
|
+
2.4.20
|
data/LICENSE
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
Copyright 2023 David Gumberg
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
8
|
+
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Fetchworks
|
2
|
+
|
3
|
+
A simple gem for fetching information about works and authors from various open databases. Today, Internet Archive's Open Library is supported.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add fetchworks
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem install fetchworks
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
```
|
18
|
+
donquixote = OpenLibraryBook.new("9780062391667")
|
19
|
+
|
20
|
+
# OpenLibraryBook.data returns a hash of all the JSON data returned by OL
|
21
|
+
donquixote.data
|
22
|
+
# => { "title": "Don Quixote Deluxe Edition", "number_of_pages": "992" [...] }
|
23
|
+
donquixote.data["title"] # => "Don Quixote Deluxe Edition"
|
24
|
+
|
25
|
+
# Use the author id's contained in a work, to request entries for the authors of a work
|
26
|
+
authors = donquixote.fetch_authors # => [#<OpenLibraryAuthor:0x01 @data={...}>, #<OpenLibraryAuthor:0x02 @data={...}> ...]
|
27
|
+
|
28
|
+
cervantes = authors[0] # => #<OpenLibraryAuthor:0x01>
|
29
|
+
cervantes.data
|
30
|
+
# => { "personal_name"=>"Miguel de Cervantes Saavedra",
|
31
|
+
# "birth_date"=>"29 Sep 1547" ... }
|
32
|
+
cervantes.data["personal_name"]
|
33
|
+
|
34
|
+
# JSON data for OpenLibraryBook and OpenLibraryAuthor can be accessed with methods:
|
35
|
+
donquixote.number_of_pages # => "992"
|
36
|
+
cervantes.bio # => "Miguel de Cervantes Saavedra was a Spanish novelist, poet, [...]"
|
37
|
+
|
38
|
+
```
|
39
|
+
|
40
|
+
### Dates
|
41
|
+
|
42
|
+
Given the partial nature of historical dates (oftentimes a year is known but a
|
43
|
+
month or day isn't), we provide a class `PartialDate` to represent dates that
|
44
|
+
are accessed via method names:
|
45
|
+
|
46
|
+
```
|
47
|
+
cervantes.birth_date # => #<PartialDate:0x01 @day=29, @month=9, @year=1547>
|
48
|
+
cervantes.birth_date.year # => 1547
|
49
|
+
|
50
|
+
donquixote.publish_date # => #<PartialDate:0x02 @day=16, @month=6, @year=2015>
|
51
|
+
donquixote.publish_date.day # => 16
|
52
|
+
```
|
53
|
+
|
54
|
+
If you still want to access the original string format of a date that
|
55
|
+
OpenLibrary provides, you can through the `@data` hash.
|
56
|
+
|
57
|
+
```
|
58
|
+
cervantes.data["birth_date"] # => "29 Sep 1547"
|
59
|
+
```
|
60
|
+
|
61
|
+
`PartialDate` is furnished with the instance methods: `#year`, `#month`, and `#day`, which
|
62
|
+
work as expected when a value is present, and return `nil` when one isn't. We also provide
|
63
|
+
convenience methods `#to_date` and `#to_time` which will return a ruby builtin `Date` or `Time`
|
64
|
+
object with the smallest possible values for the unknown attributes of the date.
|
65
|
+
|
66
|
+
```
|
67
|
+
herodotus.birth_date # => #<PartialDate:0x01 @day=nil, @month=nil, @year=-484>
|
68
|
+
herodotus.birth_date.month # => nil
|
69
|
+
herodotus.birth_date.day # => nil
|
70
|
+
|
71
|
+
ruby_date = herodotus.birth_date.to_date # => #<Date:...>
|
72
|
+
ruby_date.month # => 1
|
73
|
+
ruby_date.day # => 1
|
74
|
+
|
75
|
+
ruby_time = herodotus.birth_date.to_time # => #<Time:...>
|
76
|
+
ruby_time.hour => 0
|
77
|
+
ruby_time.sec => 0
|
78
|
+
```
|
79
|
+
|
80
|
+
## Development
|
81
|
+
|
82
|
+
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.
|
83
|
+
|
84
|
+
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).
|
85
|
+
|
86
|
+
## Contributing
|
87
|
+
|
88
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/davidgumberg/fetchworks.
|
data/Rakefile
ADDED
data/fetchworks.gemspec
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/fetchworks/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "fetchworks"
|
7
|
+
spec.version = Fetchworks::VERSION
|
8
|
+
spec.authors = ["David Gumberg"]
|
9
|
+
spec.email = ["davidzgumberg@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "A simple gem for programmatic access of data about written works."
|
12
|
+
spec.description = "A simple gem for programmatic access of data about written works.
|
13
|
+
At present, it has support for the OpenLibrary API"
|
14
|
+
|
15
|
+
spec.homepage = "https://github.com/davidgumberg/fetchworks"
|
16
|
+
spec.required_ruby_version = ">= 2.6.0"
|
17
|
+
spec.license = "MIT"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/davidgumberg/fetchworks"
|
21
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(__dir__) do
|
26
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
|
34
|
+
spec.add_dependency "open-uri"
|
35
|
+
|
36
|
+
spec.add_development_dependency "debug"
|
37
|
+
spec.add_development_dependency "rake"
|
38
|
+
spec.add_development_dependency "rspec"
|
39
|
+
spec.add_development_dependency "rubocop"
|
40
|
+
spec.add_development_dependency "rubocop-rspec"
|
41
|
+
spec.add_development_dependency "standard"
|
42
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "time"
|
4
|
+
|
5
|
+
# Date representation that permits nil values
|
6
|
+
# We need this because of the nature of date values for works
|
7
|
+
# Oftentimes a year is known, but a month, or day is not.
|
8
|
+
class PartialDate
|
9
|
+
class PartialDateError < StandardError; end
|
10
|
+
class PartialDateArgError < PartialDateError; end
|
11
|
+
attr_accessor :year, :month, :day
|
12
|
+
|
13
|
+
def to_date
|
14
|
+
Date.new(year, (month || 1), (day || 1))
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_time
|
18
|
+
date.to_time
|
19
|
+
end
|
20
|
+
|
21
|
+
def <=>(other)
|
22
|
+
to_date <=> other.to_date
|
23
|
+
end
|
24
|
+
|
25
|
+
def ==(other)
|
26
|
+
self.class == other.class &&
|
27
|
+
@year == other.year &&
|
28
|
+
@month == other.month &&
|
29
|
+
@day == other.day
|
30
|
+
end
|
31
|
+
|
32
|
+
# We accept three formats, Date, Time (which we drop down to a Date),
|
33
|
+
# and an array of up three integers in the format: [year, month, day]
|
34
|
+
# TODO: Validate array input
|
35
|
+
def initialize(arg)
|
36
|
+
if arg.is_a? Time
|
37
|
+
arg = arg.to_date
|
38
|
+
end
|
39
|
+
|
40
|
+
if arg.is_a? Date
|
41
|
+
arg = [arg.year, arg.month, arg.day]
|
42
|
+
end
|
43
|
+
|
44
|
+
unless arg.is_a? Array
|
45
|
+
raise PartialDateArgError, "Did not receive a Date, Time, or Array"
|
46
|
+
end
|
47
|
+
|
48
|
+
unless arg.length.between?(1, 3)
|
49
|
+
raise PartialDateArgError, "Received array of incorrect length."
|
50
|
+
end
|
51
|
+
|
52
|
+
@year = arg[0]
|
53
|
+
@month = arg[1]
|
54
|
+
@day = arg[2]
|
55
|
+
end
|
56
|
+
end
|
data/lib/fetchworks.rb
ADDED
@@ -0,0 +1,199 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
OPENURI_OPTS = {
|
4
|
+
read_timeout: 10,
|
5
|
+
open_timeout: 10
|
6
|
+
}.freeze
|
7
|
+
|
8
|
+
require_relative "fetchworks/version"
|
9
|
+
require_relative "fetchworks/partial_date"
|
10
|
+
|
11
|
+
require "json"
|
12
|
+
require "open-uri"
|
13
|
+
require "time"
|
14
|
+
|
15
|
+
HOST = "openlibrary.org"
|
16
|
+
PATH = "/api/books"
|
17
|
+
QUERY_POSTFIX = "&jscmd=data&format=json"
|
18
|
+
|
19
|
+
# Mixin Module for classes with a hash attribute @data
|
20
|
+
# That routes classinstance.method to classinstance.data[:method]
|
21
|
+
# TODO: infect nested hashes and arrays with this module.
|
22
|
+
|
23
|
+
module HashMethodable
|
24
|
+
# Expose hash members as methods
|
25
|
+
def method_missing(method_name, *_args, &_block)
|
26
|
+
key = method_name&.to_s
|
27
|
+
@data&.key?(key) ? @data[key] : nil
|
28
|
+
end
|
29
|
+
|
30
|
+
# Advertise hash member methods
|
31
|
+
def respond_to_missing?(method_name, include_private = false)
|
32
|
+
key = method_name&.to_s
|
33
|
+
@data&.key?(key) || super
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Provides methods for accessing OpenLibrary resources
|
38
|
+
module OpenLibrary
|
39
|
+
# Declare error classes
|
40
|
+
class InvalidISBN < StandardError; end
|
41
|
+
class OpenLibraryError < StandardError; end
|
42
|
+
class OLBadStatus < OpenLibraryError; end
|
43
|
+
class OLResourceNotFound < OpenLibraryError; end
|
44
|
+
class OLDateStrUnparseable < OpenLibraryError; end
|
45
|
+
|
46
|
+
def self.get_book(isbn)
|
47
|
+
raise InvalidISBN unless isbn.is_a?(Integer) || isbn.is_a?(String)
|
48
|
+
|
49
|
+
isbn = isbn.to_s if isbn.is_a? Integer
|
50
|
+
isbn = isbn.strip
|
51
|
+
|
52
|
+
# Primitive ISBN check: regex match for a 10 or 12 digit number
|
53
|
+
# TODO: check prefix and checksum
|
54
|
+
raise InvalidISBN unless /\A\d{10}(\d{3})?\z/.match? isbn
|
55
|
+
|
56
|
+
query = "bibkeys=ISBN:#{isbn}#{QUERY_POSTFIX}"
|
57
|
+
uri = URI::HTTPS.build(host: HOST, path: PATH, query: query)
|
58
|
+
response = uri.open(**OPENURI_OPTS)
|
59
|
+
|
60
|
+
if response&.status != ["200", "OK"]
|
61
|
+
raise OLBadStatus, response&.status
|
62
|
+
end
|
63
|
+
|
64
|
+
JSON.parse(response.read).values.first
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.get_author(url)
|
68
|
+
url = url[%r{\A.*(?=/)}] + ".json"
|
69
|
+
uri = URI.parse(url)
|
70
|
+
JSON.parse(uri.open.read)
|
71
|
+
end
|
72
|
+
|
73
|
+
# Returns a PartialDate from strings of one of the following four formats:
|
74
|
+
# "YYYY" | "Month YYYY" | "Month DD, YYYY" | "DD Month, YYYY"
|
75
|
+
#
|
76
|
+
# "Month"s are abbreviated usually, but sometimes not.
|
77
|
+
#
|
78
|
+
# This gets nasty, because we have a number of formats when two
|
79
|
+
# or three elements are present. We decide which one based off
|
80
|
+
# of which member is a recognized month name.
|
81
|
+
def self.partial_date_from_str(str)
|
82
|
+
return nil unless str.is_a?(String) && !str.empty?
|
83
|
+
|
84
|
+
# Replace commas with spaces and part out the date string
|
85
|
+
split = str.tr(",", " ").split
|
86
|
+
|
87
|
+
PartialDate.new(
|
88
|
+
case split.length
|
89
|
+
# e.g. "1991"
|
90
|
+
when 1
|
91
|
+
[split[0].to_i]
|
92
|
+
|
93
|
+
when 2
|
94
|
+
# e.g. "Jan 1991"
|
95
|
+
if Date::ABBR_MONTHNAMES.include?(split[0])
|
96
|
+
[split[1].to_i,
|
97
|
+
Date::ABBR_MONTHNAMES.index(split[0])]
|
98
|
+
|
99
|
+
# e.g. "January 1991"
|
100
|
+
elsif Date::MONTHNAMES.include?(split[0])
|
101
|
+
[split[1].to_i,
|
102
|
+
Date::MONTHNAMES.index(split[0])]
|
103
|
+
else
|
104
|
+
raise OLDateStrUnparseable
|
105
|
+
end
|
106
|
+
|
107
|
+
when 3
|
108
|
+
# e.g. "Jan 12, 1991"
|
109
|
+
if Date::ABBR_MONTHNAMES.include?(split[0])
|
110
|
+
[split[2].to_i,
|
111
|
+
Date::ABBR_MONTHNAMES.index(split[0]),
|
112
|
+
split[1].to_i]
|
113
|
+
|
114
|
+
# e.g. "January 12, 1991"
|
115
|
+
elsif Date::MONTHNAMES.include?(split[0])
|
116
|
+
[split[2].to_i,
|
117
|
+
Date::MONTHNAMES.index(split[0]),
|
118
|
+
split[1].to_i]
|
119
|
+
|
120
|
+
# e.g. "12 Jan 1991"
|
121
|
+
elsif Date::ABBR_MONTHNAMES.include?(split[1])
|
122
|
+
[split[2].to_i,
|
123
|
+
Date::ABBR_MONTHNAMES.index(split[1]),
|
124
|
+
split[0].to_i]
|
125
|
+
|
126
|
+
# e.g. "12 January 1991"
|
127
|
+
elsif Date::MONTHNAMES.include?(split[1])
|
128
|
+
[split[2].to_i,
|
129
|
+
Date::MONTHNAMES.index(split[1]),
|
130
|
+
split[0].to_i]
|
131
|
+
else
|
132
|
+
raise OLDateStrUnparseable
|
133
|
+
end
|
134
|
+
end
|
135
|
+
)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
# Methods common to OpenLibraryBook's and OpenLibraryAuthor's
|
140
|
+
# Exposes the JSON representation of an OL entry as a hash
|
141
|
+
# through @data, and as methods with method_missing,
|
142
|
+
class OpenLibraryEntry
|
143
|
+
include HashMethodable
|
144
|
+
|
145
|
+
attr_reader :data
|
146
|
+
|
147
|
+
def initialize(data)
|
148
|
+
raise StandardError unless data.is_a? Hash
|
149
|
+
|
150
|
+
@data = data
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
# A class that represents authors in the OpenLibrary database
|
155
|
+
class OpenLibraryAuthor < OpenLibraryEntry
|
156
|
+
# Can only access authors at present via
|
157
|
+
# the URL provided for them in book data
|
158
|
+
def initialize(url)
|
159
|
+
super OpenLibrary.get_author(url)
|
160
|
+
end
|
161
|
+
|
162
|
+
def birth_date
|
163
|
+
str = @data["birth_date"]
|
164
|
+
OpenLibrary.partial_date_from_str(str)
|
165
|
+
end
|
166
|
+
|
167
|
+
def death_date
|
168
|
+
str = @data["death_date"]
|
169
|
+
OpenLibrary.partial_date_from_str(str)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
# A class that represents books in the OpenLibrary database
|
174
|
+
# It exposes the JSON representation that OL provides through their API
|
175
|
+
# as a hash through @data, and as methods with method_missing,
|
176
|
+
# and handles some comple
|
177
|
+
class OpenLibraryBook < OpenLibraryEntry
|
178
|
+
# Identifier is assumed to be ISBN10/ISBN13
|
179
|
+
# TODO: Add a check or identification type
|
180
|
+
def initialize(ident)
|
181
|
+
super OpenLibrary.get_book(ident)
|
182
|
+
end
|
183
|
+
|
184
|
+
def publish_date
|
185
|
+
str = @data["publish_date"]
|
186
|
+
OpenLibrary.partial_date_from_str(str)
|
187
|
+
end
|
188
|
+
|
189
|
+
def fetch_authors
|
190
|
+
authors.collect do |author|
|
191
|
+
OpenLibraryAuthor.new author["url"]
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
# module Fetchworks
|
197
|
+
# class Error < StandardError; end
|
198
|
+
# # Your code goes here...
|
199
|
+
# end
|
data/sig/fetchworks.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fetchworks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Gumberg
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-10-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: open-uri
|
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: debug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '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: '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'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
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: rubocop-rspec
|
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: standard
|
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
|
+
description: |-
|
112
|
+
A simple gem for programmatic access of data about written works.
|
113
|
+
At present, it has support for the OpenLibrary API
|
114
|
+
email:
|
115
|
+
- davidzgumberg@gmail.com
|
116
|
+
executables: []
|
117
|
+
extensions: []
|
118
|
+
extra_rdoc_files: []
|
119
|
+
files:
|
120
|
+
- ".rspec"
|
121
|
+
- ".rubocop.yml"
|
122
|
+
- ".standard.yml"
|
123
|
+
- Gemfile
|
124
|
+
- Gemfile.lock
|
125
|
+
- LICENSE
|
126
|
+
- README.md
|
127
|
+
- Rakefile
|
128
|
+
- fetchworks.gemspec
|
129
|
+
- lib/fetchworks.rb
|
130
|
+
- lib/fetchworks/partial_date.rb
|
131
|
+
- lib/fetchworks/version.rb
|
132
|
+
- sig/fetchworks.rbs
|
133
|
+
homepage: https://github.com/davidgumberg/fetchworks
|
134
|
+
licenses:
|
135
|
+
- MIT
|
136
|
+
metadata:
|
137
|
+
homepage_uri: https://github.com/davidgumberg/fetchworks
|
138
|
+
source_code_uri: https://github.com/davidgumberg/fetchworks
|
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: 2.6.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.4.17
|
155
|
+
signing_key:
|
156
|
+
specification_version: 4
|
157
|
+
summary: A simple gem for programmatic access of data about written works.
|
158
|
+
test_files: []
|