Skip to content

Commit

Permalink
Change create_index prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
curquiza committed Jun 10, 2020
1 parent 493e7f8 commit 9237775
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ You can check out [the API documentation](https://docs.meilisearch.com/reference
# Create an index
client.create_index('books')
# Create an index and give the primary-key
client.create_index(uid: 'books', primaryKey: 'book_id')
client.create_index('books', primaryKey: 'book_id')
```

#### List all indexes <!-- omit in toc -->
Expand Down
23 changes: 8 additions & 15 deletions lib/meilisearch/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@ def show_index(index_uid)
end

# Usage:
# create_index('indexUID')
# create_index(uid: 'indexUID')
# create_index(uid: 'indexUID', primaryKey: 'id')
def create_index(attributes)
body = if attributes.is_a?(Hash)
attributes
else
{ uid: attributes }
end
# client.create_index('indexUID')
# client.create_index('indexUID', primaryKey: 'id')
def create_index(index_uid, options = {})
body = { uid: index_uid }.merge(options)
res = http_post '/indexes', body
index_object(res['uid'])
end
Expand All @@ -33,13 +28,11 @@ def delete_index(index_uid)
end

# Usage:
# index('indexUID')
# index(uid: 'indexUID')
def index(attribute)
uid = attribute.is_a?(Hash) ? attribute[:uid] : attribute
raise IndexUidError if uid.nil?
# client.index('indexUID')
def index(index_uid)
raise IndexUidError if index_uid.nil?

index_object(uid)
index_object(index_uid)
end
alias get_index index

Expand Down
26 changes: 8 additions & 18 deletions spec/meilisearch/client/indexes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
clear_all_indexes(@client)
@uid1 = 'uid1'
@uid2 = 'uid2'
@uid3 = 'uid3'
@primary_key = 'objectId'
end

Expand All @@ -17,17 +16,10 @@
expect(index.primary_key).to be_nil
end

it 'creates an index without primary-key as an Hash' do
index = @client.create_index(uid: @uid2)
expect(index).to be_a(MeiliSearch::Index)
expect(index.uid).to eq(@uid2)
expect(index.primary_key).to be_nil
end

it 'creates an index with primary-key' do
index = @client.create_index(uid: @uid3, primaryKey: @primary_key)
index = @client.create_index(@uid2, primaryKey: @primary_key)
expect(index).to be_a(MeiliSearch::Index)
expect(index.uid).to eq(@uid3)
expect(index.uid).to eq(@uid2)
expect(index.primary_key).to eq(@primary_key)
end

Expand All @@ -46,22 +38,22 @@
it 'gets list of indexes' do
response = @client.indexes
expect(response).to be_a(Array)
expect(response.count).to eq(3)
expect(response.count).to eq(2)
uids = response.map { |elem| elem['uid'] }
expect(uids).to contain_exactly(@uid1, @uid2, @uid3)
expect(uids).to contain_exactly(@uid1, @uid2)
end

it 'shows a specific index' do
response = @client.show_index(@uid3)
response = @client.show_index(@uid2)
expect(response).to be_a(Hash)
expect(response['uid']).to eq(@uid3)
expect(response['uid']).to eq(@uid2)
expect(response['primaryKey']).to eq(@primary_key)
end

it 'returns an index object based on uid' do
index = @client.index(@uid3)
index = @client.index(@uid2)
expect(index).to be_a(MeiliSearch::Index)
expect(index.uid).to eq(@uid3)
expect(index.uid).to eq(@uid2)
expect(index.primary_key).to eq(@primary_key)
end

Expand All @@ -70,8 +62,6 @@
expect { @client.show_index(@uid1) }.to raise_meilisearch_http_error_with(404)
expect(@client.delete_index(@uid2)).to be_nil
expect { @client.show_index(@uid2) }.to raise_meilisearch_http_error_with(404)
expect(@client.delete_index(@uid3)).to be_nil
expect { @client.show_index(@uid3) }.to raise_meilisearch_http_error_with(404)
expect(@client.indexes.count).to eq(0)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/meilisearch/index/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
client = MeiliSearch::Client.new($URL, $MASTER_KEY)
clear_all_indexes(client)
@index1 = client.create_index(@uid1)
@index2 = client.create_index(uid: @uid2, primaryKey: @primary_key)
@index2 = client.create_index(@uid2, primaryKey: @primary_key)
end

it 'shows the index' do
Expand Down
2 changes: 1 addition & 1 deletion spec/meilisearch/index/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@
context 'Index with primary-key' do
before(:all) do
@uid = SecureRandom.hex(4)
@client.create_index(uid: @uid, primaryKey: 'id')
@client.create_index(@uid, primaryKey: 'id')
end

after(:all) { clear_all_indexes(@client) }
Expand Down
2 changes: 1 addition & 1 deletion spec/meilisearch/index/updates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
]
client = MeiliSearch::Client.new($URL, $MASTER_KEY)
clear_all_indexes(client)
@index = client.create_index(uid: 'books', primaryKey: 'objectId')
@index = client.create_index('books', primaryKey: 'objectId')
end

after(:all) do
Expand Down

0 comments on commit 9237775

Please sign in to comment.