Skip to content

Commit

Permalink
Implemented #add_documents
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-dub committed May 2, 2009
1 parent 2caf21a commit 94eb836
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
34 changes: 31 additions & 3 deletions lib/cloudquery.rb
Expand Up @@ -15,6 +15,7 @@ module Cloudquery
:account => "account".freeze,
:schema => "schema".freeze,
:indexes => "i".freeze,
:documents => "i".freeze,
}.freeze

# standard Content-Types for requests
Expand Down Expand Up @@ -129,12 +130,16 @@ def initialize(options={})
# unless options[:account] && options[:secret]
# raise "Client requires :account => <account name> and :secret => <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))
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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('/')
Expand Down Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions spec/cloudquery_spec.rb
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -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|

Expand Down

0 comments on commit 94eb836

Please sign in to comment.