class RiakJson::Client

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.

Attributes

collection_cache[RW]
host[RW]
port[RW]
transport[RW]

Public Class Methods

load_config_file(config_file) click to toggle source

Load a config file in YAML format

# File lib/riak_json/client.rb, line 128
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
new(host=RiakJson::RIAK_TEST_HOST, port=RiakJson::RIAK_TEST_PORT) click to toggle source
# 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

Public Instance Methods

base_collection_url() click to toggle source
# File lib/riak_json/client.rb, line 48
def base_collection_url
  "#{self.base_riak_json_url}/collection"
end
base_riak_json_url() click to toggle source
# File lib/riak_json/client.rb, line 56
def base_riak_json_url
  "#{self.base_riak_url}/document"
end
base_riak_url() click to toggle source
# File lib/riak_json/client.rb, line 52
def base_riak_url
  "http://#{self.host}:#{self.port}"
end
collection(name) click to toggle source
# File lib/riak_json/client.rb, line 60
def collection(name)
  self.collection_cache[name] ||= RiakJson::Collection.new(name, self)
end
collection_index_name(collection_name) click to toggle source

Return the name of the Solr index (generated by RiakJson) for a collection

# File lib/riak_json/client.rb, line 65
def collection_index_name(collection_name)
  "#{collection_name}RJIndex"
end
collections() click to toggle source

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. @return [Array] List of RiakJson::Collection instances that exist in the cluster.

# File lib/riak_json/client.rb, line 79
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) click to toggle source
# File lib/riak_json/client.rb, line 85
def delete_json_object(collection_name, key)
  self.transport.send_request("#{self.base_collection_url}/#{collection_name}/#{key}", :delete)
end
delete_schema(collection_name) click to toggle source
# File lib/riak_json/client.rb, line 89
def delete_schema(collection_name)
  self.transport.send_request("#{self.base_collection_url}/#{collection_name}/schema", :delete)
end
get_json_object(collection_name, key) click to toggle source
# File lib/riak_json/client.rb, line 93
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) click to toggle source
# File lib/riak_json/client.rb, line 97
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) click to toggle source
# File lib/riak_json/client.rb, line 101
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) click to toggle source
# File lib/riak_json/client.rb, line 105
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) click to toggle source

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

@param format [String] @param key - can be nil @param json [String] @return [String] Returns the key for the inserted document

# File lib/riak_json/client.rb, line 118
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
ping() click to toggle source

Perform an HTTP ping to the Riak cluster

# File lib/riak_json/client.rb, line 134
def ping
  response = self.transport.get_request("#{self.base_riak_url}/ping")
end
post_to_collection(collection_name, json) click to toggle source
# File lib/riak_json/client.rb, line 138
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) click to toggle source
# File lib/riak_json/client.rb, line 149
def set_schema_json(collection_name, json)
  self.transport.send_request("#{self.base_collection_url}/#{collection_name}/schema", :put, json)
end
solr_query_raw(collection_name, query_params) click to toggle source

Perform an arbitrary raw Solr query to a collection’s index @param [String] collection_name @param [String] query_params Arbitrary query parameters that will be passed to /solr/collectionRJIndex?… endpoint @return [String] JSON result from the query

# File lib/riak_json/client.rb, line 157
def solr_query_raw(collection_name, query_params)
  url = "#{self.base_riak_url}/search/#{self.collection_index_name(collection_name)}"
  self.transport.send_request(url, :get, query_params)
end
update_json_object(collection_name, key, json) click to toggle source
# File lib/riak_json/client.rb, line 162
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