diff --git a/.circleci/config.yml b/.circleci/config.yml index fc94afd..fdd96cc 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -16,7 +16,7 @@ executors: NEO4J_ACCEPT_LICENSE_AGREEMENT: "yes" NEO4J_AUTH: "none" NEO4J_dbms_allow__upgrade: "true" - NEO4J_dbms_jvm_additional: "-Xms256m -Xmx256m" + NEO4J_dbms_jvm_additional: "-Xms128m -Xmx512m" NEO4J_dbms_logs_query_enabled: "true" NEO4J_dbms_memory_heap_max__size: "256M" NEO4J_dbms_memory_pagecache_size: "256M" diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..aa0a706 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +--- +language: ruby +cache: bundler +rvm: + - 2.7.5 +before_install: gem install bundler -v 2.1.4 diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a2e69c..7c90e89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,11 @@ Changelog ========= -## 0.0.2 04/12/2022 +## v1.0.0 04/28/2022 +* Initial gem extraction from Doximity + +## v0.0.2 04/12/2022 * Update gemspec causing faiulre to push to ruby gems -## 0.0.1 04/07/2022 +## v0.0.1 04/07/2022 * Initial empty gem diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..ba39df8 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,74 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level of experience, +nationality, personal appearance, race, religion, or sexual identity and +orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or +advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers 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, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at ryan@stawarz.com. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at [https://contributor-covenant.org/version/1/4][version] + +[homepage]: https://contributor-covenant.org +[version]: https://contributor-covenant.org/version/1/4/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1cafc11..2b63c52 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,30 +2,30 @@ We welcome contributions to this repository. Feel free to submit issues for bugs you encounter and pull requests for code and documentation contributions. -In order to prevent licensing issues, we require all contributors to sign an individual contributor license agreement, which is reproduced below: +In order to prevent licensing issues, Doximity Inc. (“Doximity”, “we”, “us”) requires all contributors to agree to an Individual Contributor License Agreement (“CLA”), which is reproduced below. By submitting your contributions to us, you agree that you have read and are bound by the CLA. If you do not agree with the CLA, you may not submit contributions. -## Individual Contributor License Agreement +## Doximity Individual Contributor License Agreement -In order to clarify the intellectual property license granted with Contributions from any person or entity, Doximity Inc. ("Doximity") must have a Contributor License Agreement ("CLA") on file that has been signed by each Contributor, indicating agreement to the license terms below. This license is for your protection as a Contributor as well as the protection of Doximity; it does not change your rights to use your own Contributions for any other purpose. +This license is for your protection as a Contributor as well as the protection of Doximity; it does not change your rights to use your own Contributions for any other purpose. -You accept and agree to the following terms and conditions for Your present and future Contributions submitted to Doximity. Except for the license granted herein to Doximity and recipients of software distributed by Doximity, You reserve all right, title, and interest in and to Your Contributions. +You accept and agree to the following terms and conditions for Your present and future Contributions submitted to Doximity. Except for the license granted herein to Doximity and recipients of software distributed by Doximity, You reserve all right, titles, and interests in and to Your Contributions. ### Definitions -"You" (or "Your") shall mean the copyright owner or legal entity authorized by the copyright owner that is making this Agreement with Doximity. For legal entities, the entity making a Contribution and all other entities that control, are controlled by, or are under common control with that entity are considered to be a single Contributor. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. +"You" (or "Your" or the “Contributor”) shall mean the copyright owner or legal entity authorized by the copyright owner that is making this Agreement with Doximity. For legal entities, the entity making a Contribution and all other entities that control, are controlled by, or are under common control with that entity are considered to be a single Contributor. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. -1. "Contribution" shall mean any original work of authorship, including any modifications or additions to an existing work, that is intentionally submitted by You to Doximity for inclusion in, or documentation of, any of the products owned or managed by Doximity (the "Work"). For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to Doximity or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, Doximity for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by You as "Not a Contribution." +1. "Contribution" shall mean the code, documentation, or any original work of authorship, including any modifications or additions to an existing work, that is intentionally submitted by You to Doximity for inclusion in, or documentation of, any of the products owned or managed by Doximity (the "Work"). For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to Doximity or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, Doximity for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by You as "Not a Contribution." 2. Grant of Copyright License. Subject to the terms and conditions of this Agreement, You hereby grant to Doximity and to recipients of software distributed by Doximity a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute Your Contributions and such derivative works. -3. Grant of Patent License. Subject to the terms and conditions of this Agreement, You hereby grant to Doximity and to recipients of software distributed by Doximity a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by You that are necessarily infringed by Your Contribution(s) alone or by combination of Your Contribution(s) with the Work to which such Contribution(s) was submitted. If any entity institutes patent litigation against You or any other entity (including a cross-claim or counterclaim in a lawsuit) alleging that your Contribution, or the Work to which you have contributed, constitutes direct or contributory patent infringement, then any patent licenses granted to that entity under this Agreement for that Contribution or Work shall terminate as of the date such litigation is filed. +3. Grant of Patent License. Subject to the terms and conditions of this Agreement, You hereby grant to Doximity and to recipients of software distributed by Doximity a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by You that are necessarily infringed by Your Contribution(s) alone or by a combination of Your Contribution(s) with the Work to which such Contribution(s) was submitted. If any entity institutes patent litigation against You or any other entity (including a cross-claim or counterclaim in a lawsuit) alleging that your Contribution, or the Work to which you have contributed, constitutes a direct or contributory patent infringement, then any patent licenses granted to that entity under this Agreement for that Contribution or Work shall terminate as of the date such litigation is filed. -4. You represent that you are legally entitled to grant the above license. If your employer(s) has rights to intellectual property that you create that includes your Contributions, you represent that you have received permission to make Contributions on behalf of that employer, that your employer has waived such rights for your Contributions to Doximity, or that your employer has executed a separate Corporate CLA with Doximity. +4. You represent that You are legally entitled to grant the above license. If your employer(s) has rights to intellectual property that you create that includes your Contributions, you represent that you have received permission to make Contributions on behalf of that employer, that your employer has waived such rights for your Contributions to Doximity, or that your employer has executed a separate Corporate CLA with Doximity. 5. You represent that each of Your Contributions is Your original creation (see section 7 for submissions on behalf of others). You represent that Your Contribution submissions include complete details of any third-party license or other restriction (including, but not limited to, related patents and trademarks) of which you are personally aware and which are associated with any part of Your Contributions. -6. You are not expected to provide support for Your Contributions, except to the extent You desire to provide support. You may provide support for free, for a fee, or not at all. Unless required by applicable law or agreed to in writing, You provide Your Contributions on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON- INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. +6. You are not expected to provide support for Your Contributions, except to the extent You desire to provide support. You may provide support for free, for a fee, or not at all. Unless required by applicable law or agreed to in writing, You provide Your Contributions on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. -7. Should You wish to submit work that is not Your original creation, You may submit it to Doximity separately from any Contribution, identifying the complete details of its source and of any license or other restriction (including, but not limited to, related patents, trademarks, and license agreements) of which you are personally aware, and conspicuously marking the work as "Submitted on behalf of a third-party: [[]named here]". +7. Should You wish to submit work that is not Your original creation, You may submit it to Doximity separately from any Contribution, identifying the complete details of its source and of any license or other restriction (including, but not limited to, related patents, trademarks, and license agreements) of which you are personally aware, and conspicuously marking the work as "Submitted on behalf of a third-party: [named here]". 8. You agree to notify Doximity of any facts or circumstances of which you become aware that would make these representations inaccurate in any respect. diff --git a/Gemfile.lock b/Gemfile.lock index 882e4b9..ed3a5dc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - neo4j-http (0.0.2) + neo4j-http (1.0.0) activesupport (>= 5.2) faraday (< 2) faraday-retry @@ -10,7 +10,7 @@ PATH GEM remote: https://rubygems.org/ specs: - activesupport (7.0.2.3) + activesupport (7.0.2.4) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) minitest (>= 5.1) diff --git a/README.md b/README.md index a7cdb40..fa6282c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,11 @@ # Neo4j::Http -The `Neo4j::Http` gem provides `Neo4j::Http::Client` as a thin wrapper around the [Neo4j HTTP API](https://neo4j.com/docs/http-api/current/). +The `Neo4j::Http` gem provides `a thin wrapper around the [Neo4j HTTP API](https://neo4j.com/docs/http-api/current/) (not the legacy REST api which was removed in 4.0). It works with Neo4j 3.5 through the latest release (at the time of this writing is 4.4) + +## Why a new gem? +The available gems to interact with Neo4j are generally: out of date relying on legacy APIs removed in 4.x, require the use of JRuby, or out of date C bindings. + +The goal of this gem is to provide a dependency free Ruby implementation that provides a simple wrapper to the [Neo4j HTTP API](https://neo4j.com/docs/http-api/current/) to do most of what applications may need in order to leverage the power Neo4j provides. ## Installation @@ -18,13 +23,96 @@ Or install it yourself as: $ gem install neo4j-http +## Configuration + +The client is configured by default via a set of environment variables from [Neo4j::Http::Configuration](https://github.com/doximity/neo4j-http/blob/master/lib/neo4j/http/configuration.rb): + +* `NEO4J_URL` - The base URL to connect to Neo4j at +* `NEO4J_USER` - The user name to use when authenticating to neo4j +* `NEO4J_PASSWORD` - The password of the user to be used for authentication +* `NEO4J_DATABASE` - The database name to be used when connecting. By default this will be nil and the path used for connecting to Neo4j wil be `/db/data/transaction/commit` to make it compliant with v3.5 of neo4j +* `NEO4J_HTTP_USER_AGENT` - The user agent name provided in the request - defaults to `Ruby Neo4j Http Client` +* `NEO4J_REQUEST_TIMEOUT_IN_SECONDS` - The number of seconds for the http request to time out if provided, defaults to nil + +These configuration values can also be set during initalization like: + +```ruby +Neo4j::Http.configure do |config| + config.request_timeout_in_seconds = 42 +end +``` + +## Usage + +The core interface can be directly accessed on `Neo4::Http::Client` - + +### Execute arbitrary cypher commands +```ruby +Neo4j::Http::Client.execute_cypher('MATCH (n:User{id: $id}) return n LIMIT 25', id: 42) +``` + +### Upsert, find and delete nodes +```ruby +node = Neo4j::Http::Node.new(label: "User", uuid: SecureRandom.uuid) +Neo4j::Http::Client.upsert_node(node) +Neo4j::Http::Client.find_node_by(label: "User", uuid: node.uuid) +Neo4j::Http::Client.find_nodes_by(label: "User", name: "Testing") +Neo4j::Http::Client.delete_node(node) +``` + +### Create a new relationship, also creating the nodes if they do not exist +```ruby +user1 = Neo4j::Http::Node.new(label: "User", uuid: SecureRandom.uuid) +user2 = Neo4j::Http::Node.new(label: "User", uuid: SecureRandom.uuid) +relationship = Neo4j::Http::Relationship.new(label: "KNOWS") +Neo4j::Http::Client.upsert_relationship(relationship: relationship, from: user1, to: user2, create_nodes: true) +``` + +### Find an existing relationship +```ruby +Neo4j::Http::Client.find_relationship(relationship: relationship, from: user1, to: user2) +``` + +### Delete the relationship if it exists +```ruby +Neo4j::Http::Client.delete_relationship(relationship: relationship, from: user1, to: user2) +``` + +Each of the methods exposed on `Neo4j::Http::Client` above are provided by instances of each of the following adapters: +* `Neo4j::Http::CypherClient` - provides an `execute_cypher` method which sends raw cypher commands to neo4j +* `Neo4j::Http::NodeClient` - provides a higher level API for upserting and deleting Nodes +* `Neo4j::Http::RelationshipClient` - provides a higher level API for upserting and deleting Relationships + +The Node and Relationship clients use the `CypherClient` under the hood. Each of these provides simple access via a `default` class method, which uses the default `Neo4j::Http.config` for creating the connections. For example + +`Neo4j::Http::NodeClient.default` + +is equivalent to: + +``` +config = Neo4j::Http.config +cypher_client = Neo4j::Http::CypherClient.new(config) +node_client = Neo4j::Http::NodeClient.new(cypher_client) +``` + +to connect to a different Neo4j database, you can create a custom configuration like: +``` +config = Neo4j::Http::Configuration.new({ database_name: 'test' }) +cypher_client = Neo4j::Http::CypherClient.new(config) +node_client = Neo4j::Http::NodeClient.new(cypher_client) +``` + ## Versioning This project follows [semantic versioning](https://semver.org). ## Development -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. +After checking out the repo, run `bin/setup` to install dependencies. + +To run specs, you'll need a running neo4j instance available at `localhost:7474`. If you have Docker installed, this is easily done by using the provided [docker-file](https://github.com/doximity/neo4j-http/blob/master/docker-compose.yml) - simply run `docker-compose up` within the project directory, and once running, you can then, run `rake spec` to run the tests in another terminal. + +You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`. @@ -33,7 +121,7 @@ To release a new version, update the version number in `version.rb`, issue a pul ## Contributing 1. Fork it -2. Create your feature branch (`git checkout -b my-new-feature`) +2. Create your feature branch (`git switch -c my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create a new Pull Request diff --git a/lib/neo4j/http.rb b/lib/neo4j/http.rb index e73da29..0c41027 100644 --- a/lib/neo4j/http.rb +++ b/lib/neo4j/http.rb @@ -1,6 +1,34 @@ require "neo4j/http/version" +require "active_support" +require "active_support/core_ext/module/delegation" +require "active_support/core_ext/object/blank" +require "active_support/core_ext/object/json" +require "active_support/core_ext/hash/indifferent_access" + +require "neo4j/http/auth_token" +require "neo4j/http/client" +require "neo4j/http/configuration" +require "neo4j/http/cypher_client" +require "neo4j/http/object_wrapper" +require "neo4j/http/node" +require "neo4j/http/node_client" +require "neo4j/http/relationship" +require "neo4j/http/relationship_client" +require "neo4j/http/results" + +require "neo4j/http/errors" + module Neo4j module Http + extend self + + def config + @congiguration ||= Configuration.new + end + + def configure + yield config + end end end diff --git a/lib/neo4j/http/auth_token.rb b/lib/neo4j/http/auth_token.rb new file mode 100644 index 0000000..27cac2a --- /dev/null +++ b/lib/neo4j/http/auth_token.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module Neo4j + module Http + class AuthToken + def self.token(username, password) + new(username, password).token + end + + def initialize(username, password) + @username = username + @password = password + end + + # See: https://neo4j.com/docs/developer-manual/current/http-api/authentication/#http-api-authenticate-to-access-the-server + def token + return "" if @username.blank? || @password.blank? + + Base64.encode64("#{@username}:#{@password}") + end + end + end +end diff --git a/lib/neo4j/http/client.rb b/lib/neo4j/http/client.rb new file mode 100644 index 0000000..100af44 --- /dev/null +++ b/lib/neo4j/http/client.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module Neo4j + module Http + class Client + CYPHER_CLIENT_METHODS = %i[execute_cypher].freeze + NODE_CLIENT_METHODS = %i[delete_node find_node_by find_nodes_by upsert_node].freeze + RELATIONSHIP_CLIENT_METHODS = %i[delete_relationship upsert_relationship].freeze + CLIENT_METHODS = (CYPHER_CLIENT_METHODS + NODE_CLIENT_METHODS + RELATIONSHIP_CLIENT_METHODS).freeze + + class << self + delegate(*CLIENT_METHODS, to: :default) + + def default + cypher_client = Http::CypherClient.new(Neo4j::Http.config) + node_client = Http::NodeClient.new(cypher_client) + relationship_client = Http::RelationshipClient.new(cypher_client) + @default ||= new(cypher_client, node_client, relationship_client) + end + end + + attr_accessor :cypher_client, :node_client, :relationship_client + + def initialize(cypher_client, node_client, relationship_client) + @cypher_client = cypher_client + @node_client = node_client + @relationship_client = relationship_client + end + + delegate(*CYPHER_CLIENT_METHODS, to: :cypher_client) + delegate(*NODE_CLIENT_METHODS, to: :node_client) + delegate(*RELATIONSHIP_CLIENT_METHODS, to: :relationship_client) + end + end +end diff --git a/lib/neo4j/http/configuration.rb b/lib/neo4j/http/configuration.rb new file mode 100644 index 0000000..8e22a0e --- /dev/null +++ b/lib/neo4j/http/configuration.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module Neo4j + module Http + class Configuration + attr_accessor :database_name + attr_accessor :password + attr_accessor :request_timeout_in_seconds + attr_accessor :uri + attr_accessor :user + attr_accessor :user_agent + + def initialize(options = ENV) + @uri = options.fetch("NEO4J_URL", "http://localhost:7474") + @user = options.fetch("NEO4J_USER", "") + @password = options.fetch("NEO4J_PASSWORD", "") + @database_name = options.fetch("NEO4J_DATABASE", nil) + @user_agent = options.fetch("NEO4J_HTTP_USER_AGENT", "Ruby Neo4j Http Client") + @request_timeout_in_seconds = options.fetch("NEO4J_REQUEST_TIMEOUT_IN_SECONDS", nil) + end + + def transaction_path + # v3.5 - /db/data/transaction/commit + # v4.x - /db/#{database_name}/tx/commit + if database_name + "/db/#{database_name}/tx/commit" + else + "/db/data/transaction/commit" + end + end + + def auth_token + Neo4j::Http::AuthToken.token(user, password) + end + end + end +end diff --git a/lib/neo4j/http/cypher_client.rb b/lib/neo4j/http/cypher_client.rb new file mode 100644 index 0000000..ac37f69 --- /dev/null +++ b/lib/neo4j/http/cypher_client.rb @@ -0,0 +1,95 @@ +# frozen_string_literal: true + +require "forwardable" +require "faraday" +require "faraday/retry" +require "faraday_middleware" + +module Neo4j + module Http + class CypherClient + def self.default + @default ||= new(Neo4j::Http.config) + end + + def initialize(configuration) + @configuration = configuration + end + + def execute_cypher(cypher, parameters = {}) + request_body = { + statements: [ + {statement: cypher, + parameters: parameters.as_json} + ] + } + + response = connection.post(transaction_path, request_body) + results = check_errors!(cypher, response, parameters) + + Neo4j::Http::Results.parse(results&.first || {}) + end + + def connection + build_connection + end + + protected + + delegate :auth_token, :transaction_path, to: :@configuration + def check_errors!(cypher, response, parameters) + raise Neo4j::Http::Errors::InvalidConnectionUrl, response.status if response.status == 404 + body = response.body || {} + errors = body.fetch("errors", []) + return body.fetch("results", {}) unless errors.present? + + error = errors.first + raise_error(error, cypher, parameters) + end + + def raise_error(error, cypher, parameters = {}) + code = error["code"] + message = error["message"] + klass = find_error_class(code) + parameters = JSON.pretty_generate(parameters.as_json) + raise klass, "#{code} - #{message}\n cypher: #{cypher} \n parameters given: \n#{parameters}" + end + + def find_error_class(code) + Neo4j::Http::Errors.const_get(code.gsub(".", "::")) + rescue + Neo4j::Http::Errors::Neo4jCodedError + end + + def build_connection + Faraday.new(url: @configuration.uri, headers: build_http_headers, request: build_request_options) do |f| + f.request :json # encode req bodies as JSON + f.request :retry # retry transient failures + f.response :json # decode response bodies as JSON + end + end + + def build_request_options + request_options = {} + + timeout = @configuration.request_timeout_in_seconds.to_i + request_options[:timeout] = timeout if timeout.positive? + + request_options + end + + def build_http_headers + { + "User-Agent" => @configuration.user_agent, + "Accept" => "application/json" + }.merge(authentication_headers) + end + + def authentication_headers + return {} if auth_token.blank? + + {"Authentication" => "Basic #{auth_token}"} + end + end + end +end diff --git a/lib/neo4j/http/errors.rb b/lib/neo4j/http/errors.rb new file mode 100644 index 0000000..1ce4d77 --- /dev/null +++ b/lib/neo4j/http/errors.rb @@ -0,0 +1,22 @@ +module Neo4j + module Http + module Errors + Neo4jError = Class.new(StandardError) + InvalidConnectionUrl = Class.new(Neo4jError) + Neo4jCodedError = Class.new(Neo4jError) + + # These are specific Errors Neo4j can raise + module Neo + module ClientError + module Statement + SyntaxError = Class.new(Neo4jCodedError) + end + + module Schema + ConstraintValidationFailed = Class.new(Neo4jCodedError) + end + end + end + end + end +end diff --git a/lib/neo4j/http/node.rb b/lib/neo4j/http/node.rb new file mode 100644 index 0000000..2b6288f --- /dev/null +++ b/lib/neo4j/http/node.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module Neo4j + module Http + class Node < ObjectWrapper + DEFAULT_PRIMARY_KEY_NAME = "uuid" + def initialize(label:, graph_node_primary_key_name: DEFAULT_PRIMARY_KEY_NAME, **attributes) + super + end + end + end +end diff --git a/lib/neo4j/http/node_client.rb b/lib/neo4j/http/node_client.rb new file mode 100644 index 0000000..e0d75b4 --- /dev/null +++ b/lib/neo4j/http/node_client.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +module Neo4j + module Http + class NodeClient + def self.default + @default ||= new(CypherClient.default) + end + + def initialize(cypher_client) + @cypher_client = cypher_client + end + + def upsert_node(node) + raise "#{node.key_name} value cannot be blank - (node keys: #{node.to_h.keys})" if node.key_value.blank? + + cypher = <<-CYPHER + MERGE (node:#{node.label} {#{node.key_name}: $key_value}) + ON CREATE SET node += $attributes + ON MATCH SET node += $attributes + return node + CYPHER + + results = @cypher_client.execute_cypher(cypher, key_value: node.key_value, attributes: node.attributes) + + results.first&.fetch("node") + end + + def delete_node(node) + cypher = <<-CYPHER + MATCH (node:#{node.label} {#{node.key_name}: $key_value}) + WITH node + DETACH DELETE node + RETURN node + CYPHER + + results = @cypher_client.execute_cypher(cypher, key_value: node.key_value) + results.first&.fetch("node") + end + + def find_node_by(label:, **attributes) + selectors = attributes.map { |key, value| "#{key}: $attributes.#{key}" }.join(", ") + cypher = "MATCH (node:#{label} { #{selectors} }) RETURN node LIMIT 1" + results = @cypher_client.execute_cypher(cypher, attributes: attributes) + return if results.empty? + results.first&.fetch("node") + end + + def find_nodes_by(label:, attributes:, limit: 100) + selectors = build_selectors(attributes) + cypher = "MATCH (node:#{label}) where #{selectors} RETURN node LIMIT #{limit}" + results = @cypher_client.execute_cypher(cypher, attributes: attributes) + results.map { |result| result["node"] } + end + + protected + + def build_selectors(attributes, node_name: :node) + attributes.map do |key, value| + if value.is_a?(Array) + "#{node_name}.#{key} IN $attributes.#{key}" + else + "#{node_name}.#{key} = $attributes.#{key}" + end + end.join(" AND ") + end + end + end +end diff --git a/lib/neo4j/http/object_wrapper.rb b/lib/neo4j/http/object_wrapper.rb new file mode 100644 index 0000000..7dcb23f --- /dev/null +++ b/lib/neo4j/http/object_wrapper.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module Neo4j + module Http + class ObjectWrapper + attr_reader :attributes, + :key_name, + :key_value, + :label, + :original_attributes + + def initialize(label:, graph_node_primary_key_name: nil, **attributes) + @original_attributes = (attributes || {}).with_indifferent_access + @attributes = original_attributes.dup.with_indifferent_access + @key_name = graph_node_primary_key_name + @key_value = @attributes.delete(key_name) + @label = label + end + + delegate :[], :to_h, :as_json, :to_json, to: :@original_attributes + + def method_missing(method_name, *args, &block) + if @original_attributes.has_key?(method_name) + original_attributes[method_name] + else + super + end + end + + def respond_to_missing?(method_name, include_private = false) + @original_attributes.has_key?(method_name) || super + end + end + end +end diff --git a/lib/neo4j/http/relationship.rb b/lib/neo4j/http/relationship.rb new file mode 100644 index 0000000..7420667 --- /dev/null +++ b/lib/neo4j/http/relationship.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Neo4j + module Http + class Relationship < ObjectWrapper + end + end +end diff --git a/lib/neo4j/http/relationship_client.rb b/lib/neo4j/http/relationship_client.rb new file mode 100644 index 0000000..9d52bb4 --- /dev/null +++ b/lib/neo4j/http/relationship_client.rb @@ -0,0 +1,81 @@ +# frozen_string_literal: true + +module Neo4j + module Http + class RelationshipClient + def self.default + @default ||= new(CypherClient.default) + end + + def initialize(cypher_client) + @cypher_client = cypher_client + end + + def upsert_relationship(relationship:, from:, to:, create_nodes: false) + match_or_merge = create_nodes ? "MERGE" : "MATCH" + from_selector = build_match_selector(:from, from) + to_selector = build_match_selector(:to, to) + relationship_selector = build_match_selector(:relationship, relationship) + + on_match = "" + if relationship.attributes.present? + on_match = <<-CYPHER + ON CREATE SET relationship += $relationship_attributes + ON MATCH SET relationship += $relationship_attributes + CYPHER + end + + cypher = +<<-CYPHER + #{match_or_merge} (#{from_selector}) + #{match_or_merge} (#{to_selector}) + MERGE (from) - [#{relationship_selector}] - (to) + #{on_match} + RETURN from, to, relationship + CYPHER + + results = @cypher_client.execute_cypher( + cypher, + from: from, + to: to, + relationship: relationship, + relationship_attributes: relationship.attributes + ) + results&.first + end + + def find_relationship(from:, relationship:, to:) + from_match_clause = build_match_selector(:from, from) + to_match_clause = build_match_selector(:to, to) + relationship_clause = build_match_selector(:relationship, relationship) + cypher = <<-CYPHER + MATCH (#{from_match_clause}) - [#{relationship_clause}] - (#{to_match_clause}) + RETURN from, to, relationship + CYPHER + + results = @cypher_client.execute_cypher(cypher, from: from, to: to, relationship: relationship) + results&.first + end + + def build_match_selector(name, data) + selector = +"#{name}:#{data.label}" + selector << " {#{data.key_name}: $#{name}.#{data.key_name}}" if data.key_name.present? + selector + end + + def delete_relationship(relationship:, from:, to:) + from_selector = build_match_selector(:from, from) + to_selector = build_match_selector(:to, to) + relationship_selector = build_match_selector(:relationship, relationship) + cypher = <<-CYPHER + MATCH (#{from_selector}) - [#{relationship_selector}] - (#{to_selector}) + WITH from, to, relationship + DELETE relationship + RETURN from, to + CYPHER + + results = @cypher_client.execute_cypher(cypher, from: from, to: to) + results&.first + end + end + end +end diff --git a/lib/neo4j/http/results.rb b/lib/neo4j/http/results.rb new file mode 100644 index 0000000..ef72777 --- /dev/null +++ b/lib/neo4j/http/results.rb @@ -0,0 +1,27 @@ +require "forwardable" + +module Neo4j + module Http + class Results + # Example result set: + # [{"columns"=>["n"], "data"=>[{"row"=>[{"name"=>"Foo", "uuid"=>"8c7dcfda-d848-4937-a91a-2e6debad2dd6"}], "meta"=>[{"id"=>242, "type"=>"node", "deleted"=>false}]}]}] + # + def self.parse(results) + columns = results["columns"] + data = results["data"] + + data.map do |result| + row = result["row"] || [] + meta = result["meta"] || [] + compacted_data = row.each_with_index.map do |attributes, index| + row_meta = meta[index] || {} + attributes["_neo4j_meta_data"] = row_meta if attributes.is_a?(Hash) + attributes + end + + columns.zip(compacted_data).to_h.with_indifferent_access + end + end + end + end +end diff --git a/lib/neo4j/http/version.rb b/lib/neo4j/http/version.rb index 0e3a281..fd6fe4e 100644 --- a/lib/neo4j/http/version.rb +++ b/lib/neo4j/http/version.rb @@ -1,5 +1,5 @@ module Neo4j module Http - VERSION = "0.0.2" + VERSION = "1.0.0" end end diff --git a/spec/neo4j/http/client_spec.rb b/spec/neo4j/http/client_spec.rb new file mode 100644 index 0000000..5201cd5 --- /dev/null +++ b/spec/neo4j/http/client_spec.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Neo4j::Http::Client, type: :uses_neo4j do + let(:cypher_client) { double(Neo4j::Http::CypherClient) } + let(:node_client) { double(Neo4j::Http::NodeClient) } + let(:relationship_client) { double(Neo4j::Http::RelationshipClient) } + subject(:client) { described_class.new(cypher_client, node_client, relationship_client) } + + describe "class methods" do + it "has the expected set of methods" do + expected_methods = described_class::CLIENT_METHODS + [:default] + expect(described_class.methods(false)).to match_array(expected_methods) + end + + described_class::CLIENT_METHODS.each do |method| + it "delegates the #{method} to the default instance" do + client_double = double(described_class) + allow(described_class).to receive(:default).and_return(client_double) + allow(client_double).to receive(method).and_return("received") + expect(described_class.public_send(method)).to eq("received") + end + end + end + + describe "cypher methods" do + it "delegates to the cypher client" do + allow(cypher_client).to receive(:execute_cypher).with(:args) + client.execute_cypher(:args) + expect(cypher_client).to have_received(:execute_cypher).with(:args) + end + end + + describe "node methods" do + described_class::NODE_CLIENT_METHODS.each do |method| + it "delegates #{method} to the node client" do + allow(node_client).to receive(method).with(:args) + client.public_send(method, :args) + expect(node_client).to have_received(method).with(:args) + end + end + end + + describe "relationship methods" do + described_class::RELATIONSHIP_CLIENT_METHODS.each do |method| + it "delegates #{method} to the relationship client" do + allow(relationship_client).to receive(method).with(:args) + client.public_send(method, :args) + expect(relationship_client).to have_received(method).with(:args) + end + end + end +end diff --git a/spec/neo4j/http/cypher_client_spec.rb b/spec/neo4j/http/cypher_client_spec.rb new file mode 100644 index 0000000..ac22bc6 --- /dev/null +++ b/spec/neo4j/http/cypher_client_spec.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Neo4j::Http::CypherClient, type: :uses_neo4j do + subject(:client) { described_class.default } + + describe "connection" do + it "uses the request timeout option when provided" do + config = Neo4j::Http::Configuration.new + config.request_timeout_in_seconds = 42 + client = described_class.new(config) + expect(client.connection.options.timeout).to eq(42) + end + + it "defaults to having no request timeout" do + expect(client.connection.options.timeout).to be_nil + end + end + + describe "execute_cypher" do + it "executes arbitrary cypher commands" do + results = client.execute_cypher("MERGE (node:Test {uuid: 'Uuid1', name: 'Foo'}) return node") + expect(results.length).to eq(1) + node = results.first["node"] + expect(node["uuid"]).to eq("Uuid1") + + results = client.execute_cypher("MERGE (node:Test {uuid: 'Uuid2', name: 'Bar'}) return node") + expect(results.length).to eq(1) + node = results.first["node"] + expect(node["uuid"]).to eq("Uuid2") + + results = client.execute_cypher("MATCH (node:Test {uuid: 'Uuid1'}) return node") + expect(results.length).to eq(1) + node = results.first["node"] + expect(node["uuid"]).to eq("Uuid1") + + results = client.execute_cypher("MATCH (node:Test) return node") + expect(results.length).to eq(2) + uuids = results.map { |result| result["node"]["uuid"] } + expect(uuids).to match_array(%w[Uuid1 Uuid2]) + names = results.map { |result| result["node"]["name"] } + expect(names).to match_array(%w[Foo Bar]) + + results = client.execute_cypher("MATCH (node:Test {uuid: 'Uuid1'}) DETACH DELETE node return node") + expect(results.length).to eq(1) + expect(results.first["node"].keys).to eq(["_neo4j_meta_data"]) + + results = client.execute_cypher("MATCH (node:Test {uuid: 'Uuid1'}) return node") + expect(results.length).to eq(0) + end + end +end diff --git a/spec/neo4j/http/node_client_spec.rb b/spec/neo4j/http/node_client_spec.rb new file mode 100644 index 0000000..5fbacf2 --- /dev/null +++ b/spec/neo4j/http/node_client_spec.rb @@ -0,0 +1,88 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Neo4j::Http::NodeClient, type: :uses_neo4j do + subject(:client) { described_class.default } + let(:cypher_client) { Neo4j::Http::CypherClient.default } + + describe "upsert_node" do + it "creates a node" do + uuid = "MyUuid" + node_in = Neo4j::Http::Node.new(label: "Test", uuid: uuid, name: "Foo") + node = client.upsert_node(node_in) + expect(node["uuid"]).to eq(uuid) + expect(node["name"]).to eq("Foo") + expect(node["_neo4j_meta_data"]).not_to be_nil + expect(node["_neo4j_meta_data"]["id"]).not_to be_nil + + results = cypher_client.execute_cypher("MATCH (node:Test {uuid: $uuid}) RETURN node", uuid: uuid) + expect(results.length).to eq(1) + node = results&.first&.fetch("node", nil) + expect(node).not_to be_nil + expect(node["name"]).to eq("Foo") + end + + it "updates the existing node" do + uuid = "MyUuid" + node_in = Neo4j::Http::Node.new(label: "Test", uuid: uuid, name: "Foo") + node1 = client.upsert_node(node_in) + expect(node1["uuid"]).to eq(uuid) + expect(node1["name"]).to eq("Foo") + expect(node1["_neo4j_meta_data"]).not_to be_nil + expect(node1["_neo4j_meta_data"]["id"]).not_to be_nil + + node_in = Neo4j::Http::Node.new(label: "Test", uuid: uuid, name: "Bar") + node2 = client.upsert_node(node_in) + expect(node2["uuid"]).to eq(uuid) + expect(node2["name"]).to eq("Bar") + expect(node2["_db_id"]).to eq(node1["_db_id"]) + expect(node2["_neo4j_meta_data"]).not_to be_nil + expect(node2["_neo4j_meta_data"]["id"]).to eq(node1["_neo4j_meta_data"]["id"]) + + results = cypher_client.execute_cypher("MATCH (node:Test {uuid: $uuid}) RETURN node", uuid: uuid) + expect(results.length).to eq(1) + end + end + + describe "find_node_by" do + it "finds a node by the attributes given" do + create_node(uuid: "Uuid2", name: "Bar") + create_node(uuid: "Uuid1", name: "Foo") + + node = client.find_node_by(label: "Test", uuid: "Uuid1") + + expect(node).not_to be_nil + expect(node["name"]).to eq("Foo") + end + + it "returns nil when no node is found" do + node = client.find_node_by(label: "Test", uuid: "MyUuid") + expect(node).to be_nil + end + end + + describe "find_nodes_by" do + it "finds nodes by the attributes given" do + create_node(uuid: "Uuid1", name: "Foo") + create_node(uuid: "Uuid2", name: "Bar") + create_node(uuid: "Uuid3", name: "Baz") + + nodes = client.find_nodes_by(label: "Test", attributes: {uuid: ["Uuid1", "Uuid3"]}) + + expect(nodes.length).to eq(2) + names = nodes.map { |node| node["name"] } + expect(names).to match_array(["Foo", "Baz"]) + end + + it "returns nil when no node is found" do + node = client.find_node_by(label: "Test", attributes: {uuid: "MyUuid"}) + expect(node).to be_nil + end + end + + def create_node(attributes = {}) + node = Neo4j::Http::Node.new(label: "Test", **attributes) + client.upsert_node(node) + end +end diff --git a/spec/neo4j/http/relationship_client_spec.rb b/spec/neo4j/http/relationship_client_spec.rb new file mode 100644 index 0000000..dbc2a64 --- /dev/null +++ b/spec/neo4j/http/relationship_client_spec.rb @@ -0,0 +1,114 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Neo4j::Http::RelationshipClient do + subject(:client) { described_class.default } + before(:each) { clean_neo4j_database } + + let(:from) { Neo4j::Http::Node.new(label: "Bot", uuid: "FromUuid", name: "Foo") } + let(:to) { Neo4j::Http::Node.new(label: "Bot", uuid: "ToUuid", name: "Bar") } + let(:relationship) { Neo4j::Http::Relationship.new(label: "KNOWS") } + + describe "upsert_relationship" do + it "creates a relationship between two nodes" do + create_node(from) + create_node(to) + result = client.upsert_relationship(relationship: relationship, from: from, to: to) + expect(result.keys).to eq(["from", "to", "relationship"]) + expect(result["from"]["uuid"]).to eq("FromUuid") + expect(result["to"]["uuid"]).to eq("ToUuid") + expect(result["relationship"]).to be_kind_of(Hash) + expect(result["relationship"].keys).to eq(%w[_neo4j_meta_data]) + end + + context "with create_nodes: true" do + it "creates a missing node when it doesn't exist" do + create_node(from) + result = client.upsert_relationship(relationship: relationship, from: from, to: to, create_nodes: true) + expect(result.keys).to eq(["from", "to", "relationship"]) + expect(result["from"]["uuid"]).to eq("FromUuid") + expect(result["to"]["uuid"]).to eq("ToUuid") + expect(result["relationship"]).to be_kind_of(Hash) + expect(result["relationship"].keys).to eq(%w[_neo4j_meta_data]) + + results = Neo4j::Http::CypherClient.default.execute_cypher("MATCH (node:Bot{uuid: 'ToUuid'}) return node") + node = results.first["node"] + expect(node["uuid"]).to eq("ToUuid") + end + end + + context "with create_nodes: false" do + it "fails when the nodes do not exist" do + create_node(from) + result = client.upsert_relationship(relationship: relationship, from: from, to: to, create_nodes: false) + + expect(result).to be_nil + end + end + + it "Sets extra attributes on the relationship when given" do + create_node(from) + create_node(to) + + relationship = Neo4j::Http::Relationship.new(label: "KNOWS", uuid: "RelationshipUuid", age: 21) + result = client.upsert_relationship(relationship: relationship, from: from, to: to) + verify_relationship(from, "KNOWS", to) + + expect(result.keys).to eq(["from", "to", "relationship"]) + expect(result["from"]["uuid"]).to eq("FromUuid") + expect(result["to"]["uuid"]).to eq("ToUuid") + expect(result["relationship"]).to be_kind_of(Hash) + expect(result["relationship"].keys).to eq(%w[uuid age _neo4j_meta_data]) + expect(result["relationship"]["uuid"]).to eq("RelationshipUuid") + expect(result["relationship"]["age"]).to eq(21) + end + + it "updates attributes on an existing relationship" do + end + end + + describe "find_relationship" do + it "finds an existing relationship" do + relationship = Neo4j::Http::Relationship.new(label: "KNOWS", value: 42.43) + result = client.upsert_relationship(relationship: relationship, from: from, to: to, create_nodes: true) + expect(result.keys).to eq(["from", "to", "relationship"]) + + result = client.find_relationship(relationship: relationship, from: from, to: to) + expect(result.keys).to eq(["from", "to", "relationship"]) + expect(result["from"]["uuid"]).to eq("FromUuid") + expect(result["to"]["uuid"]).to eq("ToUuid") + expect(result["relationship"]["value"]).to eq(42.43) + end + end + + describe "delete_relationship" do + it "Removes the relationship between the nodes" do + relationship = Neo4j::Http::Relationship.new(label: "KNOWS") + result = client.upsert_relationship(relationship: relationship, from: from, to: to, create_nodes: true) + expect(result.keys).to eq(["from", "to", "relationship"]) + + result = client.find_relationship(relationship: relationship, from: from, to: to) + expect(result.keys).to eq(["from", "to", "relationship"]) + + result = client.delete_relationship(relationship: relationship, from: from, to: to) + expect(result.keys).to eq(["from", "to"]) + + result = client.find_relationship(relationship: relationship, from: from, to: to) + expect(result).to be_nil + end + end + + def verify_relationship(from, relationship, to) + results = Neo4j::Http::CypherClient.default.execute_cypher("MATCH (from:Bot {uuid: $from})-[relationship:#{relationship}]-(to:Bot {uuid: $to}) RETURN from, to, relationship", from: from.key_value, to: to.key_value) + result = results.first + + expect(result.keys).to eq(%w[from to relationship]) + expect(result["from"]["uuid"]).to eq(from.uuid) + expect(result["to"]["uuid"]).to eq(to.uuid) + end + + def create_node(node) + Neo4j::Http::NodeClient.default.upsert_node(node) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7772a3c..ac718ad 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,7 @@ require "bundler/setup" require "neo4j/http" require "pry" +require "support/neo4j_spec_helpers" RSpec.configure do |config| # Enable flags like --only-failures and --next-failure @@ -12,4 +13,6 @@ config.expect_with :rspec do |c| c.syntax = :expect end + + include Neo4jSpecHelpers end diff --git a/spec/support/neo4j_spec_helpers.rb b/spec/support/neo4j_spec_helpers.rb new file mode 100644 index 0000000..79b386e --- /dev/null +++ b/spec/support/neo4j_spec_helpers.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module Neo4jSpecHelpers + extend ActiveSupport::Concern + + def clean_neo4j_database + cypher = "MATCH (n) DETACH DELETE n" + Neo4j::Http::CypherClient.default.execute_cypher(cypher) + end +end + +RSpec.configure do |c| + c.before(:example, type: :uses_neo4j) do + clean_neo4j_database + end +end diff --git a/vendor/cache/activesupport-7.0.2.3.gem b/vendor/cache/activesupport-7.0.2.3.gem deleted file mode 100644 index 2e69755..0000000 Binary files a/vendor/cache/activesupport-7.0.2.3.gem and /dev/null differ diff --git a/vendor/cache/activesupport-7.0.2.4.gem b/vendor/cache/activesupport-7.0.2.4.gem new file mode 100644 index 0000000..a3babb9 Binary files /dev/null and b/vendor/cache/activesupport-7.0.2.4.gem differ