Class: Fauna::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fauna/client.rb

Overview

The Ruby client for FaunaDB.

All methods return a converted JSON response. This is a Hash containing Arrays, ints, floats, strings, and other Hashes. Hash keys are always Symbols.

Any Ref, SetRef, Time or Date values in it will also be parsed. (So instead of { "@ref": "classes/frogs/123" }, you will get Fauna::Ref.new("classes/frogs/123")).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Client

Create a new Client.

params

A list of parameters to configure the connection with.

:domain

The domain to send requests to.

:scheme

Scheme to use when sending requests (either http or https).

:port

Port to use when sending requests.

:secret

Credentials to use when sending requests. User and pass must be separated by a colon.

:read_timeout

Read timeout in seconds.

:connection_timeout

Open timeout in seconds.

:observer

Callback that will be passed a RequestResult after every completed request.

:adapter

Faraday adapter to use. Either can be a symbol for the adapter, or an array of arguments.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fauna/client.rb', line 42

def initialize(params = {})
  @domain = params[:domain] || 'cloud.faunadb.com'
  @scheme = params[:scheme] || 'https'
  @port = params[:port] || (scheme == 'https' ? 443 : 80)
  @read_timeout = params[:read_timeout] || 60
  @connection_timeout = params[:connection_timeout] || 60
  @observer = params[:observer]
  @adapter = params[:adapter] || :net_http_persistent
  init_credentials(params[:secret])

  init_connection
end

Instance Attribute Details

#adapterObject (readonly)

Faraday adapter in use.



28
29
30
# File 'lib/fauna/client.rb', line 28

def adapter
  @adapter
end

#connection_timeoutObject (readonly)

Open timeout in seconds.



24
25
26
# File 'lib/fauna/client.rb', line 24

def connection_timeout
  @connection_timeout
end

#credentialsObject (readonly)

An array of the user and pass used for authentication when sending requests.



20
21
22
# File 'lib/fauna/client.rb', line 20

def credentials
  @credentials
end

#domainObject (readonly)

The domain requests will be sent to.



14
15
16
# File 'lib/fauna/client.rb', line 14

def domain
  @domain
end

#observerObject (readonly)

Callback that will be passed a RequestResult after every completed request.



26
27
28
# File 'lib/fauna/client.rb', line 26

def observer
  @observer
end

#portObject (readonly)

Port used when sending requests.



18
19
20
# File 'lib/fauna/client.rb', line 18

def port
  @port
end

#read_timeoutObject (readonly)

Read timeout in seconds.



22
23
24
# File 'lib/fauna/client.rb', line 22

def read_timeout
  @read_timeout
end

#schemeObject (readonly)

Scheme used when sending requests (either http or https).



16
17
18
# File 'lib/fauna/client.rb', line 16

def scheme
  @scheme
end

Instance Method Details

#delete(path) ⇒ Object

Performs a DELETE request for a REST endpoint.

path

Path to DELETE.

Reference: FaunaDB REST API

:category: REST Methods



164
165
166
# File 'lib/fauna/client.rb', line 164

def delete(path)
  execute(:delete, path)
end

#get(path, query = {}) ⇒ Object

Performs a GET request for a REST endpoint.

path

Path to GET.

query

Query parameters to append to the path.

Reference: FaunaDB REST API

:category: REST Methods



113
114
115
# File 'lib/fauna/client.rb', line 113

def get(path, query = {})
  execute(:get, path.to_s, query)
end

#paginate(set, params = {}, &fauna_map) ⇒ Object

Creates a Fauna::Page for paging/iterating over a set.

set

A set query to paginate over.

params

A list of parameters to pass to paginate.

fauna_map

Optional block to wrap the generated paginate query with. The block will be run in a query context. The paginate query will be passed into the block as an argument.



100
101
102
# File 'lib/fauna/client.rb', line 100

def paginate(set, params = {}, &fauna_map)
  Fauna::Page.new(self, set, params, &fauna_map)
end

#patch(path, data = {}) ⇒ Object

Performs a PATCH request for a REST endpoint.

path

Path to PATCH.

data

Data to post as the body. data is automatically converted to JSON.

Reference: FaunaDB REST API

:category: REST Methods



152
153
154
# File 'lib/fauna/client.rb', line 152

def patch(path, data = {})
  execute(:patch, path, nil, data)
end

#ping(params = {}) ⇒ Object

Ping FaunaDB.

Reference: FaunaDB Rest API.

:category: REST Methods



174
175
176
# File 'lib/fauna/client.rb', line 174

def ping(params = {})
  get 'ping', params
end

#post(path, data = {}) ⇒ Object

Performs a POST request for a REST endpoint.

path

Path to POST.

data

Data to post as the body. data is automatically converted to JSON.

Reference: FaunaDB REST API

:category: REST Methods



126
127
128
# File 'lib/fauna/client.rb', line 126

def post(path, data = {})
  execute(:post, path, nil, data)
end

#put(path, data = {}) ⇒ Object

Performs a PUT request for a REST endpoint.

path

Path to PUT.

data

Data to post as the body. data is automatically converted to JSON.

Reference: FaunaDB REST API

:category: REST Methods



139
140
141
# File 'lib/fauna/client.rb', line 139

def put(path, data = {})
  execute(:put, path, nil, data)
end

#query(expression = nil, &expr_block) ⇒ Object

Issues a query to FaunaDB.

Queries are built via the Query helpers. See FaunaDB Query API for information on constructing queries.

expression

A query expression

expr_block

May be provided instead of expression. Block is used to build an expression with Fauna.query.

Example using expression:

client.query(Fauna::Query.add(1, 2, Fauna::Query.subtract(3, 2)))

Example using block:

client.query { add(1, 2, subtract(3, 2)) }

Reference: Executing FaunaDB Queries

:category: Query Methods



85
86
87
88
89
90
91
# File 'lib/fauna/client.rb', line 85

def query(expression = nil, &expr_block)
  if expr_block.nil?
    post '', Fauna::Query::Expr.wrap(expression)
  else
    post '', Fauna::Query.expr(&expr_block)
  end
end

#with_secret(secret) ⇒ Object

Create a new client from the existing config with a given secret.

:secret

Credentials to use when sending requests. User and pass must be separated by a colon.



59
60
61
62
63
# File 'lib/fauna/client.rb', line 59

def with_secret(secret)
  with_dup do |client|
    client.send(:init_credentials, secret)
  end
end