Permalink
Browse files

Implemented adding, getting, deleting of indexes

  • Loading branch information...
cee-dub committed May 2, 2009
1 parent 25439d9 commit 419fae9c97dba0bf4cf91e81ac9180fd71da44a7
Showing with 67 additions and 9 deletions.
  1. +25 −3 lib/cloudquery.rb
  2. +42 −6 spec/cloudquery_spec.rb
View
@@ -14,6 +14,7 @@ module Cloudquery
API_PATHS = {
:account => "account".freeze,
:schema => "schema".freeze,
:indexes => "i".freeze,
}.freeze
# standard Content-Types for requests
@@ -165,6 +166,20 @@ def get_schemas
send_request get(build_path(API_PATHS[:schema]))
end
def add_indexes(*indexes)
body = JSON.generate(indexes.flatten)
send_request post(build_path(API_PATHS[:indexes]), body)
end
def delete_indexes(*indexes)
indexes = url_pipe_join(indexes)
send_request delete(build_path(API_PATHS[:indexes], indexes))
end
def get_indexes
send_request get(build_path(API_PATHS[:indexes]))
end
private
def build_path(*path_elements)
path_elements.flatten.unshift(PATH).join('/')
@@ -200,8 +215,12 @@ def default_request_params
def send_request(request, content_type=nil)
response = execute_request(request.method, request.url, request.headers, request.body, content_type)
result = JSON.parse(response.last)
result.merge({'STATUS' => response.first})
begin
result = JSON.parse(response.last)
rescue JSON::ParserError => e
result = {"REASON" => response.last}
end
result.merge!({'STATUS' => response.first})
end
def execute_request(method, url, headers, body, content_type=nil)
@@ -218,14 +237,17 @@ def execute_request(method, url, headers, body, content_type=nil)
when 'DELETE'
curl.http_delete
when 'POST'
p curl.headers
curl.http_post(body)
when 'PUT'
curl.http_put(body)
end
[curl.response_code, curl.header_str, curl.body_str]
end
def url_pipe_join(arr)
Rack::Utils.escape(Array(arr).flatten.join('|'))
end
end
end
View
@@ -23,27 +23,63 @@
account.should have_key("preferences")
end
it "updates your account on the server" do
account = @client.get_account["result"]
response = @client.update_account(account)
response['STATUS'].should be_between(200, 299)
end
it "adds a schema to your account on the server" do
response = @client.add_schema(File.open('spec/example_schema.xml'))
response['STATUS'].should be_between(200, 299)
end
it "gets the schemas for your account from the server" do
response = @client.get_schemas
response['STATUS'].should be_between(200, 299)
response['result'].should be_an_instance_of(Array)
response['result'].should have_at_least(1).item
end
it "deletes a schema from your account on the server" do
response = @client.delete_schema("spec.example")
response['STATUS'].should be_between(200, 299)
end
it "gets the schemas for your accoutn from the server" do
response = @client.get_schemas
it "adds a single index to your account on the server" do
response = @client.add_indexes('spec_index')
response['STATUS'].should be_between(200, 299)
response['result'].should be_an_instance_of(Array)
response['result'].should have(1).item
end
it "adds multiple indexes to your account on the server" do
response = @client.add_indexes %w( spec_index_1 spec_index_2 spec_index_3 )
response['STATUS'].should be_between(200, 299)
response['result'].should be_an_instance_of(Array)
response['result'].should have(3).items
end
it "gets the indexes for your account from the server" do
response = @client.get_indexes
response['STATUS'].should be_between(200, 299)
response['result'].should be_an_instance_of(Array)
response['result'].should have_at_least(4).items
end
it "deletes a single index from your account on the server" do
response = @client.delete_indexes('spec_index')
response['STATUS'].should be_between(200, 299)
response['result'].should be_an_instance_of(Array)
response['result'].should have(1).item
end
it "deletes multiple indexes from your account on the server" do
response = @client.delete_indexes %w( spec_index_1 spec_index_2 spec_index_3 )
response['STATUS'].should be_between(200, 299)
response['result'].should be_an_instance_of(Array)
response['result'].should have(3).items
end
end
end

0 comments on commit 419fae9

Please sign in to comment.