ashikawa-core 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .DS_Store
2
+ .yardoc
3
+ doc
4
+ *.gem
5
+ .bundle
6
+ Gemfile.lock
7
+ pkg/*
8
+ report/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use @$(basename `pwd`) --create
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - rbx-19mode
6
+ - jruby-19mode
7
+ - ruby-head
8
+ - jruby-head
9
+ matrix:
10
+ allow_failures:
11
+ - rvm: ruby-head
12
+ - rvm: jruby-head
13
+ script: "rake ci"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ashikawa-core.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # Ashikawa Core
2
+
3
+ [![Build Status](https://secure.travis-ci.org/triAGENS/ashikawa-core.png?branch=master)](http://travis-ci.org/triAGENS/ashikawa-core)
4
+
5
+ Ashikawa Core is a Wrapper around the ArangoDB Rest API. It provides low level access and will be used in different ArangoDB ODMs.
6
+
7
+ ## How to use it
8
+
9
+ For a detailed description of Ashikawa::Core please refer to the [documentation](http://rdoc.info/github/triAGENS/ashikawa-core/master/frames). An example:
10
+
11
+ ```ruby
12
+ database = Ashikawa::Core::Database.new "http://localhost:8529"
13
+
14
+ database["my_collection"] # => Returns the collection my_collection – creates it, if it doesn't exist
15
+ database["my_collection"].name = "new_name"
16
+ database["new_name"].delete
17
+ ```
18
+
19
+ ## How to get started developing
20
+
21
+ Getting started is easy, just follow these steps.
22
+
23
+ ### In a nutshell
24
+
25
+ * Clone the project.
26
+ * `cd` into the folder and run `bundle`
27
+ * `rake` and see all tests passing (you need to have ArangoDB installed for that)
28
+ * Happy coding!
29
+
30
+ ### Detailed description
31
+
32
+ Make sure you are running Ruby 1.9.x (or JRuby/Rubinius in 1.9 mode) and clone the latest snapshot into a directory of your choice. Also make sure ArangoDB is installed and accessible via `arangod` (for example by installing it via `brew install arangodb`).
33
+
34
+ We encourage you to use [rvm](https://rvm.io/). If you do so, a gemset for the project is created upon changing into the directory. If you do not use `rvm` nothing special will happen in this case. Don't worry about it.
35
+
36
+ Change into the project directory. Run `bundle` to get all dependencies (do a `gem install bundler` before if you don't have bundler installed).
37
+
38
+ Now you can run `rake` to see all tests passing (hopefully). Happy coding!
39
+
40
+ You can also start up yard for documentation: `rake yard:server`
41
+
42
+ ### Continuous Integration
43
+
44
+ Our tests are run on Travis CI, the build status is displayed above. **Please note** that it only runs the unit tests and not the integration tests, because that would require ArangoDB to be installed on the Travis CI boxes. *Therefore green doesn't neccessarily mean green* (which is unfortunate).
45
+
46
+ ## Contributing
47
+
48
+ When you want to write code for the project, please follow these guidelines:
49
+
50
+ 1. Claim the ticket: Tell us that you want to work on a certain ticket, we will assign it to you (We don't want two people to work on the same thing ;) )
51
+ 2. Write an Integration Test: Describe what you want to do (our integration tests touch the database)
52
+ 3. Implement it: Write a unit test, check that it fails, make the test pass – repeat (our unit tests don't touch the database)
53
+ 4. Write Documentation for it: Check the compatibility with our rules via *yardstick*
54
+ 5. Check with `rake` that everything is fine and send the Pull Request :)
55
+
56
+ ## Documentation
57
+
58
+ We want `Ashikawa::Core` to be a solid foundation for all Ruby Libraries connecting to ArangoDB. Therefore we want an excellent documentation. We created two rake tasks:
59
+
60
+ * `rake yard:report`: Measure docs in lib/**/*.rb with yardstick
61
+ * `rake yard:verify`: Verify that yardstick coverage is 100%
62
+
63
+ The Yardstick coverage will be checked by our CI. Please make sure that the coverage is always at 100%.
data/Rakefile ADDED
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec"
4
+ require "rspec/core/rake_task"
5
+ # require 'yardstick/rake/measurement'
6
+ # require 'yardstick/rake/verify'
7
+
8
+
9
+ namespace :spec do
10
+ desc "Run the integration tests. Requires ArangoDB."
11
+ RSpec::Core::RakeTask.new(:integration) do |spec|
12
+ spec.pattern = "spec/integration/*_spec.rb"
13
+ end
14
+
15
+ desc "Run the unit tests"
16
+ RSpec::Core::RakeTask.new(:unit) do |spec|
17
+ spec.pattern = "spec/unit/*_spec.rb"
18
+ end
19
+
20
+ desc "Run all tests. Requires ArangoDB"
21
+ task :all => [:integration, :unit]
22
+ end
23
+
24
+ desc "check if gems are up to date"
25
+ task :dependencies do
26
+ dependency_status = `bundle outdated`
27
+ if dependency_status.include? "up to date"
28
+ puts "Dependencies up to date."
29
+ else
30
+ puts dependency_status
31
+ exit(1)
32
+ end
33
+ end
34
+
35
+ namespace :yard do
36
+ # Yardstick::Rake::Measurement.new(:report) do |measurement|
37
+ # measurement.output = 'report/measurement.txt'
38
+ # end
39
+
40
+ # Yardstick::Rake::Verify.new(:verify) do |verify|
41
+ # verify.threshold = 100
42
+ # end
43
+
44
+ desc "generate the documentation"
45
+ task :generate do
46
+ `yard`
47
+ end
48
+
49
+ desc "start the documentation server on port 8808"
50
+ task :server do
51
+ `yard server --reload`
52
+ end
53
+
54
+ desc "get statistics on the yard documentation"
55
+ task :stats do
56
+ `yard stats`
57
+ end
58
+ end
59
+
60
+ desc "Run Unit Tests and verify documentation - no ArangoDB required"
61
+ # task :ci => ["spec:unit", "yard:verify", "dependencies"]
62
+ task :ci => ["spec:unit", "dependencies"]
63
+
64
+ desc "Run all tests and verify documentation - ArangoDB required"
65
+ # task :default => ["spec:all", "yard:verify", "dependencies"]
66
+ task :default => ["spec:all", "dependencies"]
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ashikawa-core/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "ashikawa-core"
7
+ gem.version = Ashikawa::Core::VERSION
8
+ gem.authors = ["moonglum", "EinLama"]
9
+ gem.email = ["me@moonglum.net", "tobias.eilert@me.com"]
10
+ gem.homepage = ""
11
+ gem.summary = "Ashikawa Core is a Wrapper around the ArangoDB Rest API"
12
+ gem.description = "Ashikawa Core is a Wrapper around the ArangoDB Rest API. It provides low level access and will be used in different ArangoDB ODMs."
13
+
14
+ gem.required_ruby_version = '>= 1.9.2'
15
+ gem.requirements << "ArangoDB"
16
+
17
+ gem.rubyforge_project = "ashikawa-core"
18
+
19
+ gem.files = `git ls-files`.split("\n")
20
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ gem.require_paths = ["lib"]
23
+
24
+ # Runtime Dependencies
25
+ gem.add_dependency "rest-client", "~> 1.6.7"
26
+
27
+ # Runtime Dependencies (JRuby only)
28
+ if defined? PLATFORM and PLATFORM == 'java'
29
+ gem.add_dependency "json", "~> 1.6.6"
30
+ gem.add_dependency "jruby-openssl", "~> 0.7.6.1"
31
+ end
32
+
33
+ # Development Dependencies
34
+ gem.add_development_dependency "rake", "~> 0.9.2.2"
35
+ gem.add_development_dependency "rspec", "~> 2.11.0"
36
+ gem.add_development_dependency "yard", "~> 0.8.2.1"
37
+ gem.add_development_dependency "webmock", "~> 1.8.9"
38
+ # gem.add_development_dependency "yardstick", "~> 0.6.0"
39
+ gem.add_development_dependency "redcarpet", "~> 2.1.1"
40
+ end
@@ -0,0 +1,12 @@
1
+ require "ashikawa-core/collection"
2
+ require "ashikawa-core/connection"
3
+ require "ashikawa-core/database"
4
+ require "ashikawa-core/document"
5
+ require "ashikawa-core/version"
6
+
7
+ # Ashikawa is a project dedicated to connect Ruby and ArangoDB
8
+ module Ashikawa
9
+ # Ashikawa::Core is a wrapper around the ArangoDB REST interface
10
+ module Core
11
+ end
12
+ end
@@ -0,0 +1,524 @@
1
+ require "ashikawa-core/document"
2
+
3
+ module Ashikawa
4
+ module Core
5
+ # Represents a certain Collection within the Database
6
+ class Collection
7
+ # The name of the collection, must be unique
8
+ #
9
+ # @return [String]
10
+ # @api public
11
+ # @example Change the name of a collection
12
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
13
+ # raw_collection = {
14
+ # "name" => "example_1",
15
+ # "waitForSync" => true,
16
+ # "id" => 4588,
17
+ # "status" => 3,
18
+ # "error" => false,
19
+ # "code" => 200
20
+ # }
21
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
22
+ # collection.name # => "example_1"
23
+ # collection.name = "example_2"
24
+ # collection.name # => "example_2"
25
+ attr_reader :name
26
+
27
+ # The ID of the collection. Is set by the database and unique
28
+ #
29
+ # @return [Fixnum]
30
+ # @api public
31
+ # @example Get the id of the collection
32
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
33
+ # raw_collection = {
34
+ # "name" => "example_1",
35
+ # "waitForSync" => true,
36
+ # "id" => 4588,
37
+ # "status" => 3,
38
+ # "error" => false,
39
+ # "code" => 200
40
+ # }
41
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
42
+ # collection.id #=> 4588
43
+ attr_reader :id
44
+
45
+ # Create a new Collection object with a name and an optional ID
46
+ #
47
+ # @param [Database] database The database the connection belongs to
48
+ # @param [Hash] raw_collection The raw collection returned from the server
49
+ # @api public
50
+ # @example Create a Collection object from scratch
51
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
52
+ # raw_collection = {
53
+ # "name" => "example_1",
54
+ # "waitForSync" => true,
55
+ # "id" => 4588,
56
+ # "status" => 3,
57
+ # "error" => false,
58
+ # "code" => 200
59
+ # }
60
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
61
+ def initialize(database, raw_collection)
62
+ @database = database
63
+ @name = raw_collection['name'] if raw_collection.has_key? 'name'
64
+ @id = raw_collection['id'].to_i if raw_collection.has_key? 'id'
65
+ @status = raw_collection['status'].to_i if raw_collection.has_key? 'status'
66
+ end
67
+
68
+ # Change the name of the collection
69
+ #
70
+ # @param [String] new_name New Name
71
+ # @return [String] New Name
72
+ # @api public
73
+ # @example Change the name of a collection
74
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
75
+ # raw_collection = {
76
+ # "name" => "example_1",
77
+ # "waitForSync" => true,
78
+ # "id" => 4588,
79
+ # "status" => 3,
80
+ # "error" => false,
81
+ # "code" => 200
82
+ # }
83
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
84
+ # collection.name # => "example_1"
85
+ # collection.name = "example_2"
86
+ # collection.name # => "example_2"
87
+ def name=(new_name)
88
+ send_request_for_this_collection "/rename", put: { "name" => new_name }
89
+ @name = new_name
90
+ end
91
+
92
+ # Checks if the collection is new born
93
+ #
94
+ # @return [Boolean]
95
+ # @api public
96
+ # @example Is the collection new born?
97
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
98
+ # raw_collection = {
99
+ # "name" => "example_1",
100
+ # "waitForSync" => true,
101
+ # "id" => 4588,
102
+ # "status" => 3,
103
+ # "error" => false,
104
+ # "code" => 200
105
+ # }
106
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
107
+ # collection.new_born? #=> false
108
+ def new_born?
109
+ @status == 1
110
+ end
111
+
112
+ # Checks if the collection is unloaded
113
+ #
114
+ # @return [Boolean]
115
+ # @api public
116
+ # @example Is the collection unloaded?
117
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
118
+ # raw_collection = {
119
+ # "name" => "example_1",
120
+ # "waitForSync" => true,
121
+ # "id" => 4588,
122
+ # "status" => 3,
123
+ # "error" => false,
124
+ # "code" => 200
125
+ # }
126
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
127
+ # collection.unloaded? #=> false
128
+ def unloaded?
129
+ @status == 2
130
+ end
131
+
132
+ # Checks if the collection is loaded
133
+ #
134
+ # @return [Boolean]
135
+ # @api public
136
+ # @example Is the collection loaded?
137
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
138
+ # raw_collection = {
139
+ # "name" => "example_1",
140
+ # "waitForSync" => true,
141
+ # "id" => 4588,
142
+ # "status" => 3,
143
+ # "error" => false,
144
+ # "code" => 200
145
+ # }
146
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
147
+ # collection.loaded? #=> true
148
+ def loaded?
149
+ @status == 3
150
+ end
151
+
152
+ # Checks if the collection is in the process of being unloaded
153
+ #
154
+ # @return [Boolean]
155
+ # @api public
156
+ # @example Is the collection unloaded?
157
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
158
+ # raw_collection = {
159
+ # "name" => "example_1",
160
+ # "waitForSync" => true,
161
+ # "id" => 4588,
162
+ # "status" => 3,
163
+ # "error" => false,
164
+ # "code" => 200
165
+ # }
166
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
167
+ # collection.being_unloaded? #=> false
168
+ def being_unloaded?
169
+ @status == 4
170
+ end
171
+
172
+ # Checks if the collection is corrupted
173
+ #
174
+ # @return [Boolean]
175
+ # @api public
176
+ # @example Is the collection corrupted?
177
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
178
+ # raw_collection = {
179
+ # "name" => "example_1",
180
+ # "waitForSync" => true,
181
+ # "id" => 4588,
182
+ # "status" => 3,
183
+ # "error" => false,
184
+ # "code" => 200
185
+ # }
186
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
187
+ # collection.corrupted? #=> false
188
+ def corrupted?
189
+ @status > 5
190
+ end
191
+
192
+ # Does the document wait until the data has been synchronised to disk?
193
+ #
194
+ # @return [Boolean]
195
+ # @api public
196
+ # @example Does the collection wait for file synchronization?
197
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
198
+ # raw_collection = {
199
+ # "name" => "example_1",
200
+ # "waitForSync" => true,
201
+ # "id" => 4588,
202
+ # "status" => 3,
203
+ # "error" => false,
204
+ # "code" => 200
205
+ # }
206
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
207
+ # collection.wait_for_sync? #=> false
208
+ def wait_for_sync?
209
+ server_response = send_request_for_this_collection "/properties"
210
+ server_response["waitForSync"]
211
+ end
212
+
213
+ # Change if the document will wait until the data has been synchronised to disk
214
+ #
215
+ # @return [String] Response from the server
216
+ # @api public
217
+ # @example Tell the collection to wait for file synchronization
218
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
219
+ # raw_collection = {
220
+ # "name" => "example_1",
221
+ # "waitForSync" => true,
222
+ # "id" => 4588,
223
+ # "status" => 3,
224
+ # "error" => false,
225
+ # "code" => 200
226
+ # }
227
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
228
+ # collection.wait_for_sync = true
229
+ def wait_for_sync=(new_value)
230
+ server_response = send_request_for_this_collection "/properties", put: { "waitForSync" => new_value }
231
+ end
232
+
233
+ # Returns the number of documents in the collection
234
+ #
235
+ # @return [Fixnum] Number of documents
236
+ # @api public
237
+ # @example How many documents are in the collection?
238
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
239
+ # raw_collection = {
240
+ # "name" => "example_1",
241
+ # "waitForSync" => true,
242
+ # "id" => 4588,
243
+ # "status" => 3,
244
+ # "error" => false,
245
+ # "code" => 200
246
+ # }
247
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
248
+ # collection.length # => 0
249
+ def length
250
+ server_response = send_request_for_this_collection "/count"
251
+ server_response["count"]
252
+ end
253
+
254
+ # Return a figure for the collection
255
+ #
256
+ # @param [Symbol] figure_type The figure you want to know:
257
+ # * :datafiles_count - the number of active datafiles
258
+ # * :alive_size - the total size in bytes used by all living documents
259
+ # * :alive_count - the number of living documents
260
+ # * :dead_size - the total size in bytes used by all dead documents
261
+ # * :dead_count - the number of dead documents
262
+ # @return [Fixnum] The figure you requested
263
+ # @api public
264
+ # @example Get the datafile count for a collection
265
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
266
+ # raw_collection = {
267
+ # "name" => "example_1",
268
+ # "waitForSync" => true,
269
+ # "id" => 4588,
270
+ # "status" => 3,
271
+ # "error" => false,
272
+ # "code" => 200
273
+ # }
274
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
275
+ # collection.figure :datafiles_count #=> 0
276
+ def figure(figure_type)
277
+ server_response = send_request_for_this_collection "/figures"
278
+
279
+ figure_area, figure_name = figure_type.to_s.split "_"
280
+ server_response["figures"][figure_area][figure_name]
281
+ end
282
+
283
+ # Deletes the collection
284
+ #
285
+ # @return [String] Response from the server
286
+ # @api public
287
+ # @example Delete a collection
288
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
289
+ # raw_collection = {
290
+ # "name" => "example_1",
291
+ # "waitForSync" => true,
292
+ # "id" => 4588,
293
+ # "status" => 3,
294
+ # "error" => false,
295
+ # "code" => 200
296
+ # }
297
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
298
+ # collection.delete
299
+ def delete
300
+ send_request_for_this_collection "", delete: {}
301
+ end
302
+
303
+ # Load the collection into memory
304
+ #
305
+ # @return [String] Response from the server
306
+ # @api public
307
+ # @example Load a collection into memory
308
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
309
+ # raw_collection = {
310
+ # "name" => "example_1",
311
+ # "waitForSync" => true,
312
+ # "id" => 4588,
313
+ # "status" => 3,
314
+ # "error" => false,
315
+ # "code" => 200
316
+ # }
317
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
318
+ # collection.load
319
+ def load
320
+ send_request_for_this_collection "/load", put: {}
321
+ end
322
+
323
+ # Load the collection into memory
324
+ #
325
+ # @return [String] Response from the server
326
+ # @api public
327
+ # @example Unload a collection into memory
328
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
329
+ # raw_collection = {
330
+ # "name" => "example_1",
331
+ # "waitForSync" => true,
332
+ # "id" => 4588,
333
+ # "status" => 3,
334
+ # "error" => false,
335
+ # "code" => 200
336
+ # }
337
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
338
+ # collection.unload
339
+ def unload
340
+ send_request_for_this_collection "/unload", put: {}
341
+ end
342
+
343
+ # Delete all documents from the collection
344
+ #
345
+ # @return [String] Response from the server
346
+ # @api public
347
+ # @example Remove all documents from a collection
348
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
349
+ # raw_collection = {
350
+ # "name" => "example_1",
351
+ # "waitForSync" => true,
352
+ # "id" => 4588,
353
+ # "status" => 3,
354
+ # "error" => false,
355
+ # "code" => 200
356
+ # }
357
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
358
+ # collection.truncate!
359
+ def truncate!
360
+ send_request_for_this_collection "/truncate", put: {}
361
+ end
362
+
363
+ # Retrieves all documents for this collection
364
+ #
365
+ # @note It is advised to NOT use this method due to possible HUGE data amounts requested
366
+ # @param [Hash] options Additional options for this query.
367
+ # @option options [Integer] :limit limit the maximum number of queried and returned elements.
368
+ # @option options [Integer] :skip skip the first <n> documents of the query.
369
+ # @return [Array<Document>]
370
+ # @api public
371
+ # @example Get an array with all documents
372
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
373
+ # raw_collection = {
374
+ # "name" => "example_1",
375
+ # "waitForSync" => true,
376
+ # "id" => 4588,
377
+ # "status" => 3,
378
+ # "error" => false,
379
+ # "code" => 200
380
+ # }
381
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
382
+ # collection.all # => [#<Document id=43>]
383
+ def all(options={})
384
+ request_data = { "collection" => @name }
385
+
386
+ request_data["limit"] = options[:limit] if options.has_key? :limit
387
+ request_data["skip"] = options[:skip] if options.has_key? :skip
388
+
389
+ server_response = @database.send_request "/simple/all", :put => request_data
390
+
391
+ documents_from_response(server_response)
392
+ end
393
+
394
+ # Looks for documents in the collection which match the given criteria
395
+ #
396
+ # @param [Hash] reference_data a Hash with data similar to the documents you are looking for.
397
+ # @param [Hash] options Additional options for this query.
398
+ # @option options [Integer] :limit limit the maximum number of queried and returned elements.
399
+ # @option options [Integer] :skip skip the first <n> documents of the query.
400
+ # @return [Array<Document>]
401
+ # @api public
402
+ # @example Find all documents in the collection that are red
403
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
404
+ # raw_collection = {
405
+ # "name" => "example_1",
406
+ # "waitForSync" => true,
407
+ # "id" => 4588,
408
+ # "status" => 3,
409
+ # "error" => false,
410
+ # "code" => 200
411
+ # }
412
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
413
+ # collection.by_example { "color" => "red"} # => [#<Document id=2444 color="red">]
414
+ def by_example(reference_data, options={})
415
+ request_data = { "collection" => @name, "example" => reference_data }
416
+
417
+ request_data["limit"] = options[:limit] if options.has_key? :limit
418
+ request_data["skip"] = options[:skip] if options.has_key? :skip
419
+
420
+ server_response = @database.send_request "/simple/by-example", :put => request_data
421
+
422
+ documents_from_response(server_response)
423
+ end
424
+
425
+ # Looks for documents in the collection based on location
426
+ #
427
+ # @param [Hash] options Options for this search.
428
+ # @option options [Integer] :latitude Latitude location for your search.
429
+ # @option options [Integer] :longitude Longitude location for your search.
430
+ # @return [Array<Document>]
431
+ # @api public
432
+ # @example Find all documents at Infinite Loop
433
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
434
+ # raw_collection = {
435
+ # "name" => "example_1",
436
+ # "waitForSync" => true,
437
+ # "id" => 4588,
438
+ # "status" => 3,
439
+ # "error" => false,
440
+ # "code" => 200
441
+ # }
442
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
443
+ # collection.near latitude: 37.331693, longitude: -122.030468
444
+ def near(options={})
445
+ request_data = { "collection" => @name }
446
+
447
+ request_data["latitude"] = options[:latitude] if options.has_key? :latitude
448
+ request_data["longitude"] = options[:longitude] if options.has_key? :longitude
449
+
450
+ server_response = @database.send_request "/simple/near", :put => request_data
451
+
452
+ documents_from_response(server_response)
453
+ end
454
+
455
+ # Looks for documents in the collection within a certain radius
456
+ #
457
+ # @param [Hash] options Options for this search.
458
+ # @option options [Integer] :latitude Latitude location for your search.
459
+ # @option options [Integer] :longitude Longitude location for your search.
460
+ # @option options [Integer] :radius Radius around the given location you want to search in.
461
+ # @return [Array<Document>]
462
+ # @api public
463
+ # @example Find all documents within a radius of 100 to Infinite Loop
464
+ # database = Ashikawa::Core::Database.new "http://localhost:8529"
465
+ # raw_collection = {
466
+ # "name" => "example_1",
467
+ # "waitForSync" => true,
468
+ # "id" => 4588,
469
+ # "status" => 3,
470
+ # "error" => false,
471
+ # "code" => 200
472
+ # }
473
+ # collection = Ashikawa::Core::Collection.new database, raw_collection
474
+ # collection.within latitude: 37.331693, longitude: -122.030468, radius: 100
475
+ def within(options={})
476
+ request_data = { "collection" => @name }
477
+
478
+ request_data["latitude"] = options[:latitude] if options.has_key? :latitude
479
+ request_data["longitude"] = options[:longitude] if options.has_key? :longitude
480
+ request_data["radius"] = options[:radius] if options.has_key? :radius
481
+
482
+ server_response = @database.send_request "/simple/within", :put => request_data
483
+
484
+ documents_from_response(server_response)
485
+ end
486
+
487
+ # Fetch a certain document by its ID
488
+ #
489
+ # @param [Integer] document_id the id of the document
490
+ # @return Document
491
+ # @api public
492
+ # @example Fetch a document with the ID 12345
493
+ # document = collection[12345]
494
+ def [](document_id)
495
+ Ashikawa::Core::Document.new "#{@id}/#{document_id}"
496
+ end
497
+
498
+ private
499
+
500
+ # Send a request to the server with the name of the collection prepended
501
+ #
502
+ # @return [String] Response from the server
503
+ # @api private
504
+ def send_request_for_this_collection(path, method={})
505
+ if method == {}
506
+ @database.send_request "/collection/#{id}#{path}"
507
+ else
508
+ @database.send_request "/collection/#{id}#{path}", method
509
+ end
510
+ end
511
+
512
+ # Takes JSON returned by the database and collects Documents from the data
513
+ #
514
+ # @param [Array<Hash>] parsed_server_response parsed JSON response from the server. Should contain document-hashes.
515
+ # @return [Array<Document>]
516
+ # @api private
517
+ def documents_from_response(parsed_server_response)
518
+ parsed_server_response.collect do |document|
519
+ Ashikawa::Core::Document.new(document["_id"], document["_rev"])
520
+ end
521
+ end
522
+ end
523
+ end
524
+ end