Class: WAB::Client

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

Overview

A client for a WAB server. It is not specific to any particular runner. The client allows direct access to the server data. It is just another view implementation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addr, port, options = {}) ⇒ Client

Create a new Client for the server at addr and port. The provided options should match the attribute names and types.



26
27
28
29
30
31
32
33
34
# File 'lib/wab/client.rb', line 26

def initialize(addr, port, options={})
  @server_address = addr
  @server_port = port
  @path_prefix = options.fetch(:path_prefix, '/v1/')
  @tql_path = options.fetch(:tql_path, '/tql')
  @type_key = options.fetch(:type_key, 'kind').to_sym
  @keep_alive = !!options.fetch(:keep_alive, true)
  @http = nil
end

Instance Attribute Details

#keep_aliveObject

If true the connection to the server is Keep-Alive.



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

def keep_alive
  @keep_alive
end

#path_prefixObject

Prefix to add to the URL path.



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

def path_prefix
  @path_prefix
end

#server_addressObject

Address of the WAB server.



12
13
14
# File 'lib/wab/client.rb', line 12

def server_address
  @server_address
end

#server_portObject

The port the WAB serer is listening on.



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

def server_port
  @server_port
end

#tql_pathObject

The URL path to execute TQL,



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

def tql_path
  @tql_path
end

#type_keyObject

The key for the type. The default is ‘kind’.



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

def type_key
  @type_key
end

Instance Method Details

#create(data, kind = nil, query = nil) ⇒ Object

Create a new data object. If a query is provided it is treated as a check against an existing object with the same key/value pairs.

On error an Exception will be raised.

data

the data to use as a new object.

kind

the kind of the data. If nil the kind is taken from the data

query

query parameters to match against existing instances. A match fails the insert.



44
45
46
47
# File 'lib/wab/client.rb', line 44

def create(data, kind=nil, query=nil)
  kind ||= data[@type_key] || data[@type_key.to_s]
  send_request('PUT', kind, query, data)
end

#delete(kind, query = nil) ⇒ Object

Deletes the data for the identified object(s). The query can be a reference to a specific object or a set of parameters to match.

The return is the identifiers for the object updated.

On error an Exception should be raised.

kind

the data type

query

query parameters.



87
88
89
# File 'lib/wab/client.rb', line 87

def delete(kind, query=nil)
  send_request('DELETE', kind, query, nil)
end

#find(tql) ⇒ Object

Request the server evaluate a TQL query.

tql

query to evaluate.

Raises:



94
95
96
97
# File 'lib/wab/client.rb', line 94

def find(tql)
  raise ArgError.new('tql') if tql.nil?
  send_request('POST', 'tql', nil, tql)
end

#read(kind, query = nil) ⇒ Object

Return the objects according to the kind and query arguments. The following patterns supported:

  • query is a ref [12345] looks for MyType with reference ID of 12345

  • query is a Hash name:fred,age:63 looks for all MyTypes with a name

    of 'fred' and an age of 63.
    
kind

the data type

query

query parameters as a Hash.



58
59
60
# File 'lib/wab/client.rb', line 58

def read(kind, query=nil)
  send_request('GET', kind, query, nil)
end

#update(kind, data, query) ⇒ Object

Replaces the object data for the identified object. The query can be a reference to a specific object or a set of parameters to match.

The return should be the identifiers for the object updated.

On error an Exception should be raised.

kind

the data type

data

the data to use as a new object.

query

query parameters.

Raises:



72
73
74
75
76
# File 'lib/wab/client.rb', line 72

def update(kind, data, query)
  raise ArgError.new('data') if data.nil?
  raise ArgError.new('query') if query.nil?
  send_request('POST', kind, query, data)
end