class Neo4j::Core::CypherSession::Adaptors::HTTP::Requestor

Basic wrapper around HTTP requests to standard Neo4j HTTP endpoints

- Takes care of JSONifying objects passed as body (Hash/Array/Query)
- Sets headers, including user agent string

Constants

REQUEST_HEADERS

Public Class Methods

new(url, user_agent_string, instrument_proc, faraday_configurator) click to toggle source
    # File lib/neo4j/core/cypher_session/adaptors/http.rb
116 def initialize(url, user_agent_string, instrument_proc, faraday_configurator)
117   self.url = url
118   @user = user
119   @password = password
120   @user_agent_string = user_agent_string
121   @faraday = wrap_connection_failed! { faraday_connection(faraday_configurator) }
122   @instrument_proc = instrument_proc
123 end

Private Class Methods

statement_from_query(query) click to toggle source
    # File lib/neo4j/core/cypher_session/adaptors/http.rb
201 def statement_from_query(query)
202   {statement: query.cypher,
203    parameters: query.parameters || {},
204    resultDataContents: ROW_REST}
205 end

Public Instance Methods

get(path, body = '', options = {}) click to toggle source

Convenience method to request(:get, …)

    # File lib/neo4j/core/cypher_session/adaptors/http.rb
148 def get(path, body = '', options = {})
149   request(:get, path, body, options)
150 end
post(path, body = '', options = {}) click to toggle source

Convenience method to request(:post, …)

    # File lib/neo4j/core/cypher_session/adaptors/http.rb
143 def post(path, body = '', options = {})
144   request(:post, path, body, options)
145 end
request(method, path, body = '', _options = {}) click to toggle source

@method HTTP method (:get/:post/:delete/:put) @path Path part of URL @body Body for the request. If a Query or Array of Queries,

it is automatically converted
    # File lib/neo4j/core/cypher_session/adaptors/http.rb
132 def request(method, path, body = '', _options = {})
133   request_body = request_body(body)
134   url = url_from_path(path)
135   @instrument_proc.call(method, url, request_body) do
136     wrap_connection_failed! do
137       @faraday.run_request(method, url, request_body, REQUEST_HEADERS)
138     end
139   end
140 end

Private Instance Methods

faraday_connection(configurator) click to toggle source
    # File lib/neo4j/core/cypher_session/adaptors/http.rb
154 def faraday_connection(configurator)
155   require 'faraday'
156   require 'faraday_middleware/multi_json'
157 
158   Faraday.new(url) do |faraday|
159     faraday.request :multi_json
160 
161     faraday.response :multi_json, symbolize_keys: true, content_type: 'application/json'
162 
163     faraday.headers['Content-Type'] = 'application/json'
164     faraday.headers['User-Agent'] = @user_agent_string
165 
166     configurator.call(faraday)
167   end
168 end
password_config(options) click to toggle source
    # File lib/neo4j/core/cypher_session/adaptors/http.rb
170 def password_config(options)
171   options.fetch(:basic_auth, {}).fetch(:password, @password)
172 end
request_body(body) click to toggle source
    # File lib/neo4j/core/cypher_session/adaptors/http.rb
178 def request_body(body)
179   return body if body.is_a?(String)
180 
181   body_is_query_array = body.is_a?(Array) && body.all? { |o| o.respond_to?(:cypher) }
182   case body
183   when Hash, Array
184     return {statements: body.map(&self.class.method(:statement_from_query))} if body_is_query_array
185 
186     body
187   else
188     {statements: [self.class.statement_from_query(body)]} if body.respond_to?(:cypher)
189   end
190 end
url_base() click to toggle source
    # File lib/neo4j/core/cypher_session/adaptors/http.rb
208 def url_base
209   "#{scheme}://#{host}:#{port}"
210 end
url_from_path(path) click to toggle source
    # File lib/neo4j/core/cypher_session/adaptors/http.rb
212 def url_from_path(path)
213   url_base + (path[0] != '/' ? '/' + path : path)
214 end
username_config(options) click to toggle source
    # File lib/neo4j/core/cypher_session/adaptors/http.rb
174 def username_config(options)
175   options.fetch(:basic_auth, {}).fetch(:username, @user)
176 end
wrap_connection_failed!() { || ... } click to toggle source
    # File lib/neo4j/core/cypher_session/adaptors/http.rb
192 def wrap_connection_failed!
193   yield
194 rescue Faraday::ConnectionFailed => e
195   raise CypherSession::ConnectionFailedError, "#{e.class}: #{e.message}"
196 end