Class: RiakJson::Client

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

Overview

RiakJson::Client makes REST calls to the Riak Json API endpoints, on behalf of a Collection. Stores the details of a Riak/RiakJson HTTP connection (host, port), and manages a cache of collection references. Uses a pluggable ClientTransport component to make the actual HTTP requests.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = RiakJson::RIAK_TEST_HOST, port = RiakJson::RIAK_TEST_PORT) ⇒ Client

Returns a new instance of Client.



41
42
43
44
45
46
# File 'lib/riak_json/client.rb', line 41

def initialize(host=RiakJson::RIAK_TEST_HOST, port=RiakJson::RIAK_TEST_PORT)
  @collection_cache = {}
  @transport = RiakJson::ClientTransport.new
  @host = host
  @port = port
end

Instance Attribute Details

#collection_cacheObject

Returns the value of attribute collection_cache.



37
38
39
# File 'lib/riak_json/client.rb', line 37

def collection_cache
  @collection_cache
end

#hostObject

Returns the value of attribute host.



39
40
41
# File 'lib/riak_json/client.rb', line 39

def host
  @host
end

#portObject

Returns the value of attribute port.



39
40
41
# File 'lib/riak_json/client.rb', line 39

def port
  @port
end

#transportObject

Returns the value of attribute transport.



38
39
40
# File 'lib/riak_json/client.rb', line 38

def transport
  @transport
end

Class Method Details

.load_config_file(config_file) ⇒ Object

Load a config file in YAML format



123
124
125
126
# File 'lib/riak_json/client.rb', line 123

def self.load_config_file(config_file)
  config_file = File.expand_path(config_file)
  config_hash = YAML.load(ERB.new(File.read(config_file)).result)
end

Instance Method Details

#base_collection_urlObject



48
49
50
# File 'lib/riak_json/client.rb', line 48

def base_collection_url
  "#{self.base_riak_json_url}/collection"
end

#base_riak_json_urlObject



56
57
58
# File 'lib/riak_json/client.rb', line 56

def base_riak_json_url
  "#{self.base_riak_url}/document"
end

#base_riak_urlObject



52
53
54
# File 'lib/riak_json/client.rb', line 52

def base_riak_url
  "http://#{self.host}:#{self.port}"
end

#collection(name) ⇒ Object



60
61
62
# File 'lib/riak_json/client.rb', line 60

def collection(name)
  self.collection_cache[name] ||= RiakJson::Collection.new(name, self)
end

#collectionsArray

List all of the RiakJson collections on the riak cluster This is different from a Riak ‘list buckets’ command. Instead of iterating over all the keys on the cluster, ‘list collections’ only lists the custom RJ bucket types on the cluster (from the ring metadata) Raw JSON that’s returned by RJ:

{"collections":[{"name":"collection1"},{"name":"collection2"}]}

This is then mapped to a list of RiakJsonCollection instances.

Returns:

  • (Array)

    List of RiakJson::Collection instances that exist in the cluster.



74
75
76
77
78
# File 'lib/riak_json/client.rb', line 74

def collections
  result = self.transport.send_request("#{self.base_collection_url}", :get)
  collection_list = JSON.parse(result)['collections']
  collection_list.map { |ea| self.collection(ea['name'])}
end

#delete_json_object(collection_name, key) ⇒ Object



80
81
82
# File 'lib/riak_json/client.rb', line 80

def delete_json_object(collection_name, key)
  self.transport.send_request("#{self.base_collection_url}/#{collection_name}/#{key}", :delete)
end

#delete_schema(collection_name) ⇒ Object



84
85
86
# File 'lib/riak_json/client.rb', line 84

def delete_schema(collection_name)
  self.transport.send_request("#{self.base_collection_url}/#{collection_name}/schema", :delete)
end

#get_json_object(collection_name, key) ⇒ Object



88
89
90
# File 'lib/riak_json/client.rb', line 88

def get_json_object(collection_name, key)
  self.transport.send_request("#{self.base_collection_url}/#{collection_name}/#{key}", :get)
end

#get_query_all(collection_name, query_json) ⇒ Object



92
93
94
# File 'lib/riak_json/client.rb', line 92

def get_query_all(collection_name, query_json)
  self.transport.send_request("#{self.base_collection_url}/#{collection_name}/query/all", :put, query_json)
end

#get_query_one(collection_name, query_json) ⇒ Object



96
97
98
# File 'lib/riak_json/client.rb', line 96

def get_query_one(collection_name, query_json)
  self.transport.send_request("#{self.base_collection_url}/#{collection_name}/query/one", :put, query_json)
end

#get_schema(collection_name) ⇒ Object



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

def get_schema(collection_name)
  self.transport.send_request("#{self.base_collection_url}/#{collection_name}/schema", :get)
end

#insert_json_object(collection_name, key, json) ⇒ String

Sends a JSON document to a collection resource If a key is specified, issues a PUT to that key If key is nil, issues a POST to the collection, and returns the

key generated by RiakJson

Parameters:

  • format (String)
  • key
    • can be nil

  • json (String)

Returns:

  • (String)

    Returns the key for the inserted document



113
114
115
116
117
118
119
120
# File 'lib/riak_json/client.rb', line 113

def insert_json_object(collection_name, key, json)
  if key.nil?
    key = self.post_to_collection(collection_name, json)
  else
    self.transport.send_request("#{self.base_collection_url}/#{collection_name}/#{key}", :put, json)
    key
  end
end

#pingObject

Perform an HTTP ping to the Riak cluster



129
130
131
# File 'lib/riak_json/client.rb', line 129

def ping
  response = self.transport.get_request("#{self.base_riak_url}/ping")
end

#post_to_collection(collection_name, json) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/riak_json/client.rb', line 133

def post_to_collection(collection_name, json)
  response = self.transport.send_request("#{self.base_collection_url}/#{collection_name}", :post, json)
  if response.code == 201
    location = response.headers[:location]
    key = location.split('/').last
  else
    raise Exception, "Error inserting document into collection - key not returned"
  end
  key
end

#set_schema_json(collection_name, json) ⇒ Object



144
145
146
# File 'lib/riak_json/client.rb', line 144

def set_schema_json(collection_name, json)
  self.transport.send_request("#{self.base_collection_url}/#{collection_name}/schema", :put, json)
end

#update_json_object(collection_name, key, json) ⇒ Object



148
149
150
151
152
153
# File 'lib/riak_json/client.rb', line 148

def update_json_object(collection_name, key, json)
  if key.nil? or key.empty?
    raise Exception, "Error: cannot update document, key missing"
  end
  self.transport.send_request("#{self.base_collection_url}/#{collection_name}/#{key}", :put, json)
end