cayley 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +102 -0
- data/lib/cayley.rb +8 -0
- data/lib/cayley/graph.rb +55 -0
- data/lib/cayley/path.rb +66 -0
- data/lib/cayley/version.rb +1 -1
- metadata +63 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 793eed313948db158c0686987ee9309a9f9b94db
|
4
|
+
data.tar.gz: f3f73cf8f68ec5b912e0c4f44cc731ee3b740e2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d923514bf7c0923317505b37f72d2cc55f193b2da5bdea3e4d169a429d0636553d6002e206f3585b47f0d07519518d5de7e8b5408476827288695d1aaea271f0
|
7
|
+
data.tar.gz: a30a1142bb6ca416ba802f38ba01a5f11c8020e53236bce64ad61626f6d8773af1b0bdb71250aa50fdc065ad6739629d58cea6a01f3a8bfe5ed436f3ca755c00
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# cayley-ruby
|
2
|
+
|
3
|
+
A ruby library for working with
|
4
|
+
[Google's Cayley](https://github.com/google/cayley)
|
5
|
+
graph database.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
You can install it via gem
|
10
|
+
|
11
|
+
```bash
|
12
|
+
gem install cayley
|
13
|
+
```
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
Start your Cayley
|
18
|
+
|
19
|
+
```bash
|
20
|
+
# example using 30kmoviedata from cayley's repository
|
21
|
+
./cayley http --dbpath=30kmoviedata.nt
|
22
|
+
```
|
23
|
+
|
24
|
+
You can use methods from
|
25
|
+
[Gremlin API documentation](https://github.com/google/cayley/blob/master/docs/GremlinAPI.md), just translate method names to snake style equivalent.
|
26
|
+
|
27
|
+
As a first step you need to create your client
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'cayley'
|
31
|
+
|
32
|
+
graph = Cayley::Graph.new
|
33
|
+
|
34
|
+
# or
|
35
|
+
|
36
|
+
graph = Cayley::Graph.new(host: 'localhost', port: 64210)
|
37
|
+
```
|
38
|
+
|
39
|
+
Then using **30kmovies.nt** db from cayley's repository you can do this
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
graph.vertex.get_limit(5)
|
43
|
+
|
44
|
+
graph.vertex('Humphrey Bogart').lll
|
45
|
+
|
46
|
+
graph.v('Humphrey Bogart').all
|
47
|
+
|
48
|
+
graph.v('Humphrey Bogart').in('name').all
|
49
|
+
|
50
|
+
graph.v('Casablanca').in('name').all
|
51
|
+
|
52
|
+
graph.v().has('name', 'Casablanca').all
|
53
|
+
```
|
54
|
+
|
55
|
+
You can also use morphism
|
56
|
+
|
57
|
+
```
|
58
|
+
film_to_actor = graph.morphism
|
59
|
+
.out('/film/film/starring')
|
60
|
+
.out('/film/performance/actor')
|
61
|
+
graph.v
|
62
|
+
.has('name', 'Casablanca')
|
63
|
+
.follow(film_to_actor)
|
64
|
+
.out('name').all
|
65
|
+
```
|
66
|
+
|
67
|
+
For more info take a look at
|
68
|
+
[Cayley's repository](https://github.com/google/cayley)
|
69
|
+
|
70
|
+
## Advanced
|
71
|
+
|
72
|
+
By default result of your queries are wrapped in Hashie::Mash so you can
|
73
|
+
do things like these
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
graph.v.all.each do |result|
|
77
|
+
puts result.id # instead of result['id']
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
If you want to use plain ruby hashes you can disable wrapping by
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
graph = Cayley::Graph.new(result_wrapper: nil)
|
85
|
+
```
|
86
|
+
|
87
|
+
Or you can use some custom wrapper if you put class instead of nil.
|
88
|
+
|
89
|
+
## Debugging
|
90
|
+
|
91
|
+
If you are not sure why you are getting nil as a result of your query
|
92
|
+
and you are not able to find out the solution you can turn on debug mode
|
93
|
+
where used Gremlin query is printed out.
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
graph = Cayley::Graph.new(debug: true)
|
97
|
+
```
|
98
|
+
|
99
|
+
## TODO
|
100
|
+
|
101
|
+
* logger
|
102
|
+
* tests
|
data/lib/cayley.rb
CHANGED
data/lib/cayley/graph.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
module Cayley
|
2
|
+
|
3
|
+
class Graph
|
4
|
+
API_PATH = '/api/v1/query/gremlin'
|
5
|
+
|
6
|
+
|
7
|
+
def initialize params
|
8
|
+
@host = params[:host] || 'localhost'
|
9
|
+
@port = params[:port] || 64210
|
10
|
+
@debug = params[:debug] || false
|
11
|
+
@result_wrapper = params[:result_wrapper] || Hashie::Mash
|
12
|
+
end
|
13
|
+
|
14
|
+
def query
|
15
|
+
Query.new(self)
|
16
|
+
end
|
17
|
+
|
18
|
+
def vertex *args
|
19
|
+
Path.vertex(self, *args)
|
20
|
+
end
|
21
|
+
|
22
|
+
alias_method :v, :vertex
|
23
|
+
|
24
|
+
def morphism
|
25
|
+
Path.morphism(self)
|
26
|
+
end
|
27
|
+
|
28
|
+
alias_method :m, :morphism
|
29
|
+
|
30
|
+
def perform query
|
31
|
+
constructed = query.construct
|
32
|
+
puts "DEBUG - Query: #{constructed}" if @debug
|
33
|
+
results = request(constructed)
|
34
|
+
|
35
|
+
if results && @result_wrapper
|
36
|
+
results.map{ |r| @result_wrapper.new(r) }
|
37
|
+
else
|
38
|
+
results
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
protected
|
43
|
+
|
44
|
+
def request data
|
45
|
+
url = "http://#{@host}:#{@port}#{API_PATH}"
|
46
|
+
r = Curl::Easy.new(url) do |curl|
|
47
|
+
curl.headers["User-Agent"] = "curb-#{Curl::VERSION}"
|
48
|
+
#curl.verbose = true
|
49
|
+
end
|
50
|
+
r.http_post(data)
|
51
|
+
JSON.load(r.body_str)['result']
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
data/lib/cayley/path.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
module Cayley
|
2
|
+
|
3
|
+
class Path
|
4
|
+
attr_accessor :calls
|
5
|
+
|
6
|
+
METHODS = [
|
7
|
+
:out, :in, :both, :is, :has, # basic
|
8
|
+
:tag, :back, :save, # tags
|
9
|
+
:intersect, :union, # joining
|
10
|
+
]
|
11
|
+
|
12
|
+
def self.vertex graph, *args
|
13
|
+
path = Path.new(graph)
|
14
|
+
path.calls = [{ method: :vertex, args: args }]
|
15
|
+
path
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.morphism graph
|
19
|
+
Path.new(graph)
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize graph=nil
|
23
|
+
@graph = graph
|
24
|
+
@calls = []
|
25
|
+
end
|
26
|
+
|
27
|
+
def method_missing name, *args
|
28
|
+
return super unless METHODS.include?(name)
|
29
|
+
add(name, *args)
|
30
|
+
end
|
31
|
+
|
32
|
+
def add name, *args
|
33
|
+
@calls << { method: name, args: args }
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def follow path
|
38
|
+
path.calls.each { |c| @calls << c }
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
42
|
+
def + path
|
43
|
+
clone.follow(path)
|
44
|
+
end
|
45
|
+
|
46
|
+
def all
|
47
|
+
add(:all)
|
48
|
+
@graph.perform(self)
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_limit limit
|
52
|
+
add(:get_limit, limit)
|
53
|
+
@graph.perform(self)
|
54
|
+
end
|
55
|
+
|
56
|
+
def construct
|
57
|
+
calls = @calls.map do |call|
|
58
|
+
m = call[:method].to_s.camelize
|
59
|
+
as = (call[:args] || []).map{ |a| "\"#{a}\"" }.join(", ")
|
60
|
+
"#{m}(#{as})"
|
61
|
+
end
|
62
|
+
'graph.' + calls.join('.')
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/lib/cayley/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cayley
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rene Klacan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.8.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.8.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: curb
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.8.5
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.8.5
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: hashie
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.2.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.2.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 4.1.4
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 4.1.4
|
13
69
|
- !ruby/object:Gem::Dependency
|
14
70
|
name: rspec
|
15
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,14 +94,17 @@ dependencies:
|
|
38
94
|
- - ~>
|
39
95
|
- !ruby/object:Gem::Version
|
40
96
|
version: '3.0'
|
41
|
-
description: '
|
97
|
+
description: Ruby library for working with Google's Cayley graph database
|
42
98
|
email: rene@klacan.sk
|
43
99
|
executables: []
|
44
100
|
extensions: []
|
45
101
|
extra_rdoc_files: []
|
46
102
|
files:
|
47
103
|
- lib/cayley.rb
|
104
|
+
- lib/cayley/path.rb
|
48
105
|
- lib/cayley/version.rb
|
106
|
+
- lib/cayley/graph.rb
|
107
|
+
- README.md
|
49
108
|
homepage: https://github.com/reneklacan/cayley-ruby
|
50
109
|
licenses:
|
51
110
|
- Beerware
|
@@ -69,6 +128,6 @@ rubyforge_project:
|
|
69
128
|
rubygems_version: 2.0.14
|
70
129
|
signing_key:
|
71
130
|
specification_version: 4
|
72
|
-
summary: '
|
131
|
+
summary: Ruby library for working with Google's Cayley graph database
|
73
132
|
test_files: []
|
74
133
|
has_rdoc:
|