sinatra_resource 0.3.4 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.4
1
+ 0.3.5
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../../helpers/resource_test_helper')
2
2
 
3
- class SourcesGetManyResourceTest < ResourceTestCase
3
+ class SourcesGetManySearchResourceTest < ResourceTestCase
4
4
 
5
5
  include DataCatalog
6
6
 
@@ -0,0 +1,87 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../helpers/resource_test_helper')
2
+
3
+ class SourcesUsagesGetManySearchResourceTest < ResourceTestCase
4
+
5
+ include DataCatalog
6
+
7
+ def app; Sources end
8
+
9
+ SEARCH_FILTERS = [
10
+ { :filter => "description=academic" },
11
+ { :filter => "description:cad" },
12
+ ]
13
+
14
+ before do
15
+ @sources = 3.times.map do |i|
16
+ create_source(:title => "Source #{i}")
17
+ end
18
+ @sources.each do |source|
19
+ 4.times do |i|
20
+ source.usages << new_usage(
21
+ :title => "Usage #{i}",
22
+ :description => (i % 2 == 0) ? "academic" : "press"
23
+ )
24
+ end
25
+ source.save!
26
+ end
27
+ @source = @sources[1]
28
+ @search = SEARCH_FILTERS[0]
29
+ end
30
+
31
+ after do
32
+ @sources.each { |x| x.destroy }
33
+ end
34
+
35
+ context "get /:id/usages" do
36
+ context "anonymous" do
37
+ before do
38
+ get "/#{@source.id}/usages", @search
39
+ end
40
+
41
+ use "return 401 because the API key is missing"
42
+ end
43
+
44
+ context "incorrect API key" do
45
+ before do
46
+ get "/#{@source.id}/usages", @search.merge(
47
+ :api_key => BAD_API_KEY)
48
+ end
49
+
50
+ use "return 401 because the API key is invalid"
51
+ end
52
+ end
53
+
54
+ %w(basic curator admin).each do |role|
55
+ context "#{role} : get /:fake_id/usages" do
56
+ before do
57
+ get "/#{FAKE_ID}/usages", @search.merge(
58
+ :api_key => api_key_for(role))
59
+ end
60
+
61
+ use "return 404 Not Found with empty response body"
62
+ end
63
+
64
+ SEARCH_FILTERS.each do |search|
65
+ context "#{role} : get /:id/usages" do
66
+ before do
67
+ get "/#{@source.id}/usages", search.merge(
68
+ :api_key => api_key_for(role))
69
+ end
70
+
71
+ use "return 200 Ok"
72
+
73
+ test "body should have 2 sources" do
74
+ assert_equal 2, parsed_response_body.length
75
+ end
76
+
77
+ test "body should have correct source titles" do
78
+ actual = parsed_response_body.map { |e| e["title"] }
79
+ assert_equal ["Usage 0", "Usage 2"], actual.sort
80
+ end
81
+
82
+ docs_properties %w(title url description id)
83
+ end
84
+ end
85
+ end
86
+
87
+ end
@@ -232,7 +232,7 @@ module SinatraResource
232
232
  if params.empty?
233
233
  children
234
234
  else
235
- children.all(make_conditions(params, child_model))
235
+ select_by(children, make_conditions(params, child_model))
236
236
  end
237
237
  else
238
238
  children = if params.empty?
@@ -352,6 +352,37 @@ module SinatraResource
352
352
  conditions # TODO: incomplete
353
353
  end
354
354
 
355
+ # Select +children+ that match +conditions+.
356
+ #
357
+ # This method is needed because MongoMapper does not have +all+
358
+ # defined on the proxy for an embedded document many association.
359
+ #
360
+ # It does not currently support conditions such as the following:
361
+ # :value => { '$gte' => 3 }
362
+ # :value => { '$in' => [24, 36, 48] }
363
+ #
364
+ # @params [<MongoMapper::EmbeddedDocument>] children
365
+ #
366
+ # @params [Hash] conditions
367
+ #
368
+ # @return [<MongoMapper::EmbeddedDocument>]
369
+ def select_by(children, conditions)
370
+ children.select do |child|
371
+ match = true
372
+ conditions.each_pair do |key, value|
373
+ match &&= case value
374
+ when String, Integer
375
+ child[key] == value
376
+ when Regexp
377
+ child[key] =~ value
378
+ else raise Error, "embedded document search does not " +
379
+ "support: #{value.inspect}"
380
+ end
381
+ end
382
+ match
383
+ end
384
+ end
385
+
355
386
  # Select only the +children+ that are related to the +parent+ by
356
387
  # way of the +association+.
357
388
  #
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sinatra_resource}
8
- s.version = "0.3.4"
8
+ s.version = "0.3.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David James"]
@@ -86,6 +86,7 @@ Gem::Specification.new do |s|
86
86
  "examples/datacatalog/test/resources/sources_usages/sources_usages_delete_test.rb",
87
87
  "examples/datacatalog/test/resources/sources_usages/sources_usages_get_many_test.rb",
88
88
  "examples/datacatalog/test/resources/sources_usages/sources_usages_get_one_test.rb",
89
+ "examples/datacatalog/test/resources/sources_usages/sources_usages_get_search_test.rb",
89
90
  "examples/datacatalog/test/resources/sources_usages/sources_usages_post_test.rb",
90
91
  "examples/datacatalog/test/resources/sources_usages/sources_usages_put_test.rb",
91
92
  "examples/datacatalog/test/resources/users/users_delete_test.rb",
@@ -180,6 +181,7 @@ Gem::Specification.new do |s|
180
181
  "examples/datacatalog/test/resources/sources_usages/sources_usages_delete_test.rb",
181
182
  "examples/datacatalog/test/resources/sources_usages/sources_usages_get_many_test.rb",
182
183
  "examples/datacatalog/test/resources/sources_usages/sources_usages_get_one_test.rb",
184
+ "examples/datacatalog/test/resources/sources_usages/sources_usages_get_search_test.rb",
183
185
  "examples/datacatalog/test/resources/sources_usages/sources_usages_post_test.rb",
184
186
  "examples/datacatalog/test/resources/sources_usages/sources_usages_put_test.rb",
185
187
  "examples/datacatalog/test/resources/users/users_delete_test.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - David James
@@ -181,6 +181,7 @@ files:
181
181
  - examples/datacatalog/test/resources/sources_usages/sources_usages_delete_test.rb
182
182
  - examples/datacatalog/test/resources/sources_usages/sources_usages_get_many_test.rb
183
183
  - examples/datacatalog/test/resources/sources_usages/sources_usages_get_one_test.rb
184
+ - examples/datacatalog/test/resources/sources_usages/sources_usages_get_search_test.rb
184
185
  - examples/datacatalog/test/resources/sources_usages/sources_usages_post_test.rb
185
186
  - examples/datacatalog/test/resources/sources_usages/sources_usages_put_test.rb
186
187
  - examples/datacatalog/test/resources/users/users_delete_test.rb
@@ -297,6 +298,7 @@ test_files:
297
298
  - examples/datacatalog/test/resources/sources_usages/sources_usages_delete_test.rb
298
299
  - examples/datacatalog/test/resources/sources_usages/sources_usages_get_many_test.rb
299
300
  - examples/datacatalog/test/resources/sources_usages/sources_usages_get_one_test.rb
301
+ - examples/datacatalog/test/resources/sources_usages/sources_usages_get_search_test.rb
300
302
  - examples/datacatalog/test/resources/sources_usages/sources_usages_post_test.rb
301
303
  - examples/datacatalog/test/resources/sources_usages/sources_usages_put_test.rb
302
304
  - examples/datacatalog/test/resources/users/users_delete_test.rb