class WAB::Client

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.

Attributes

keep_alive[RW]

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

path_prefix[RW]

Prefix to add to the URL path.

server_address[RW]

Address of the WAB server.

server_port[RW]

The port the WAB serer is listening on.

tql_path[RW]

The URL path to execute TQL,

type_key[RW]

The key for the type. The default is 'kind'.

Public Class Methods

new(addr, port, options={}) click to toggle source

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

# 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

Public Instance Methods

create(data, kind=nil, query=nil) click to toggle source

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.

# 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) click to toggle source

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.

# File lib/wab/client.rb, line 87
def delete(kind, query=nil)
  send_request('DELETE', kind, query, nil)
end
find(tql) click to toggle source

Request the server evaluate a TQL query.

tql

query to evaluate.

# 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) click to toggle source

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.

# File lib/wab/client.rb, line 58
def read(kind, query=nil)
  send_request('GET', kind, query, nil)
end
update(kind, data, query) click to toggle source

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.

# 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

Private Instance Methods

connect() click to toggle source
# File lib/wab/client.rb, line 101
def connect
  @http ||= Net::HTTP.new(@server_address, @server_port)
end
form_path(kind, query) click to toggle source
# File lib/wab/client.rb, line 105
def form_path(kind, query)
  return @tql_path if kind == 'tql'
  path = "#{@path_prefix}#{kind}"
  if query.is_a?(Hash)
    first = true
    query.each_pair { |k,v|
      if first
        path << '?'
        first = false
      else
        path << '&'
      end
      path << "#{k}=#{v}"
    }
  elsif !query.nil?
    path << '/'
    path << query.to_s
  end
  path
end
send_request(method, kind, query, content) click to toggle source
# File lib/wab/client.rb, line 126
def send_request(method, kind, query, content)
  connect
  header = {'Connection' => (@keep_alive ? 'Keep-Alive' : 'Close') }
  unless content.nil?
    if content.is_a?(Data)
      content = content.json
    else
      content = Oj.dump(content, mode: :wab, indent: 0)
    end
    header['Content-Type'] = 'application/json'
    resp = @http.send_request(method, form_path(kind, query), content, header)
  else
    resp = @http.send_request(method, form_path(kind, query))
  end
  raise Error.new(resp.body) unless resp.is_a?(Net::HTTPOK)
  Oj.load(resp.body, mode: :wab)
end