bok_choy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 43d055eafd9af6679bee016cb2b4dfd03ad76b8debba36c94ccbaf137053ac85
4
+ data.tar.gz: 819fe37ee40adff0d6bedfe98c6105cbe737dffaca202222c52b22489e7dc990
5
+ SHA512:
6
+ metadata.gz: 5503ffdf0f89da4f9618b3ffeacdc6725e59f932a4eb9578aa11acc82ecef55769da86c3ff57911f4780985aa4462c9768f1d51353895a00d461f6dad48c8c13
7
+ data.tar.gz: bc37a4f7606991fcb59dcf9c9d3f4925e0611c6538a217e8e687360e1cbaa80a7b1f9fd5dad0e821ca2b56b2e351fd65409475d7e3e65c50cfa9139b06a674c2
@@ -0,0 +1,19 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.1.3
14
+ bundler-cache: true
15
+ - name: Run the default task
16
+ run: bundle exec rake
17
+ env:
18
+ USER: ${{ vars.USER }}
19
+ PASSWORD: ${{ secrets.PASSWORD }}
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ .byebug_history
2
+ /.bundle/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.0.1] - 2024-09-11
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at diapriid@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in bok_choy.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bok_choy (0.0.1)
5
+ faraday (~> 2.2)
6
+ faraday-follow_redirects (>= 0.1, < 0.4)
7
+ multi_json (~> 1.15)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ addressable (2.8.1)
13
+ public_suffix (>= 2.0.2, < 6.0)
14
+ byebug (11.1.3)
15
+ crack (0.4.5)
16
+ rexml
17
+ faraday (2.11.0)
18
+ faraday-net_http (>= 2.0, < 3.4)
19
+ logger
20
+ faraday-follow_redirects (0.3.0)
21
+ faraday (>= 1, < 3)
22
+ faraday-net_http (3.3.0)
23
+ net-http
24
+ hashdiff (1.0.1)
25
+ logger (1.6.1)
26
+ minitest (5.15.0)
27
+ multi_json (1.15.0)
28
+ net-http (0.4.1)
29
+ uri
30
+ power_assert (2.0.1)
31
+ public_suffix (5.0.1)
32
+ rake (13.0.6)
33
+ rexml (3.2.5)
34
+ test-unit (3.5.3)
35
+ power_assert
36
+ uri (0.13.1)
37
+ vcr (6.0.0)
38
+ webmock (3.18.1)
39
+ addressable (>= 2.8.0)
40
+ crack (>= 0.3.2)
41
+ hashdiff (>= 0.4.0, < 2.0.0)
42
+
43
+ PLATFORMS
44
+ x86_64-linux
45
+
46
+ DEPENDENCIES
47
+ bok_choy!
48
+ bundler (~> 2.1, >= 2.1.4)
49
+ byebug
50
+ minitest (~> 5.0)
51
+ rake (~> 13.0)
52
+ test-unit (~> 3.3, >= 3.3.6)
53
+ vcr (~> 6.0)
54
+ webmock (~> 3.18)
55
+
56
+ BUNDLED WITH
57
+ 2.3.24
data/LICENSE.txt ADDED
@@ -0,0 +1,13 @@
1
+ Copyright © 2023 Species File Group. All rights reserved.
2
+
3
+ Developed by: Species File Group
4
+ Illinois Natural History Survey
5
+ https://github.com/SpeciesFileGroup/bok_choy
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal with 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:
8
+
9
+ Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimers.
10
+ Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimers in the documentation and/or other materials provided with the distribution.
11
+ Neither the names of <Name of Development Group, Name of Institution>, nor the names of its contributors may be used to endorse or promote products derived from this Software without specific prior written permission.
12
+
13
+ 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 CONTRIBUTORS 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 WITH THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # BokChoy
2
+
3
+ BokChoy is a Ruby wrapper on the [BHLNames](https://github.com/gnames/bhlnames) API. Code follow the spirit/approach of the Gem [serrano](https://github.com/sckott/serrano), and indeed much of the wrapping utility is copied 1:1 from that repo, thanks [@sckott](https://github.com/sckott).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bok_choy'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bok_choy
20
+
21
+ ## Usage
22
+
23
+ ### [Name references](https://bhlnames.globalnames.org/apidoc/index.html#/default/post-name-refs)
24
+ Although BHLnames offers a GET endpoint, the API wrapper always encodes the input parameters into JSON and uses the POST endpoint.
25
+ For more control of the input parameters, you can omit all other parameters and directly post JSON. See the [API docs](https://bhlnames.globalnames.org/apidoc/index.html#/default/post-name-refs) for the full list of possible inputs in the JSON.
26
+
27
+ Find BHL reference matches for a scientific name
28
+ ```ruby
29
+ BokChoy.name_refs(name: 'Pardosa moesta') # => Hash
30
+ ```
31
+
32
+ Find BHL reference matches with a scientific name, author and year:
33
+ ```ruby
34
+ BokChoy.name_refs(name: 'Pardosa moesta', author: 'Banks', year: 1892, reference: 'Bulletin of the American Museum of Natural History v.29 (1911)', nomen_event: false) # => Hash
35
+ ```
36
+
37
+ Find BHL reference matches for a scientific name with a reference string:
38
+ ```ruby
39
+ BokChoy.name_refs(name: 'Pardosa moesta', reference: 'Bulletin of the American Museum of Natural History v.29 (1911)') # => Hash
40
+ ```
41
+
42
+ Find BHL reference matches for a nomenclatural event:
43
+ ```ruby
44
+ BokChoy.name_refs(name: 'Pardosa moesta', nomen_event: true) # => Hash
45
+ ```
46
+
47
+ Find BHL reference matches with json input:
48
+ ```ruby
49
+ BokChoy.name_refs(json: {"name": {"nameString": "Pardosa moesta", "author": "Banks", "year": 1892}, "reference": {}, "params": {"nomenEvent": true}}) # => Hash
50
+ ```
51
+
52
+ ---
53
+ ### [Item metadata](https://bhlnames.globalnames.org/apidoc/index.html#/default/get-item)
54
+ Get BHL item metadata by item ID:
55
+ ```ruby
56
+ BokChoy.items(73397) # => Hash
57
+ ```
58
+
59
+ ---
60
+ ### [Page reference metadata](https://bhlnames.globalnames.org/apidoc/index.html#/default/get-refs)
61
+ Get BHL reference metadata by a page ID:
62
+ ```ruby
63
+ BokChoy.references(6589171) # => Hash
64
+ ```
65
+
66
+ ---
67
+ ### [External identifiers](https://bhlnames.globalnames.org/apidoc/index.html#/default/get-cached-refs)
68
+ Get a nomenclatural event in BHL by an external identifier:
69
+ ```ruby
70
+ BokChoy.cached_refs('6C8Q2') # => Hash
71
+ ```
72
+
73
+ ---
74
+ ### [Ping](https://bhlnames.globalnames.org/apidoc/index.html#/default/get-ping)
75
+ Test the status of the API service:
76
+ ```ruby
77
+ BokChoy.ping # => String
78
+ ```
79
+
80
+ ---
81
+ ### [Version](https://bhlnames.globalnames.org/apidoc/index.html#/default/get-version)
82
+ Get the version of the BHLnames API service:
83
+ ```ruby
84
+ BokChoy.version # => Hash
85
+ ```
86
+
87
+ ---
88
+
89
+ ## Development
90
+
91
+ 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.
92
+
93
+ 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`, update the `CHANGELOG.md`, 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).
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/SpeciesFileGroup/bok_choy. 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/SpeciesFileGroup/bok_choy/blob/main/CODE_OF_CONDUCT.md).
98
+
99
+ ## License
100
+
101
+ The gem is available as open source under the terms of the [NCSA/Illinois](https://github.com/SpeciesFileGroup/bok_choy/blob/main/LICENSE.txt) license. You can learn more about the NCSA license on [Wikipedia](https://en.wikipedia.org/wiki/University_of_Illinois/NCSA_Open_Source_License) and compare it with other open source licenses at the [Open Source Initiative](https://opensource.org/license/uoi-ncsa-php/).
102
+
103
+ ## Code of Conduct
104
+
105
+ Everyone interacting in the BokChoy project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/SpeciesFileGroup/bok_choy/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ t.verbose = true
11
+ end
12
+
13
+ desc "Run tests"
14
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "bok_choy"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bok_choy.gemspec ADDED
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/bok_choy/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "bok_choy"
7
+ s.version = BokChoy::VERSION
8
+ s.authors = ["Geoff Ower, Matt Yoder"]
9
+ s.email = ["gdower@illinois.edu"]
10
+
11
+ s.summary = "BHL Names Client"
12
+ s.description = "Bok Choy is a low-level wrapper around the Bionomia API."
13
+ s.homepage = "https://github.com/SpeciesFileGroup/bok_choy"
14
+ s.license = "NCSA"
15
+ s.required_ruby_version = ">= 2.4.0"
16
+
17
+ # s.metadata["allowed_push_host"] = "TODO: Set to 'https://mygemserver.com'"
18
+
19
+ s.metadata["homepage_uri"] = s.homepage
20
+ s.metadata["source_code_uri"] = "https://github.com/SpeciesFileGroup/bok_choy"
21
+ s.metadata["changelog_uri"] = "https://github.com/SpeciesFileGroup/bok_choy/releases/tag/v#{s.version}"
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
+ s.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|s|features)/}) }
27
+ end
28
+ s.bindir = "exe"
29
+ s.executables = s.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ s.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # s.add_dependency "example-gem", "~> 1.0"
34
+
35
+ s.add_development_dependency "bundler", "~> 2.1", ">= 2.1.4"
36
+ # s.add_development_dependency "codecov", "~> 0.5.0"
37
+ # s.add_development_dependency "json", "~> 2.3", ">= 2.3.1"
38
+ s.add_development_dependency "rake", "~> 13.0", ">= 13.0.1"
39
+ # s.add_development_dependency "standard", "~> 1.0"
40
+ # s.add_development_dependency "simplecov", "~> 0.21.2"
41
+ s.add_development_dependency "test-unit", "~> 3.3", ">= 3.3.6"
42
+ s.add_development_dependency "vcr", "~> 6.0"
43
+ s.add_development_dependency "webmock", "~> 3.18"
44
+
45
+ s.add_runtime_dependency "faraday", "~> 2.2"
46
+ s.add_runtime_dependency "faraday-follow_redirects", ">= 0.1", "< 0.4"
47
+ s.add_runtime_dependency "multi_json", "~> 1.15"
48
+
49
+ # TODO: comment out
50
+ s.add_development_dependency "byebug"
51
+
52
+ # s.add_runtime_dependency "thor", "~> 1.0", ">= 1.0.1"
53
+
54
+ # For more information and examples about making a new gem, checkout our
55
+ # guide at: https://bundler.io/guides/creating_gem.html
56
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BokChoy
4
+ # Custom error class for rescuing from all BokChoy errors
5
+ class Error < StandardError; end
6
+
7
+ # Raised when Bionomia returns the HTTP status code 400
8
+ class BadRequest < Error; end
9
+
10
+ # Raised when Bionomia returns the HTTP status code 404
11
+ class NotFound < Error; end
12
+
13
+ # Raised when Bionomia returns the HTTP status code 500
14
+ class InternalServerError < Error; end
15
+
16
+ # Raised when Bionomia returns the HTTP status code 502
17
+ class BadGateway < Error; end
18
+
19
+ # Raised when Bionomia returns the HTTP status code 503
20
+ class ServiceUnavailable < Error; end
21
+
22
+ # Raised when Bionomia returns the HTTP status code 504
23
+ class GatewayTimeout < Error; end
24
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "multi_json"
5
+
6
+ # @private
7
+ module Faraday
8
+ module BokChoyErrors
9
+ # @private
10
+ class Middleware < Faraday::Middleware
11
+ def call(env)
12
+ @app.call(env).on_complete do |response|
13
+ case response[:status].to_i
14
+ when 400
15
+ raise BokChoy::BadRequest, error_message_400(response)
16
+ when 404
17
+ raise BokChoy::NotFound, error_message_400(response)
18
+ when 500
19
+ raise BokChoy::InternalServerError, error_message_500(response, "Something is technically wrong.")
20
+ when 502
21
+ raise BokChoy::BadGateway, error_message_500(response, "The server returned an invalid or incomplete response.")
22
+ when 503
23
+ raise BokChoy::ServiceUnavailable, error_message_500(response, "Crossref is rate limiting your requests.")
24
+ when 504
25
+ raise BokChoy::GatewayTimeout, error_message_500(response, "504 Gateway Time-out")
26
+ end
27
+ end
28
+ end
29
+
30
+ def initialize(app)
31
+ super app
32
+ @parser = nil
33
+ end
34
+
35
+ private
36
+
37
+ def error_message_400(response)
38
+ "\n #{response[:method].to_s.upcase} #{response[:url]}\n Status #{response[:status]}#{error_body(response[:body])}"
39
+ end
40
+
41
+ def error_body(body)
42
+ if !body.nil? && !body.empty? && body.is_a?(String)
43
+ if json?(body)
44
+ body = ::MultiJson.load(body)
45
+ if body["message"].nil?
46
+ body = nil
47
+ else
48
+ body = body["message"]
49
+ end
50
+ end
51
+ end
52
+
53
+ if body.nil?
54
+ nil
55
+ else
56
+ ": #{body}"
57
+ end
58
+ end
59
+
60
+ def error_message_500(response, body = nil)
61
+ "#{response[:method].to_s.upcase} #{response[:url]}: #{[response[:status].to_s + ":", body].compact.join(" ")}"
62
+ end
63
+
64
+ def json?(string)
65
+ MultiJson.load(string)
66
+ true
67
+ rescue MultiJson::ParseError
68
+ false
69
+ end
70
+ end
71
+ end
72
+ end
73
+
@@ -0,0 +1,26 @@
1
+ # taken from https://raw.githubusercontent.com/sckott/serrano/main/lib/serrano/helpers/configuration.rb
2
+ # taken from: https://viget.com/extend/easy-gem-configuration-variables-with-defaults
3
+ module Configuration
4
+ def configuration
5
+ yield self
6
+ end
7
+
8
+ def define_setting(name, default = nil)
9
+ class_variable_set("@@#{name}", default)
10
+ define_class_method "#{name}=" do |value|
11
+ class_variable_set("@@#{name}", value)
12
+ end
13
+ define_class_method name do
14
+ class_variable_get("@@#{name}")
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def define_class_method(name, &block)
21
+ (class << self; self; end).instance_eval do
22
+ define_method name, &block
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,64 @@
1
+ require_relative "faraday" # !! Potential ruby 3.0 difference in module loading? relative differs from Serrano
2
+ require "faraday/follow_redirects"
3
+ require_relative "utils"
4
+
5
+ module BokChoy
6
+
7
+ class Request
8
+ attr_accessor :endpoint
9
+ attr_accessor :q
10
+ attr_accessor :verbose
11
+
12
+ attr_accessor :options
13
+
14
+ def initialize(**args)
15
+ @endpoint = args[:endpoint]
16
+ @verbose = args[:verbose]
17
+ @q = args[:q]
18
+ @json = args[:json].to_json
19
+ @options = args[:options] # TODO: not added at bok_choy.rb
20
+ end
21
+
22
+ def perform
23
+
24
+ args = {
25
+ json: @json
26
+ }
27
+ opts = args.delete_if { |_k, v| v.nil? }
28
+
29
+ Faraday::Utils.default_space_encoding = "+"
30
+
31
+ conn = if verbose
32
+ Faraday.new(url: BokChoy.base_url) do |f|
33
+ f.response :logger
34
+ f.use Faraday::BokChoyErrors::Middleware
35
+ f.adapter Faraday.default_adapter
36
+ end
37
+ else
38
+ Faraday.new(url: BokChoy.base_url) do |f|
39
+ f.use Faraday::BokChoyErrors::Middleware
40
+ f.adapter Faraday.default_adapter
41
+ end
42
+ end
43
+
44
+ conn.headers['Accept'] = 'application/json,*/*'
45
+ conn.headers[:user_agent] = make_user_agent
46
+ conn.headers["X-USER-AGENT"] = make_user_agent
47
+ conn.headers['Content-Type'] = 'application/json'
48
+
49
+ if %w[name_refs].include? endpoint
50
+ res = conn.post(endpoint, opts[:json])
51
+ else
52
+ res = conn.get(endpoint, opts)
53
+ end
54
+
55
+ # Handles endpoints that do not return JSON
56
+ begin
57
+ MultiJson.load(res.body)
58
+ rescue MultiJson::ParseError
59
+ res.body
60
+ end
61
+
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ # From https://raw.githubusercontent.com/sckott/serrano/main/lib/serrano/utils.rb
4
+
5
+ def make_user_agent
6
+ requa = "Faraday/v" + Faraday::VERSION
7
+ habua = "BokChoy/v" + BokChoy::VERSION
8
+ ua = requa + " " + habua
9
+ if BokChoy.mailto
10
+ ua += " (mailto:%s)" % BokChoy.mailto
11
+ end
12
+ # ua += format(' (mailto:%s)', BokChoy.mailto) if BokChoy.mailto
13
+ ua
14
+ end
15
+
16
+ def field_query_handler(x)
17
+ tmp = x.keep_if { |z| z.match(/query_/) }
18
+ rename_query_filters(tmp)
19
+ end
20
+
21
+ def rename_query_filters(foo)
22
+ foo = foo.tostrings
23
+ foo = foo.map { |x, y| [x.to_s.sub("container_title", "container-title"), y] }.to_h
24
+ foo.map { |x, y| [x.to_s.sub("query_", "query."), y] }.to_h
25
+ end
26
+
27
+ class Hash
28
+ def tostrings
29
+ map { |(k, v)| [k.to_s, v] }.to_h
30
+ end
31
+ end
32
+
33
+ class Hash
34
+ def tosymbols
35
+ map { |(k, v)| [k.to_sym, v] }.to_h
36
+ end
37
+ end
38
+
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BokChoy
4
+ VERSION = "0.0.1"
5
+ end
data/lib/bok_choy.rb ADDED
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+ require "erb"
3
+ require_relative "bok_choy/error"
4
+ require_relative "bok_choy/version"
5
+ require_relative "bok_choy/request"
6
+ require "bok_choy/helpers/configuration"
7
+
8
+ # The BokChoy module provides a Ruby interface to the BHL Names API
9
+ module BokChoy
10
+ extend Configuration
11
+
12
+ define_setting :base_url, "https://bhlnames.globalnames.org/api/v1/"
13
+ define_setting :mailto, ENV["BOK_CHOY_API_EMAIL"]
14
+
15
+ # Finds BHL references for a name
16
+ # @param name [String] A canonical scientific name (e.g., Pardosa moesta)
17
+ # @param author [String, nil] An author string (e.g., Banks)
18
+ # @param year [String, nil] A year string (e.g., 1892)
19
+ # @param reference [String, nil] A reference string (e.g., Docums Mycol. 34(nos 135-136))
20
+ # @param nomen_event [Boolean, nil] If true, tries to find nomenclatural event reference (default: true)
21
+ # @param json [Hash, nil] An optional JSON hash of name and reference data (overrides all other parameters)
22
+ # @param verbose [Boolean] Print headers to STDOUT
23
+ #
24
+ # @return [Hash] A result hash
25
+ def self.name_refs(name: nil, author: nil, year: nil, reference: nil, nomen_event: true, json: nil, verbose: false)
26
+ raise "Name or json required" if name.nil? && json.nil?
27
+
28
+ if json.nil?
29
+ json = {'name': {}, 'reference': {}, 'params': {}}
30
+ json[:name][:nameString] = name
31
+ json[:name][:author] = author unless author.nil?
32
+ json[:name][:year] = year unless year.nil?
33
+ json[:reference][:refString] = reference unless reference.nil?
34
+ json[:params][:nomenEvent] = nomen_event
35
+ end
36
+ endpoint = "name_refs"
37
+ Request.new(endpoint: endpoint, json: json, verbose: verbose).perform
38
+ end
39
+
40
+ # Finds a nomenclatural event in BHL by an external ID from a data source
41
+ #
42
+ # @param id [String] An external ID from a data source
43
+ # @param verbose [Boolean] Print headers to STDOUT
44
+ #
45
+ # @return [Hash] A result hash
46
+ def self.cached_refs(id, verbose: false)
47
+ raise "ID required" if id.nil?
48
+
49
+ endpoint = "cached_refs/#{id}"
50
+ Request.new(endpoint: endpoint, verbose: verbose).perform
51
+ end
52
+
53
+ # Get metadata and taxonomic statistics for a BHL item
54
+ #
55
+ # @param id [String] A BHL item ID
56
+ # @param verbose [Boolean] Print headers to STDOUT
57
+ #
58
+ # @return [Hash] A result hash
59
+ def self.items(id, verbose: false)
60
+ raise "ID required" if id.nil?
61
+
62
+ endpoint = "items/#{id}"
63
+ Request.new(endpoint: endpoint, verbose: verbose).perform
64
+ end
65
+
66
+ # Get BHL reference metadata by page ID
67
+ #
68
+ # @param id [String] A page ID from BHL
69
+ # @param verbose [Boolean] Print headers to STDOUT
70
+ #
71
+ # @return [Hash, String, Boolean] A JSON-LD hash or CSV
72
+ def self.page(id, verbose: false)
73
+ raise "ID required" if id.nil?
74
+
75
+ endpoint = "references/#{id}"
76
+ Request.new(endpoint: endpoint, verbose: verbose).perform
77
+ end
78
+
79
+ # Finds BHL items in which a given higher taxon is prevalent
80
+ #
81
+ # @param taxon_name [String] A taxonomic name (e.g., Lepidoptera)
82
+ # @param verbose [Boolean] Print headers to STDOUT
83
+ #
84
+ # @return [Hash] A result hash
85
+ def self.taxon_items(taxon_name: nil, verbose: false)
86
+ raise "Taxon name required" if taxon_name.nil?
87
+
88
+ taxon_url = ERB::Util.url_encode(taxon_name)
89
+ endpoint = "taxon_items/#{taxon_url}"
90
+ Request.new(endpoint: endpoint, verbose: verbose).perform
91
+ end
92
+
93
+ # Gets BHL reference metadata by page ID
94
+ #
95
+ # @param id [String] A page ID from BHL
96
+ # @param verbose [Boolean] Print headers to STDOUT
97
+ #
98
+ # @return [Hash] A result hash
99
+ def self.references(id, verbose: false)
100
+ raise "ID required" if id.nil?
101
+
102
+ endpoint = "references/#{id}"
103
+ Request.new(endpoint: endpoint, verbose: verbose).perform
104
+ end
105
+
106
+ # Check the API status
107
+ #
108
+ # @param verbose [Boolean] Print headers to STDOUT
109
+ #
110
+ # @return [String] A pong string
111
+ def self.ping(verbose: false)
112
+ endpoint = "ping"
113
+ Request.new(
114
+ endpoint: endpoint,
115
+ verbose: verbose
116
+ ).perform
117
+ end
118
+
119
+ # Check the API version
120
+ #
121
+ # @param verbose [Boolean] Print headers to STDOUT
122
+ #
123
+ # @return [Hash] A version hash
124
+ def self.version(verbose: false)
125
+ endpoint = "version"
126
+ Request.new(
127
+ endpoint: endpoint,
128
+ verbose: verbose
129
+ ).perform
130
+ end
131
+
132
+ end
metadata ADDED
@@ -0,0 +1,215 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bok_choy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Geoff Ower, Matt Yoder
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.1.4
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.1.4
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '13.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 13.0.1
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '13.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 13.0.1
53
+ - !ruby/object:Gem::Dependency
54
+ name: test-unit
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '3.3'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 3.3.6
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.3'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 3.3.6
73
+ - !ruby/object:Gem::Dependency
74
+ name: vcr
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '6.0'
80
+ type: :development
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - "~>"
85
+ - !ruby/object:Gem::Version
86
+ version: '6.0'
87
+ - !ruby/object:Gem::Dependency
88
+ name: webmock
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '3.18'
94
+ type: :development
95
+ prerelease: false
96
+ version_requirements: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - "~>"
99
+ - !ruby/object:Gem::Version
100
+ version: '3.18'
101
+ - !ruby/object:Gem::Dependency
102
+ name: faraday
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: '2.2'
108
+ type: :runtime
109
+ prerelease: false
110
+ version_requirements: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - "~>"
113
+ - !ruby/object:Gem::Version
114
+ version: '2.2'
115
+ - !ruby/object:Gem::Dependency
116
+ name: faraday-follow_redirects
117
+ requirement: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0.1'
122
+ - - "<"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.4'
125
+ type: :runtime
126
+ prerelease: false
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0.1'
132
+ - - "<"
133
+ - !ruby/object:Gem::Version
134
+ version: '0.4'
135
+ - !ruby/object:Gem::Dependency
136
+ name: multi_json
137
+ requirement: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - "~>"
140
+ - !ruby/object:Gem::Version
141
+ version: '1.15'
142
+ type: :runtime
143
+ prerelease: false
144
+ version_requirements: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - "~>"
147
+ - !ruby/object:Gem::Version
148
+ version: '1.15'
149
+ - !ruby/object:Gem::Dependency
150
+ name: byebug
151
+ requirement: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ type: :development
157
+ prerelease: false
158
+ version_requirements: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ description: Bok Choy is a low-level wrapper around the Bionomia API.
164
+ email:
165
+ - gdower@illinois.edu
166
+ executables: []
167
+ extensions: []
168
+ extra_rdoc_files: []
169
+ files:
170
+ - ".github/workflows/main.yml"
171
+ - ".gitignore"
172
+ - CHANGELOG.md
173
+ - CODE_OF_CONDUCT.md
174
+ - Gemfile
175
+ - Gemfile.lock
176
+ - LICENSE.txt
177
+ - README.md
178
+ - Rakefile
179
+ - bin/console
180
+ - bin/setup
181
+ - bok_choy.gemspec
182
+ - lib/bok_choy.rb
183
+ - lib/bok_choy/error.rb
184
+ - lib/bok_choy/faraday.rb
185
+ - lib/bok_choy/helpers/configuration.rb
186
+ - lib/bok_choy/request.rb
187
+ - lib/bok_choy/utils.rb
188
+ - lib/bok_choy/version.rb
189
+ homepage: https://github.com/SpeciesFileGroup/bok_choy
190
+ licenses:
191
+ - NCSA
192
+ metadata:
193
+ homepage_uri: https://github.com/SpeciesFileGroup/bok_choy
194
+ source_code_uri: https://github.com/SpeciesFileGroup/bok_choy
195
+ changelog_uri: https://github.com/SpeciesFileGroup/bok_choy/releases/tag/v0.0.1
196
+ post_install_message:
197
+ rdoc_options: []
198
+ require_paths:
199
+ - lib
200
+ required_ruby_version: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - ">="
203
+ - !ruby/object:Gem::Version
204
+ version: 2.4.0
205
+ required_rubygems_version: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - ">="
208
+ - !ruby/object:Gem::Version
209
+ version: '0'
210
+ requirements: []
211
+ rubygems_version: 3.5.11
212
+ signing_key:
213
+ specification_version: 4
214
+ summary: BHL Names Client
215
+ test_files: []