Class: Fauna::Client
- Inherits:
-
Object
- Object
- Fauna::Client
- 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": { "id": "123", "class": { "@ref": { "id": "frogs", "class": { "@ref": { "id": "classes" } } } } } }, you will get Fauna::Ref.new("123", Fauna::Ref.new("frogs", Fauna::Native.classes))).
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Faraday adapter in use.
-
#connection_timeout ⇒ Object
readonly
Open timeout in seconds.
-
#credentials ⇒ Object
readonly
An array of the user and pass used for authentication when sending requests.
-
#domain ⇒ Object
readonly
The domain requests will be sent to.
-
#observer ⇒ Object
readonly
Callback that will be passed a
RequestResultafter every completed request. -
#port ⇒ Object
readonly
Port used when sending requests.
-
#read_timeout ⇒ Object
readonly
Read timeout in seconds.
-
#scheme ⇒ Object
readonly
Scheme used when sending requests (either
httporhttps).
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ Client
constructor
Create a new Client.
-
#paginate(set, params = {}, &fauna_map) ⇒ Object
Creates a Fauna::Page for paging/iterating over a set.
-
#ping(params = {}) ⇒ Object
Ping FaunaDB.
-
#query(expression = nil, &expr_block) ⇒ Object
Issues a query to FaunaDB.
-
#with_secret(secret) ⇒ Object
Create a new client from the existing config with a given secret.
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
httporhttps). :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] || 'db.fauna.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
#adapter ⇒ Object (readonly)
Faraday adapter in use.
28 29 30 |
# File 'lib/fauna/client.rb', line 28 def adapter @adapter end |
#connection_timeout ⇒ Object (readonly)
Open timeout in seconds.
24 25 26 |
# File 'lib/fauna/client.rb', line 24 def connection_timeout @connection_timeout end |
#credentials ⇒ Object (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 |
#domain ⇒ Object (readonly)
The domain requests will be sent to.
14 15 16 |
# File 'lib/fauna/client.rb', line 14 def domain @domain end |
#observer ⇒ Object (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 |
#port ⇒ Object (readonly)
Port used when sending requests.
18 19 20 |
# File 'lib/fauna/client.rb', line 18 def port @port end |
#read_timeout ⇒ Object (readonly)
Read timeout in seconds.
22 23 24 |
# File 'lib/fauna/client.rb', line 22 def read_timeout @read_timeout end |
#scheme ⇒ Object (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
#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 |
#ping(params = {}) ⇒ Object
110 111 112 |
# File 'lib/fauna/client.rb', line 110 def ping(params = {}) execute(:get, :ping, params) 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? execute(:post, :'', nil, Fauna::Query::Expr.wrap(expression)) else execute(:post, :'', nil, 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 |