From 94eb836dcb918c7da574fe1e07d93be4b046daa3 Mon Sep 17 00:00:00 2001 From: Cameron Walters Date: Sat, 2 May 2009 14:58:58 -0700 Subject: [PATCH] Implemented #add_documents --- lib/cloudquery.rb | 34 ++++++++++++++++++++-- spec/cloudquery_spec.rb | 63 +++++++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 1 + 3 files changed, 95 insertions(+), 3 deletions(-) diff --git a/lib/cloudquery.rb b/lib/cloudquery.rb index 23801ad..5c196c2 100644 --- a/lib/cloudquery.rb +++ b/lib/cloudquery.rb @@ -15,6 +15,7 @@ module Cloudquery :account => "account".freeze, :schema => "schema".freeze, :indexes => "i".freeze, + :documents => "i".freeze, }.freeze # standard Content-Types for requests @@ -129,12 +130,16 @@ def initialize(options={}) # unless options[:account] && options[:secret] # raise "Client requires :account => and :secret => " # end + @account = options[:account] @secret = options[:secret] + @secure = options[:secure] != false # must pass false for insecure + + @document_id_method = nil end - # Account Management + # Account management def get_account send_request get(build_path(API_PATHS[:account], @account)) @@ -149,6 +154,8 @@ def delete_account send_request delete(build_path(API_PATHS[:account], @account)) end + # Schema management + def add_schema(xml) body = xml.instance_of?(File) ? xml.read : xml request = post(build_path(API_PATHS[:schema]), body) @@ -166,6 +173,8 @@ def get_schemas send_request get(build_path(API_PATHS[:schema])) end + # Index management + def add_indexes(*indexes) body = JSON.generate(indexes.flatten) send_request post(build_path(API_PATHS[:indexes]), body) @@ -180,6 +189,20 @@ def get_indexes send_request get(build_path(API_PATHS[:indexes])) end + # Document management + + def add_documents(index, docs, *schemas) + docs = [docs] if docs.is_a?(Hash) + if @document_id_method + docs.each { |d| d.send(@document_id_method) } + end + request = post( + build_path(API_PATHS[:documents], index, url_pipe_join(schemas)), + JSON.generate(docs) + ) + send_request request + end + private def build_path(*path_elements) path_elements.flatten.unshift(PATH).join('/') @@ -245,8 +268,13 @@ def execute_request(method, url, headers, body, content_type=nil) [curl.response_code, curl.header_str, curl.body_str] end - def url_pipe_join(arr) - Rack::Utils.escape(Array(arr).flatten.join('|')) + def url_pipe_join(arr, default_value='*') + arr = Array(arr).flatten + if arr.empty? + default_value + else + Rack::Utils.escape(arr.join('|')) + end end end end diff --git a/spec/cloudquery_spec.rb b/spec/cloudquery_spec.rb index baff66c..4993b8a 100644 --- a/spec/cloudquery_spec.rb +++ b/spec/cloudquery_spec.rb @@ -81,6 +81,69 @@ response['result'].should be_an_instance_of(Array) response['result'].should have(3).items end + + describe "document management" do + before(:each) do + @client.add_indexes('spec_index') + @client.add_schema(File.open('spec/example_schema.xml')) + end + + it "adds a document to an index on the server" do + document = { + 'spec.example.name' => 'Steve Rogers', + 'spec.example.email' => ['steve.rogers@example.com','captain.america@marvel.com'], + 'spec.example.telephone' => ['555-555-5555','123-456-6789'], + 'spec.example.address' => ['Lower East Side, NY NY'], + 'spec.example.birthday' => ParseDate.parsedate('July 4, 1917'), + 'spec.example.note' => 'Captain America!', + } + response = @client.add_documents('spec_index', document, 'spec.example') + response['STATUS'].should be_between(200, 299) + response['result'].should have(1).item + end + + it "adds multiple documents to an index on the server" do + documents = [ + { + 'spec.example.name' => 'Steve Rogers', + 'spec.example.email' => ['steve.rogers@example.com','captain.america@marvel.com'], + 'spec.example.telephone' => ['555-555-5555', '123-456-6789'], + 'spec.example.address' => ['Lower East Side, NY NY'], + 'spec.example.birthday' => ParseDate.parsedate('July 4, 1917'), + 'spec.example.note' => 'Captain America!', + }, + { + 'spec.example.name' => 'Clark Kent', + 'spec.example.email' => ['clark.kent@example.com','superman@dc.com'], + 'spec.example.telephone' => ['555-123-1234', '555-456-6789'], + 'spec.example.address' => + ['344 Clinton St., Apt. #3B, Metropolis', 'The Fortess of Solitude, North Pole'], + 'spec.example.birthday' => ParseDate.parsedate('June 18, 1938'), + 'spec.example.note' => + 'Superhuman strength, speed, stamina, durability, senses, intelligence, regeneration, and longevity; super breath, heat vision, x-ray vision and flight. Member of the justice league.', + }, + { + 'spec.example.name' => 'Bruce Wayne', + 'spec.example.email' => ['bruce.wayne@example.com','batman@dc.com'], + 'spec.example.telephone' => ['555-123-6666', '555-456-6666'], + 'spec.example.address' => + ['1007 Mountain Drive, Gotham', 'The Batcave, Gotham'], + 'spec.example.birthday' => ParseDate.parsedate('February 19, 1939'), + 'spec.example.note' => + 'Sidekick is Robin. Has problems with the Joker. Member of the justice league.', + }, + ] + + response = @client.add_documents('spec_index', documents, 'spec.example') + response['STATUS'].should be_between(200, 299) + response['result'].should have(3).items + end + + after(:each) do + @client.delete_schema("spec.example") + @client.delete_indexes('spec_index') + end + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 126f2b6..49b16f4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,6 +3,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__)) $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'cloudquery' +require 'parsedate' Spec::Runner.configure do |config|