fauna 1.3.2 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/CHANGELOG +2 -0
- data/Manifest +0 -1
- data/README.md +0 -2
- data/Rakefile +19 -12
- data/fauna.gemspec +7 -6
- data/lib/fauna.rb +1 -3
- data/lib/fauna/cache.rb +10 -12
- data/lib/fauna/client.rb +3 -3
- data/lib/fauna/connection.rb +28 -31
- data/lib/fauna/named_resource.rb +2 -2
- data/lib/fauna/rails.rb +20 -18
- data/lib/fauna/resource.rb +58 -30
- data/lib/fauna/set.rb +27 -16
- data/lib/fauna/util.rb +2 -2
- data/lib/tasks/fauna.rake +14 -14
- data/test/class_test.rb +6 -1
- data/test/client_test.rb +9 -8
- data/test/connection_test.rb +12 -12
- data/test/database_test.rb +5 -6
- data/test/query_test.rb +13 -9
- data/test/readme_test.rb +5 -5
- data/test/set_test.rb +2 -3
- data/test/test_helper.rb +47 -44
- metadata +17 -33
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -2
- data/fauna-ruby.pem +0 -21
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YzZmYmZjOTlmOWM0ZjEwYWY4ZjcwOWE3NzVjYjdlYTEwZTQ4Y2Q1OQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
Y2ExYTdiMzU3YjQ5OGQ1Zjg2OWYzMjI5OWE4Mzg5MWQwYzgzOTBlNA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NDI4MmY3ZTI2YzEyOGQ5MTM5ZDNmZmFiNTJlN2MxYmRjZDM5ZTllMzc1YTYz
|
10
|
+
M2ZkNDJlNjIyMjIxODBjMTgyYjEzYjg1NGFhMzBjN2U1ODNiMjhmZmRjOGQz
|
11
|
+
NWY5OGE0YjVhYTVkZGUzZDIxMGMxNDk3YzdlMzZiYjgwOTliMTY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZjE2NGM0Y2ExZjA3MjcxMDRlNDRlN2Y0OWU1OWM2YzE1MjAxYzZjOGJiYTky
|
14
|
+
ZjIwMWJkYzc4ZWJlZTcxMTk3MDk1Nzc1ZjEwNjU0MzBjMGJhNzFjM2EzOTgw
|
15
|
+
Yzk1Y2NmZTMzMmIwMjBkY2Y1YjhhNzUzYTc3NDk1ODVlNjQxZjA=
|
data/CHANGELOG
CHANGED
data/Manifest
CHANGED
data/README.md
CHANGED
@@ -36,8 +36,6 @@ All API requests start with an instance of `Fauna::Connection`.
|
|
36
36
|
Creating a connection requires either a token, a server key, or a
|
37
37
|
client key.
|
38
38
|
|
39
|
-
Let's use a server key we got from our [Fauna Cloud console](https://fauna.org/account/databases):
|
40
|
-
|
41
39
|
```ruby
|
42
40
|
server_key = 'ls8AkXLdakAAAALPAJFy3LvQAAGwDRAS_Prjy6O8VQBfQAlZzwAA'
|
43
41
|
```
|
data/Rakefile
CHANGED
@@ -1,23 +1,30 @@
|
|
1
1
|
require 'echoe'
|
2
2
|
|
3
|
-
Echoe.new(
|
4
|
-
p.author =
|
5
|
-
p.project =
|
6
|
-
p.summary =
|
3
|
+
Echoe.new('fauna') do |p|
|
4
|
+
p.author = 'Fauna, Inc.'
|
5
|
+
p.project = 'fauna'
|
6
|
+
p.summary = 'Ruby client for the Fauna distributed database.'
|
7
7
|
p.retain_gemspec = true
|
8
|
-
p.
|
9
|
-
p.
|
10
|
-
p.
|
11
|
-
p.dependencies = ["faraday ~>0.9.0", "json ~>1.8.0"]
|
12
|
-
p.development_dependencies = ["mocha", "echoe", "minitest ~>4.0"]
|
8
|
+
p.licenses = ['Mozilla Public License, Version 2.0 (MPL2)']
|
9
|
+
p.dependencies = ['faraday ~>0.9.0', 'json ~>1.8.0']
|
10
|
+
p.development_dependencies = ['mocha', 'echoe', 'minitest ~>4.0', 'rubocop']
|
13
11
|
end
|
14
12
|
|
15
13
|
task :beautify do
|
16
|
-
require
|
17
|
-
Dir[
|
14
|
+
require 'ruby-beautify'
|
15
|
+
Dir['**/*rb'].each do |filename|
|
18
16
|
s = RBeautify.beautify_string(:ruby, File.read(filename))
|
19
17
|
File.write(filename, s) unless s.empty?
|
20
|
-
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
begin
|
22
|
+
require 'rubocop/rake_task'
|
23
|
+
RuboCop::RakeTask.new
|
24
|
+
rescue LoadError
|
25
|
+
task :rubocop do
|
26
|
+
$stderr.puts 'Rubocop is disabled'
|
27
|
+
end
|
21
28
|
end
|
22
29
|
|
23
30
|
task :prerelease => [:manifest, :test, :install]
|
data/fauna.gemspec
CHANGED
@@ -1,25 +1,23 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
# stub: fauna 1.3.
|
2
|
+
# stub: fauna 1.3.3 ruby lib
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "fauna"
|
6
|
-
s.version = "1.3.
|
6
|
+
s.version = "1.3.3"
|
7
7
|
|
8
8
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
9
9
|
s.authors = ["Fauna, Inc."]
|
10
|
-
s.
|
11
|
-
s.date = "2014-05-15"
|
10
|
+
s.date = "2014-11-18"
|
12
11
|
s.description = "Ruby client for the Fauna distributed database."
|
13
12
|
s.email = ""
|
14
13
|
s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README.md", "lib/fauna.rb", "lib/fauna/cache.rb", "lib/fauna/client.rb", "lib/fauna/connection.rb", "lib/fauna/named_resource.rb", "lib/fauna/rails.rb", "lib/fauna/resource.rb", "lib/fauna/set.rb", "lib/fauna/util.rb", "lib/tasks/fauna.rake"]
|
15
|
-
s.files = ["CHANGELOG", "Gemfile", "LICENSE", "Manifest", "README.md", "Rakefile", "fauna
|
14
|
+
s.files = ["CHANGELOG", "Gemfile", "LICENSE", "Manifest", "README.md", "Rakefile", "fauna.gemspec", "lib/fauna.rb", "lib/fauna/cache.rb", "lib/fauna/client.rb", "lib/fauna/connection.rb", "lib/fauna/named_resource.rb", "lib/fauna/rails.rb", "lib/fauna/resource.rb", "lib/fauna/set.rb", "lib/fauna/util.rb", "lib/tasks/fauna.rake", "test/class_test.rb", "test/client_test.rb", "test/connection_test.rb", "test/database_test.rb", "test/query_test.rb", "test/readme_test.rb", "test/set_test.rb", "test/test_helper.rb"]
|
16
15
|
s.homepage = "http://fauna.github.com/fauna/"
|
17
16
|
s.licenses = ["Mozilla Public License, Version 2.0 (MPL2)"]
|
18
17
|
s.rdoc_options = ["--line-numbers", "--title", "Fauna", "--main", "README.md"]
|
19
18
|
s.require_paths = ["lib"]
|
20
19
|
s.rubyforge_project = "fauna"
|
21
20
|
s.rubygems_version = "2.1.10"
|
22
|
-
s.signing_key = "/Users/eweaver/cloudburst/configuration/gem_certificates/gem-private_key.pem"
|
23
21
|
s.summary = "Ruby client for the Fauna distributed database."
|
24
22
|
s.test_files = ["test/class_test.rb", "test/client_test.rb", "test/connection_test.rb", "test/database_test.rb", "test/query_test.rb", "test/readme_test.rb", "test/set_test.rb", "test/test_helper.rb"]
|
25
23
|
|
@@ -32,12 +30,14 @@ Gem::Specification.new do |s|
|
|
32
30
|
s.add_development_dependency(%q<mocha>, [">= 0"])
|
33
31
|
s.add_development_dependency(%q<echoe>, [">= 0"])
|
34
32
|
s.add_development_dependency(%q<minitest>, ["~> 4.0"])
|
33
|
+
s.add_development_dependency(%q<rubocop>, [">= 0"])
|
35
34
|
else
|
36
35
|
s.add_dependency(%q<faraday>, ["~> 0.9.0"])
|
37
36
|
s.add_dependency(%q<json>, ["~> 1.8.0"])
|
38
37
|
s.add_dependency(%q<mocha>, [">= 0"])
|
39
38
|
s.add_dependency(%q<echoe>, [">= 0"])
|
40
39
|
s.add_dependency(%q<minitest>, ["~> 4.0"])
|
40
|
+
s.add_dependency(%q<rubocop>, [">= 0"])
|
41
41
|
end
|
42
42
|
else
|
43
43
|
s.add_dependency(%q<faraday>, ["~> 0.9.0"])
|
@@ -45,5 +45,6 @@ Gem::Specification.new do |s|
|
|
45
45
|
s.add_dependency(%q<mocha>, [">= 0"])
|
46
46
|
s.add_dependency(%q<echoe>, [">= 0"])
|
47
47
|
s.add_dependency(%q<minitest>, ["~> 4.0"])
|
48
|
+
s.add_dependency(%q<rubocop>, [">= 0"])
|
48
49
|
end
|
49
50
|
end
|
data/lib/fauna.rb
CHANGED
data/lib/fauna/cache.rb
CHANGED
@@ -6,7 +6,7 @@ module Fauna
|
|
6
6
|
attr_reader :connection
|
7
7
|
|
8
8
|
def initialize(connection)
|
9
|
-
|
9
|
+
fail ArgumentError, 'Connection cannot be nil' unless connection
|
10
10
|
@cache = {}
|
11
11
|
@connection = connection
|
12
12
|
end
|
@@ -32,18 +32,16 @@ module Fauna
|
|
32
32
|
|
33
33
|
def put(ref, data)
|
34
34
|
res = @connection.put(ref, data)
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
35
|
+
return unless res['resource']
|
36
|
+
update_cache(ref, res)
|
37
|
+
res['resource']
|
39
38
|
end
|
40
39
|
|
41
40
|
def patch(ref, data)
|
42
41
|
res = @connection.patch(ref, data)
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
end
|
42
|
+
return unless res['resource']
|
43
|
+
update_cache(ref, res)
|
44
|
+
res['resource']
|
47
45
|
end
|
48
46
|
|
49
47
|
def delete(ref, data)
|
@@ -52,11 +50,11 @@ module Fauna
|
|
52
50
|
nil
|
53
51
|
end
|
54
52
|
|
55
|
-
|
53
|
+
private
|
56
54
|
|
57
55
|
def update_cache(ref, res)
|
58
|
-
# FIXME Implement set range caching
|
59
|
-
if
|
56
|
+
# FIXME: Implement set range caching
|
57
|
+
if res['resource']['class'] != 'resources' && res['resource']['class'] != 'events'
|
60
58
|
@cache[ref] = res['resource']['ref'] # store the non-canonical ref as a pointer to the real one.
|
61
59
|
@cache[res['resource']['ref']] = res['resource']
|
62
60
|
end
|
data/lib/fauna/client.rb
CHANGED
@@ -16,7 +16,7 @@ module Fauna
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def self.reset_context
|
19
|
-
stack = []
|
19
|
+
stack = [] # rubocop:disable Lint/UselessAssignment
|
20
20
|
end
|
21
21
|
|
22
22
|
def self.get(ref, query = {}, pagination = {})
|
@@ -40,11 +40,11 @@ module Fauna
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def self.connection
|
43
|
-
stack.last
|
43
|
+
stack.last || fail(NoContextError, 'You must be within a Fauna::Client.context block to perform operations.')
|
44
44
|
end
|
45
45
|
|
46
46
|
class << self
|
47
|
-
|
47
|
+
private
|
48
48
|
|
49
49
|
def stack
|
50
50
|
Thread.current[:fauna_context_stack] ||= []
|
data/lib/fauna/connection.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Fauna
|
2
|
-
class Connection
|
2
|
+
class Connection # rubocop:disable Metrics/ClassLength
|
3
3
|
class Error < RuntimeError
|
4
4
|
attr_reader :error, :reason, :parameters
|
5
5
|
|
@@ -28,25 +28,25 @@ module Fauna
|
|
28
28
|
|
29
29
|
attr_reader :domain, :scheme, :port, :credentials, :timeout, :connection_timeout, :adapter, :logger
|
30
30
|
|
31
|
-
def initialize(params={})
|
31
|
+
def initialize(params = {}) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
|
32
32
|
@logger = params[:logger] || nil
|
33
|
-
@domain = params[:domain] ||
|
34
|
-
@scheme = params[:scheme] ||
|
35
|
-
@port = params[:port] || (@scheme ==
|
33
|
+
@domain = params[:domain] || 'rest1.fauna.org'
|
34
|
+
@scheme = params[:scheme] || 'https'
|
35
|
+
@port = params[:port] || (@scheme == 'https' ? 443 : 80)
|
36
36
|
@timeout = params[:timeout] || 60
|
37
37
|
@connection_timeout = params[:connection_timeout] || 60
|
38
38
|
@adapter = params[:adapter] || Faraday.default_adapter
|
39
|
-
@credentials = params[:secret].to_s.split(
|
39
|
+
@credentials = params[:secret].to_s.split(':')
|
40
40
|
|
41
|
-
if ENV[
|
41
|
+
if ENV['FAUNA_DEBUG']
|
42
42
|
@logger = Logger.new(STDERR)
|
43
43
|
@logger.formatter = proc { |_, _, _, msg| "#{msg}\n" }
|
44
44
|
end
|
45
45
|
|
46
46
|
@conn = Faraday.new(
|
47
47
|
:url => "#{@scheme}://#{@domain}:#{@port}/",
|
48
|
-
:headers => {
|
49
|
-
:request => { :timeout => @timeout, :open_timeout => @connection_timeout }
|
48
|
+
:headers => { 'Accept-Encoding' => 'gzip', 'Content-Type' => 'application/json;charset=utf-8' },
|
49
|
+
:request => { :timeout => @timeout, :open_timeout => @connection_timeout },
|
50
50
|
) do |conn|
|
51
51
|
conn.adapter(@adapter)
|
52
52
|
conn.basic_auth(@credentials[0].to_s, @credentials[1].to_s)
|
@@ -74,39 +74,36 @@ module Fauna
|
|
74
74
|
nil
|
75
75
|
end
|
76
76
|
|
77
|
-
|
77
|
+
private
|
78
78
|
|
79
79
|
def parse(headers, body)
|
80
80
|
obj = body.empty? ? {} : JSON.parse(body)
|
81
|
-
obj.merge!(
|
81
|
+
obj.merge!('headers' => headers)
|
82
82
|
obj
|
83
83
|
end
|
84
84
|
|
85
85
|
def log(indent)
|
86
|
-
Array(yield).
|
87
|
-
|
88
|
-
end.flatten.each do |line|
|
89
|
-
@logger.debug(" " * indent + line)
|
90
|
-
end
|
86
|
+
lines = Array(yield).collect { |string| string.split("\n") }
|
87
|
+
lines.flatten.each { |line| @logger.debug(' ' * indent + line) }
|
91
88
|
end
|
92
89
|
|
93
90
|
def query_string_for_logging(query)
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
end
|
91
|
+
return unless query && !query.empty?
|
92
|
+
|
93
|
+
'?' + query.collect do |k, v|
|
94
|
+
"#{k}=#{v}"
|
95
|
+
end.join('&')
|
99
96
|
end
|
100
97
|
|
101
98
|
def inflate(response)
|
102
|
-
if
|
99
|
+
if %w(gzip deflate).include?(response.headers['Content-Encoding'])
|
103
100
|
Zlib::GzipReader.new(StringIO.new(response.body.to_s), :external_encoding => Encoding::UTF_8).read
|
104
101
|
else
|
105
102
|
response.body.to_s
|
106
103
|
end
|
107
104
|
end
|
108
105
|
|
109
|
-
def execute(action, ref, data = nil, query = nil)
|
106
|
+
def execute(action, ref, data = nil, query = nil) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
|
110
107
|
if @logger
|
111
108
|
log(0) { "Fauna #{action.to_s.upcase} /#{ref}#{query_string_for_logging(query)}" }
|
112
109
|
log(2) { "Credentials: #{@credentials}" }
|
@@ -120,7 +117,7 @@ module Fauna
|
|
120
117
|
real = r1.to_f - r0.to_f
|
121
118
|
cpu = (t1.utime - t0.utime) + (t1.stime - t0.stime) + (t1.cutime - t0.cutime) + (t1.cstime - t0.cstime)
|
122
119
|
log(2) { ["Response headers: #{JSON.pretty_generate(response.headers)}", "Response JSON: #{body}"] }
|
123
|
-
log(2) { "Response (#{response.status}): API processing #{response.headers[
|
120
|
+
log(2) { "Response (#{response.status}): API processing #{response.headers['X-HTTP-Request-Processing-Time']}ms, network latency #{((real - cpu) * 1000).to_i}ms, local processing #{(cpu * 1000).to_i}ms" }
|
124
121
|
else
|
125
122
|
response = execute_without_logging(action, ref, data, query)
|
126
123
|
body = inflate(response)
|
@@ -130,17 +127,17 @@ module Fauna
|
|
130
127
|
when 200..299
|
131
128
|
[response.headers, body]
|
132
129
|
when 400
|
133
|
-
|
130
|
+
fail BadRequest.new(JSON.parse(body))
|
134
131
|
when 401
|
135
|
-
|
132
|
+
fail Unauthorized.new(JSON.parse(body))
|
136
133
|
when 403
|
137
|
-
|
134
|
+
fail PermissionDenied.new(JSON.parse(body))
|
138
135
|
when 404
|
139
|
-
|
136
|
+
fail NotFound.new(JSON.parse(body))
|
140
137
|
when 405
|
141
|
-
|
138
|
+
fail MethodNotAllowed.new(JSON.parse(body))
|
142
139
|
else
|
143
|
-
|
140
|
+
fail NetworkError, body
|
144
141
|
end
|
145
142
|
end
|
146
143
|
|
@@ -148,7 +145,7 @@ module Fauna
|
|
148
145
|
@conn.send(action) do |req|
|
149
146
|
req.params = query if query.is_a?(Hash)
|
150
147
|
req.body = data.to_json if data.is_a?(Hash)
|
151
|
-
req.url(ref)
|
148
|
+
req.url(ref || '')
|
152
149
|
end
|
153
150
|
end
|
154
151
|
end
|
data/lib/fauna/named_resource.rb
CHANGED
data/lib/fauna/rails.rb
CHANGED
@@ -10,11 +10,12 @@ if defined?(Rails)
|
|
10
10
|
@silent = false
|
11
11
|
|
12
12
|
CONFIG_FILE = "#{Rails.root}/config/fauna.yml"
|
13
|
-
LOCAL_CONFIG_FILE = "#{ENV[
|
14
|
-
APP_NAME = Rails.application.class.name.split(
|
13
|
+
LOCAL_CONFIG_FILE = "#{ENV['HOME']}/.fauna.yml"
|
14
|
+
APP_NAME = Rails.application.class.name.split('::').first.underscore
|
15
15
|
FIXTURES_DIR = "#{Rails.root}/test/fixtures/fauna"
|
16
16
|
|
17
|
-
|
17
|
+
# rubocop:disable Metrics/BlockNesting
|
18
|
+
def self.auth! # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
|
18
19
|
if File.exist? CONFIG_FILE
|
19
20
|
credentials = YAML.load_file(CONFIG_FILE)[Rails.env] || {}
|
20
21
|
|
@@ -22,37 +23,38 @@ if defined?(Rails)
|
|
22
23
|
credentials.merge!((YAML.load_file(LOCAL_CONFIG_FILE)[APP_NAME] || {})[Rails.env] || {})
|
23
24
|
end
|
24
25
|
|
25
|
-
|
26
|
-
if credentials[
|
27
|
-
STDERR.puts ">> Using Fauna server key #{credentials[
|
26
|
+
unless @silent
|
27
|
+
if credentials['secret']
|
28
|
+
STDERR.puts ">> Using Fauna server key #{credentials['secret'].inspect} for #{APP_NAME.inspect}."
|
28
29
|
else
|
29
|
-
STDERR.puts ">> Using Fauna account #{credentials[
|
30
|
+
STDERR.puts ">> Using Fauna account #{credentials['email'].inspect} for #{APP_NAME.inspect}."
|
30
31
|
end
|
31
32
|
|
32
|
-
STDERR.puts
|
33
|
+
STDERR.puts '>> You can change this in config/fauna.yml or ~/.fauna.yml.'
|
33
34
|
end
|
34
35
|
|
35
|
-
if credentials[
|
36
|
-
secret = credentials[
|
36
|
+
if credentials['secret']
|
37
|
+
secret = credentials['secret']
|
37
38
|
else
|
38
39
|
self.root_connection = Connection.new(
|
39
|
-
:email => credentials[
|
40
|
-
:password => credentials[
|
41
|
-
|
40
|
+
:email => credentials['email'],
|
41
|
+
:password => credentials['password'],
|
42
|
+
:logger => Rails.logger)
|
42
43
|
|
43
|
-
secret = root_connection.post(
|
44
|
+
secret = root_connection.post('keys', 'role' => 'server')['resource']['key']
|
44
45
|
end
|
45
46
|
|
46
|
-
self.connection = Connection.new(secret
|
47
|
+
self.connection = Connection.new(:secret => secret, :logger => Rails.logger)
|
47
48
|
else
|
48
|
-
|
49
|
-
STDERR.puts
|
49
|
+
unless @silent
|
50
|
+
STDERR.puts '>> Fauna account not configured. You can add one in config/fauna.yml.'
|
50
51
|
end
|
51
52
|
end
|
52
53
|
|
53
54
|
@silent = true
|
54
55
|
nil
|
55
56
|
end
|
57
|
+
# rubocop:enable Metrics/BlockNesting
|
56
58
|
|
57
59
|
# Around filter to set up a default context
|
58
60
|
|
@@ -85,7 +87,7 @@ if defined?(Rails)
|
|
85
87
|
# suffix for association fields, but not _ref.
|
86
88
|
if defined? ActiveSupport::Inflector
|
87
89
|
ActiveSupport::Inflector.inflections do |inflect|
|
88
|
-
inflect.human
|
90
|
+
inflect.human(/_ref$/i, '')
|
89
91
|
end
|
90
92
|
end
|
91
93
|
end
|