Class: Datomic::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/datomic/client.rb,
lib/datomic/client/version.rb,
lib/datomic/client/response.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

HANDLE_RESPONSE =
lambda do |body, request, response|
  Response.new body, response, request
end
VERSION =
'0.4.1'

Instance Method Summary collapse

Constructor Details

#initialize(url, storage = nil) ⇒ Client

Returns a new instance of Client.



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

def initialize(url, storage = nil)
  @url = url
  @storage = storage
end

Instance Method Details

#create_database(dbname) ⇒ Object



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

def create_database(dbname)
  RestClient.post root_url('data', @storage) + "/", {"db-name" => dbname},
    :content_type => 'application/x-www-form-urlencoded', &HANDLE_RESPONSE
end

#database_info(dbname, options = {}) ⇒ Object

Options:

  • :t - Specifies version/time of db. Defaults to latest version



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

def database_info(dbname, options = {})
  version = options.fetch(:t, '-')
  get db_url(dbname, version) + "/", :Accept => 'application/edn'
end

#datoms(dbname, params = {}) ⇒ Object

This endpoint hits both datoms and index-range APIs. params take any param in addition to following options:

Options:

  • :t - Specifies version/time of db. Defaults to latest version



41
42
43
44
# File 'lib/datomic/client.rb', line 41

def datoms(dbname, params = {})
  version = params.fetch(:t, '-')
  get db_url(dbname, version, "datoms"), params
end

#db_alias(dbname) ⇒ Object



75
76
77
# File 'lib/datomic/client.rb', line 75

def db_alias(dbname)
  {:"db/alias" => "#@storage/#{dbname}"}
end

#entity(dbname, id, params = {}) ⇒ Object

params take any param in addition to following options:

Options:

  • :t - Specifies version/time of db. Defaults to latest version



50
51
52
53
# File 'lib/datomic/client.rb', line 50

def entity(dbname, id, params = {})
  version = params.fetch(:t, '-')
  get db_url(dbname, version, 'entity'), params.merge(:e => id)
end

#events(dbname, &block) ⇒ Object

Streams events. For each event, given block is called with Net::HTTP response from event



67
68
69
70
71
72
73
# File 'lib/datomic/client.rb', line 67

def events(dbname, &block)
  # can't use RestClient.get b/c of :block_response
  RestClient::Request.execute(:method => :get,
    :url => root_url('events', @storage, dbname),
    :headers => {:accept => "text/event-stream"},
    :block_response => block, &HANDLE_RESPONSE)
end

#query(query, args_or_dbname, params = {}) ⇒ Object

Query can be a ruby data structure or a string representing clojure data If the args_or_dbname is a String, it will be converted into one arg pointing to that database.



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

def query(query, args_or_dbname, params = {})
  query = transmute_data(query)
  args = args_or_dbname.is_a?(String) ? [db_alias(args_or_dbname)] : args_or_dbname
  args = transmute_data(args)
  get root_url("api/query"), params.merge(:q => query, :args => args)
end

#transact(dbname, data) ⇒ Object

Data can be a ruby data structure or a string representing clojure data



30
31
32
33
34
# File 'lib/datomic/client.rb', line 30

def transact(dbname, data)
  data = transmute_data(data)
  RestClient.post(db_url(dbname) + "/", {"tx-data" => data},
                  :Accept => 'application/edn', &HANDLE_RESPONSE)
end