Class: Tina4::GraphUrl

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/graph.rb

Overview

A parsed graph connection URL — the DatabaseUrl sibling for graph engines.

engine is the CANONICAL name the factory selects an adapter by; a scheme alias resolves to its engine here. bolt/neo4j/memgraph all speak Bolt/Cypher and share ONE adapter ("bolt"); the engine label only tunes per-engine defaults.

Constant Summary collapse

SCHEME_ENGINE =
{
  "ultipa" => "ultipa",
  "ultipas" => "ultipa", # TLS variant
  "neo4j" => "bolt",
  "neo4j+s" => "bolt",
  "bolt" => "bolt",
  "bolt+s" => "bolt",
  "memgraph" => "bolt",
  "arango" => "arango",
  "arangodb" => "arango",
}.freeze
ENGINE_DEFAULT_PORT =

Default port per engine when the URL omits one.

{
  "ultipa" => 60_061,
  "bolt" => 7687,
  "arango" => 8529,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ GraphUrl

Returns a new instance of GraphUrl.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tina4/graph.rb', line 79

def initialize(url)
  @raw = url
  parsed = begin
    URI.parse(url.to_s)
  rescue URI::InvalidURIError
    raise ArgumentError, "GraphUrl: Invalid URL format — expected scheme://host:port/graph"
  end

  scheme = (parsed.scheme || "").downcase
  engine = SCHEME_ENGINE[scheme]
  if engine.nil?
    raise ArgumentError,
          "GraphUrl: Unsupported graph URL scheme '#{scheme}'. Supported: " \
          "#{SCHEME_ENGINE.keys.sort.join(', ')} (e.g. ultipa://host:60061/mygraph)."
  end

  @scheme = scheme
  @engine = engine
  @host = parsed.host && !parsed.host.empty? ? parsed.host : "localhost"
  @port = parsed.port || ENGINE_DEFAULT_PORT[engine]
  # The path is the graph/database name (one leading slash stripped).
  graph = (parsed.path || "").sub(%r{\A/}, "")
  @graph = graph.empty? ? nil : graph
  @username = parsed.user ? URI.decode_www_form_component(parsed.user) : nil
  @password = parsed.password ? URI.decode_www_form_component(parsed.password) : nil
  @params = URI.decode_www_form(parsed.query || "").to_h
  # TLS if the scheme says so (…s) or ?tls=1.
  @use_tls = scheme.end_with?("s") || %w[1 true].include?(@params["tls"])
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



76
77
78
# File 'lib/tina4/graph.rb', line 76

def engine
  @engine
end

#graphObject (readonly)

Returns the value of attribute graph.



76
77
78
# File 'lib/tina4/graph.rb', line 76

def graph
  @graph
end

#hostObject (readonly)

Returns the value of attribute host.



76
77
78
# File 'lib/tina4/graph.rb', line 76

def host
  @host
end

#paramsObject (readonly)

Returns the value of attribute params.



76
77
78
# File 'lib/tina4/graph.rb', line 76

def params
  @params
end

#passwordObject (readonly)

Returns the value of attribute password.



76
77
78
# File 'lib/tina4/graph.rb', line 76

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



76
77
78
# File 'lib/tina4/graph.rb', line 76

def port
  @port
end

#rawObject (readonly)

Returns the value of attribute raw.



76
77
78
# File 'lib/tina4/graph.rb', line 76

def raw
  @raw
end

#schemeObject (readonly)

Returns the value of attribute scheme.



76
77
78
# File 'lib/tina4/graph.rb', line 76

def scheme
  @scheme
end

#use_tlsObject (readonly)

Returns the value of attribute use_tls.



76
77
78
# File 'lib/tina4/graph.rb', line 76

def use_tls
  @use_tls
end

#usernameObject (readonly)

Returns the value of attribute username.



76
77
78
# File 'lib/tina4/graph.rb', line 76

def username
  @username
end

Class Method Details

.from_env(env_key = "TINA4_GRAPH_URL") ⇒ Object

Parse the configured URL, or nil when the variable is not set.



116
117
118
119
# File 'lib/tina4/graph.rb', line 116

def self.from_env(env_key = "TINA4_GRAPH_URL")
  url = (ENV[env_key] || "").strip
  url.empty? ? nil : new(url)
end

Instance Method Details

#dsnObject

host:port/graph — for messages, never carrying credentials.



110
111
112
113
# File 'lib/tina4/graph.rb', line 110

def dsn
  target = @port ? "#{@host}:#{@port}" : @host
  @graph ? "#{target}/#{@graph}" : target
end