supplejack_client 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +27 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +16 -0
  5. data/Guardfile +24 -0
  6. data/LICENSE.txt +674 -0
  7. data/README.md +153 -0
  8. data/Rakefile +9 -0
  9. data/lib/generators/locales/en.yml +11 -0
  10. data/lib/generators/supplejack/install_generator.rb +28 -0
  11. data/lib/generators/templates/README +19 -0
  12. data/lib/generators/templates/supplejack_client.rb +120 -0
  13. data/lib/supplejack/config.rb +116 -0
  14. data/lib/supplejack/controllers/helpers.rb +172 -0
  15. data/lib/supplejack/engine.rb +20 -0
  16. data/lib/supplejack/exceptions.rb +17 -0
  17. data/lib/supplejack/facet.rb +33 -0
  18. data/lib/supplejack/item.rb +94 -0
  19. data/lib/supplejack/item_relation.rb +73 -0
  20. data/lib/supplejack/log_subscriber.rb +58 -0
  21. data/lib/supplejack/paginated_collection.rb +61 -0
  22. data/lib/supplejack/record.rb +147 -0
  23. data/lib/supplejack/request.rb +95 -0
  24. data/lib/supplejack/search.rb +346 -0
  25. data/lib/supplejack/url_formats/item_hash.rb +208 -0
  26. data/lib/supplejack/user.rb +132 -0
  27. data/lib/supplejack/user_set.rb +349 -0
  28. data/lib/supplejack/user_set_relation.rb +143 -0
  29. data/lib/supplejack/util.rb +120 -0
  30. data/lib/supplejack/version.rb +10 -0
  31. data/lib/supplejack_client.rb +29 -0
  32. data/spec/spec_helper.rb +23 -0
  33. data/spec/supplejack/controllers/helpers_spec.rb +277 -0
  34. data/spec/supplejack/facet_spec.rb +44 -0
  35. data/spec/supplejack/item_relation_spec.rb +111 -0
  36. data/spec/supplejack/item_spec.rb +115 -0
  37. data/spec/supplejack/log_subscriber_spec.rb +40 -0
  38. data/spec/supplejack/paginated_collection_spec.rb +43 -0
  39. data/spec/supplejack/record_spec.rb +255 -0
  40. data/spec/supplejack/request_spec.rb +195 -0
  41. data/spec/supplejack/search_spec.rb +727 -0
  42. data/spec/supplejack/url_formats/item_hash_spec.rb +341 -0
  43. data/spec/supplejack/user_set_relation_spec.rb +149 -0
  44. data/spec/supplejack/user_set_spec.rb +465 -0
  45. data/spec/supplejack/user_spec.rb +159 -0
  46. data/supplejack_client.gemspec +30 -0
  47. metadata +159 -0
@@ -0,0 +1,44 @@
1
+ # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
2
+ # and is licensed under the GNU General Public License, version 3.
3
+ # See https://github.com/DigitalNZ/supplejack_client for details.
4
+ #
5
+ # Supplejack was created by DigitalNZ at the National Library of NZ
6
+ # and the Department of Internal Affairs. http://digitalnz.org/supplejack
7
+
8
+ require 'spec_helper'
9
+
10
+ module Supplejack
11
+ describe Facet do
12
+
13
+ it 'initializes the facet with a name' do
14
+ facet = Supplejack::Facet.new('description', {})
15
+ facet.name.should eq('description')
16
+ end
17
+
18
+ it 'initializes the facet with values' do
19
+ facet = Supplejack::Facet.new('location', {'Wellington' => 100, 'Auckland' => 10})
20
+ facet.values.should eq({'Wellington' => 100, 'Auckland' => 10})
21
+ end
22
+
23
+ context 'sorting' do
24
+ let(:facet) { Supplejack::Facet.new('location', {'Wellington' => 100, 'Auckland' => 10, 'Dunedin' => 5, 'Queenstown' => 30}) }
25
+
26
+ it 'sorts by alphabetical order set in config' do
27
+ Supplejack.stub(:facets_sort) { :index }
28
+ facet.values.keys.should eq ['Auckland', 'Dunedin', 'Queenstown', 'Wellington']
29
+ facet.values.values.should eq [10, 5, 30, 100]
30
+ end
31
+
32
+ it 'sorts by count set in the config' do
33
+ Supplejack.stub(:facets_sort) { :count }
34
+ facet.values.keys.should eq ['Wellington', 'Queenstown', 'Auckland', 'Dunedin']
35
+ facet.values.values.should eq [100, 30, 10, 5]
36
+ end
37
+
38
+ it 'overrides the global facets sort' do
39
+ Supplejack.stub(:facets_sort) { :count }
40
+ facet.values(:index).keys.should eq ['Auckland', 'Dunedin', 'Queenstown', 'Wellington']
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,111 @@
1
+ # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
2
+ # and is licensed under the GNU General Public License, version 3.
3
+ # See https://github.com/DigitalNZ/supplejack_client for details.
4
+ #
5
+ # Supplejack was created by DigitalNZ at the National Library of NZ
6
+ # and the Department of Internal Affairs. http://digitalnz.org/supplejack
7
+
8
+ require 'spec_helper'
9
+
10
+ module Supplejack
11
+ describe ItemRelation do
12
+ let(:supplejack_set) { Supplejack::UserSet.new(id: '1234567890', records: [{record_id: 1, position: 1}]) }
13
+ let(:relation) { Supplejack::ItemRelation.new(supplejack_set) }
14
+
15
+ describe '#initialize' do
16
+ it 'assigns the user_set object as @user_set' do
17
+ Supplejack::ItemRelation.new(supplejack_set).user_set.should eq supplejack_set
18
+ end
19
+
20
+ it 'initializes an array of Supplejack::Items' do
21
+ Supplejack::ItemRelation.new(supplejack_set).items.should be_a Array
22
+ end
23
+
24
+ it 'returns an empty array of items when the user_set attributes records are nil' do
25
+ supplejack_set.stub(:attributes) { {} }
26
+ Supplejack::ItemRelation.new(supplejack_set).items.should be_empty
27
+ end
28
+
29
+ it 'adds the user_set_id to the Supplejack::Item object' do
30
+ Supplejack::ItemRelation.new(supplejack_set).items.first.user_set_id.should eq '1234567890'
31
+ end
32
+
33
+ it 'adds the api_key in the user_set to the Supplejack::Item' do
34
+ supplejack_set.api_key = 'abc123'
35
+ Supplejack::ItemRelation.new(supplejack_set).items.first.api_key.should eq 'abc123'
36
+ end
37
+ end
38
+
39
+ describe '#all' do
40
+ it 'returns the array of @items' do
41
+ supplejack_set.items.all.should be_a Array
42
+ end
43
+ end
44
+
45
+ describe '#find' do
46
+ it 'returns finds the item by record_id' do
47
+ item = relation.find(1)
48
+ item.should be_a Supplejack::Item
49
+ item.record_id.should eq 1
50
+ end
51
+
52
+ it 'finds the item by a string record_id' do
53
+ item = relation.find('1')
54
+ item.should be_a Supplejack::Item
55
+ item.record_id.should eq 1
56
+ end
57
+ end
58
+
59
+ describe '#build' do
60
+ it 'initializes a new item object with the user_set_id' do
61
+ item = relation.build
62
+ item.user_set_id.should eq '1234567890'
63
+ end
64
+
65
+ it 'accepts a hash of attributes' do
66
+ item = relation.build(record_id: 2, position: 9)
67
+ item.record_id.should eq 2
68
+ item.position.should eq 9
69
+ end
70
+
71
+ it 'adds the user_set api_key' do
72
+ relation.stub(:user_set) { double(:user_set, api_key: '1234').as_null_object }
73
+ item = relation.build
74
+ item.api_key.should eq '1234'
75
+ end
76
+ end
77
+
78
+ describe '#create' do
79
+ let(:item) { double(:item).as_null_object }
80
+
81
+ it 'builds and saves the item' do
82
+ relation.should_receive(:build) { item }
83
+ item.should_receive(:save)
84
+ relation.create
85
+ end
86
+
87
+ it 'passes the parameters along to the build method' do
88
+ relation.should_receive(:build).with(record_id: 8, position: 3) { item }
89
+ relation.create(record_id: 8, position: 3)
90
+ end
91
+ end
92
+
93
+ context 'items array behaviour' do
94
+ it 'executes array methods on the @items array' do
95
+ relation = Supplejack::ItemRelation.new(supplejack_set)
96
+ items = relation.instance_variable_get('@items')
97
+ items.should_receive(:size)
98
+ relation.size
99
+ end
100
+
101
+ it 'should be able to iterate through the items relation' do
102
+ relation = Supplejack::ItemRelation.new(supplejack_set)
103
+ relation.each do |item|
104
+ item.should be_a Supplejack::Item
105
+ end
106
+ relation.size.should eq 1
107
+ end
108
+ end
109
+ end
110
+ end
111
+
@@ -0,0 +1,115 @@
1
+ # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
2
+ # and is licensed under the GNU General Public License, version 3.
3
+ # See https://github.com/DigitalNZ/supplejack_client for details.
4
+ #
5
+ # Supplejack was created by DigitalNZ at the National Library of NZ
6
+ # and the Department of Internal Affairs. http://digitalnz.org/supplejack
7
+
8
+ require 'spec_helper'
9
+
10
+ module Supplejack
11
+ describe Item do
12
+
13
+ describe '#initialize' do
14
+ it 'accepts a hash of attributes' do
15
+ Supplejack::Item.new({record_id: '123', name: 'Dog'})
16
+ end
17
+
18
+ it 'accepts a hash with string keys' do
19
+ Supplejack::Item.new({'record_id' => '123'}).record_id.should eq '123'
20
+ end
21
+
22
+ it 'handles nil attributes' do
23
+ Supplejack::Item.new(nil).record_id.should be_nil
24
+ end
25
+
26
+ [:record_id].each do |attribute|
27
+ it 'should initialize the attribute #{attribute}' do
28
+ Supplejack::Item.new({attribute => 'value'}).send(attribute).should eq 'value'
29
+ end
30
+ end
31
+ end
32
+
33
+ describe '#attributes' do
34
+ it 'should not include the api_key' do
35
+ Supplejack::Item.new({api_key: '1234'}).attributes.should_not have_key(:api_key)
36
+ end
37
+
38
+ it 'should not include the user_set_id' do
39
+ Supplejack::Item.new({user_set_id: '1234'}).attributes.should_not have_key(:user_set_id)
40
+ end
41
+ end
42
+
43
+ describe '#save' do
44
+ let(:item) { Supplejack::Item.new(record_id: 1, title: 'Dogs', user_set_id: '1234', api_key: 'abc') }
45
+
46
+ it 'triggers a post request to create a set_item with the set api_key' do
47
+ item.should_receive(:post).with('/sets/1234/records', {api_key: 'abc'}, {record: {record_id: 1}})
48
+ item.save.should be_true
49
+ end
50
+
51
+ it 'sends the position when set' do
52
+ item.should_receive(:post).with('/sets/1234/records', {api_key: 'abc'}, {record: {record_id: 1, position: 3}})
53
+ item.position = 3
54
+ item.save.should be_true
55
+ end
56
+
57
+ context 'HTTP error is raised' do
58
+ before :each do
59
+ item.stub(:post).and_raise(RestClient::Forbidden.new)
60
+ end
61
+
62
+ it 'returns false when a HTTP error is raised' do
63
+ item.save.should be_false
64
+ end
65
+
66
+ it 'stores the error when a error is raised' do
67
+ item.save
68
+ item.errors.should eq 'Forbidden: '
69
+ end
70
+ end
71
+ end
72
+
73
+ describe '#destroy' do
74
+ let(:item) { Supplejack::Item.new(user_set_id: '1234', api_key: 'abc', record_id: 5) }
75
+
76
+ it 'triggers a delete request with the user_set api_key' do
77
+ item.should_receive(:delete).with('/sets/1234/records/5', {api_key: 'abc'})
78
+ item.destroy
79
+ end
80
+
81
+ context 'HTTP error is raised' do
82
+ before :each do
83
+ item.stub(:delete).and_raise(RestClient::Forbidden.new)
84
+ end
85
+
86
+ it 'returns false when a HTTP error is raised' do
87
+ item.destroy.should be_false
88
+ end
89
+
90
+ it 'stores the error when a error is raised' do
91
+ item.destroy
92
+ item.errors.should eq 'Forbidden: '
93
+ end
94
+ end
95
+ end
96
+
97
+ describe '#date' do
98
+ it 'returns a Time object' do
99
+ item = Supplejack::Item.new(date: ['1977-01-01T00:00:00.000Z'])
100
+ item.date.should eq Time.parse('1977-01-01T00:00:00.000Z')
101
+ end
102
+
103
+ it 'returns nil when the date is not in the correct format' do
104
+ item = Supplejack::Item.new(date: ['afsdfgsdfg'])
105
+ item.date.should be_nil
106
+ end
107
+ end
108
+
109
+ describe '#method_missing' do
110
+ it 'returns nil for any unknown attribute' do
111
+ Supplejack::Item.new.non_existent_method.should be_nil
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,40 @@
1
+ # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
2
+ # and is licensed under the GNU General Public License, version 3.
3
+ # See https://github.com/DigitalNZ/supplejack_client for details.
4
+ #
5
+ # Supplejack was created by DigitalNZ at the National Library of NZ
6
+ # and the Department of Internal Affairs. http://digitalnz.org/supplejack
7
+
8
+ require 'spec_helper'
9
+
10
+ module Supplejack
11
+ describe LogSubscriber do
12
+
13
+ it 'returns nil when logging not enabled' do
14
+ Supplejack.stub(:enable_logging) { false }
15
+ Supplejack::LogSubscriber.new.log_request(1, {}).should be_nil
16
+ end
17
+
18
+ describe '#api_environment' do
19
+ it 'returns Prod' do
20
+ Supplejack.stub(:api_url) { 'http://api.digitalnz.org' }
21
+ Supplejack::LogSubscriber.new.api_environment.should eq 'Prod'
22
+ end
23
+
24
+ it 'returns Staging' do
25
+ Supplejack.stub(:api_url) { 'http://hippo.uat.digitalnz.org:8001' }
26
+ Supplejack::LogSubscriber.new.api_environment.should eq 'Staging'
27
+ end
28
+
29
+ it 'returns Staging' do
30
+ Supplejack.stub(:api_url) { 'http://api.uat.digitalnz.org' }
31
+ Supplejack::LogSubscriber.new.api_environment.should eq 'Staging'
32
+ end
33
+
34
+ it 'returns Dev' do
35
+ Supplejack.stub(:api_url) { 'http://localhost:3000' }
36
+ Supplejack::LogSubscriber.new.api_environment.should eq 'Dev'
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,43 @@
1
+ # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
2
+ # and is licensed under the GNU General Public License, version 3.
3
+ # See https://github.com/DigitalNZ/supplejack_client for details.
4
+ #
5
+ # Supplejack was created by DigitalNZ at the National Library of NZ
6
+ # and the Department of Internal Affairs. http://digitalnz.org/supplejack
7
+
8
+ require 'spec_helper'
9
+
10
+ describe 'PaginatedCollection' do
11
+ subject { Supplejack::PaginatedCollection.new [], 1, 10, 20 }
12
+
13
+ it { subject.should be_an(Array) }
14
+
15
+ context 'behaves like a WillPaginate::Collection' do
16
+ it { subject.total_entries.should eql(20) }
17
+ it { subject.total_pages.should eql(2) }
18
+ it { subject.current_page.should eql(1) }
19
+ it { subject.per_page.should eql(10) }
20
+ it { subject.previous_page.should be_nil }
21
+ it { subject.next_page.should eql(2) }
22
+ it { subject.out_of_bounds?.should_not be_true }
23
+ it { subject.offset.should eql(0) }
24
+
25
+ it 'should allow setting total_count' do
26
+ subject.total_count = 1
27
+ subject.total_count.should eql(1)
28
+ end
29
+
30
+ it 'should allow setting total_entries' do
31
+ subject.total_entries = 1
32
+ subject.total_entries.should eql(1)
33
+ end
34
+ end
35
+
36
+ context 'behaves like Kaminari' do
37
+ it { subject.total_count.should eql(20) }
38
+ it { subject.num_pages.should eql(2) }
39
+ it { subject.limit_value.should eql(10) }
40
+ it { subject.first_page?.should be_true }
41
+ it { subject.last_page?.should_not be_true }
42
+ end
43
+ end
@@ -0,0 +1,255 @@
1
+ # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
2
+ # and is licensed under the GNU General Public License, version 3.
3
+ # See https://github.com/DigitalNZ/supplejack_client for details.
4
+ #
5
+ # Supplejack was created by DigitalNZ at the National Library of NZ
6
+ # and the Department of Internal Affairs. http://digitalnz.org/supplejack
7
+
8
+ require 'spec_helper'
9
+
10
+ class SupplejackRecord
11
+ include Supplejack::Record
12
+ end
13
+
14
+ class Search < Supplejack::Search
15
+ def initialize(params={})
16
+ super
17
+ self.or = {:type => ['Person']}
18
+ end
19
+ end
20
+
21
+ class SpecialSearch < Supplejack::Search
22
+ def initialize(params={})
23
+ super(params)
24
+ if self.api_params && self.api_params[:and]
25
+ self.api_params[:and].delete(:format)
26
+ end
27
+ end
28
+ end
29
+
30
+ module Supplejack
31
+ describe Record do
32
+ it 'initializes its attributes from a JSON string' do
33
+ record = SupplejackRecord.new(%{{"type": "Person", "location": "NZ"}})
34
+ record.attributes.should eq({:type => 'Person', :location => 'NZ'})
35
+ end
36
+
37
+ it 'handles nil params' do
38
+ record = SupplejackRecord.new(nil)
39
+ record.attributes.should eq({})
40
+ end
41
+
42
+ it 'handles a string as params' do
43
+ record = SupplejackRecord.new('')
44
+ record.attributes.should eq({})
45
+ end
46
+
47
+ it 'handles a array as params' do
48
+ record = SupplejackRecord.new([])
49
+ record.attributes.should eq({})
50
+ end
51
+
52
+ it 'raises a NoMethodError for every method call that doesn\'t have a key in the attributes' do
53
+ record = SupplejackRecord.new
54
+ expect { record.something }.to raise_error(NoMethodError)
55
+ end
56
+
57
+ it 'should return the value when is present in the attributes' do
58
+ record = SupplejackRecord.new(:weird_method => 'Something')
59
+ record.weird_method.should eq 'Something'
60
+ end
61
+
62
+ describe 'id' do
63
+ it 'returns the record_id' do
64
+ record = SupplejackRecord.new({'record_id' => '95'})
65
+ record.id.should eq 95
66
+ end
67
+
68
+ it 'returns the id' do
69
+ record = SupplejackRecord.new({'id' => '96'})
70
+ record.id.should eq 96
71
+ end
72
+ end
73
+
74
+ describe '#title' do
75
+ it 'returns the title attribute value' do
76
+ SupplejackRecord.new(title: 'Dogs').title.should eq 'Dogs'
77
+ end
78
+
79
+ it 'returns "Untitled" for records without a title' do
80
+ SupplejackRecord.new(title: nil).title.should eq 'Untitled'
81
+ end
82
+ end
83
+
84
+ describe '#metadata' do
85
+ it 'returns an array of hashes with supplejack fields their values and schemas' do
86
+ Supplejack.stub(:supplejack_fields) { [:location] }
87
+ record = SupplejackRecord.new({:location => 'Wellington'})
88
+ record.metadata.should include({:name => 'location', :schema => 'supplejack', :value => 'Wellington'})
89
+ end
90
+
91
+ it 'should not return metadata for inexistent attribtues' do
92
+ Supplejack.stub(:supplejack_fields) { [:description] }
93
+ record = SupplejackRecord.new({:location => 'Wellington'})
94
+ record.metadata.should be_empty
95
+ end
96
+
97
+ it 'returns multiple elements for a multi value field' do
98
+ Supplejack.stub(:supplejack_fields) { [:location] }
99
+ record = SupplejackRecord.new({:location => ['Wellington', 'Auckland']})
100
+ record.metadata.should include({:name => 'location', :schema => 'supplejack', :value => 'Wellington'}, {:name => 'location', :schema => 'supplejack', :value => 'Auckland'})
101
+ end
102
+
103
+ it 'returns a empty array for a empty field' do
104
+ Supplejack.stub(:supplejack_fields) { [:location] }
105
+ record = SupplejackRecord.new({:location => nil})
106
+ record.metadata.should be_empty
107
+ end
108
+
109
+ it 'works for boolean fields too' do
110
+ Supplejack.stub(:supplejack_fields) { [:is_human] }
111
+ record = SupplejackRecord.new({:is_human => true})
112
+ record.metadata.should include({:name => 'is_human', :schema => 'supplejack', :value => true})
113
+ end
114
+
115
+ it 'works for boolean fields when they are true' do
116
+ Supplejack.stub(:admin_fields) { [:is_animal] }
117
+ record = SupplejackRecord.new({:is_animal => true})
118
+ record.metadata.should include({:name => 'is_animal', :schema => 'admin', :value => true})
119
+ end
120
+
121
+ it 'works for boolean fields when they are false' do
122
+ Supplejack.stub(:admin_fields) { [:is_animal] }
123
+ record = SupplejackRecord.new({:is_animal => false})
124
+ record.metadata.should include({:name => 'is_animal', :schema => 'admin', :value => false})
125
+ end
126
+
127
+ it 'returns names with the schema removed' do
128
+ Supplejack.stub(:supplejack_fields) { [:identifier] }
129
+ record = SupplejackRecord.new({:identifier => 'sj:IE1174615'})
130
+ record.metadata.should include({:name => 'identifier', :schema => 'supplejack', :value => 'sj:IE1174615'})
131
+ end
132
+ end
133
+
134
+ describe '#single_value_methods' do
135
+ before(:each) do
136
+ Supplejack.single_value_methods = [:description]
137
+ end
138
+
139
+ it 'converts values defined in the single_value_methods to a string' do
140
+ record = SupplejackRecord.new({'description' => ['One', 'Two']})
141
+ record.description.should eq 'One'
142
+ end
143
+
144
+ it 'returns the string if is already a string' do
145
+ record = SupplejackRecord.new({'description' => 'One'})
146
+ record.description.should eq 'One'
147
+ end
148
+ end
149
+
150
+ [:next_record, :previous_record, :next_page, :previous_page].each do |attr|
151
+ describe "#{attr}" do
152
+ it "returns the #{attr}" do
153
+ record = SupplejackRecord.new({attr => 1})
154
+ record.send(attr).should eq 1
155
+ end
156
+
157
+ it "returns the nil" do
158
+ record = SupplejackRecord.new({})
159
+ record.send(attr).should be_nil
160
+ end
161
+ end
162
+ end
163
+
164
+ describe '#find' do
165
+ context 'single record' do
166
+ it 'raises a Supplejack::RecordNotFound' do
167
+ SupplejackRecord.stub(:get).and_raise(RestClient::ResourceNotFound)
168
+ expect { SupplejackRecord.find(1) }.to raise_error(Supplejack::RecordNotFound)
169
+ end
170
+
171
+ it 'raises a Supplejack::MalformedRequest' do
172
+ expect { SupplejackRecord.find('replace_this') }.to raise_error(Supplejack::MalformedRequest)
173
+ end
174
+
175
+ it 'requests the record from the API' do
176
+ SupplejackRecord.should_receive(:get).with('/records/1', {:fields => 'default'}).and_return({'record' => {}})
177
+ SupplejackRecord.find(1)
178
+ end
179
+
180
+ it 'initializes a new SupplejackRecord object' do
181
+ SupplejackRecord.stub(:get).and_return({'record' => {'record_id' => '1', 'title' => 'Wellington'}})
182
+ record = SupplejackRecord.find(1)
183
+ record.class.should eq SupplejackRecord
184
+ record.id.should eq 1
185
+ record.title.should eq 'Wellington'
186
+ end
187
+
188
+ it 'send the fields defined in the configuration' do
189
+ Supplejack.stub(:fields) { [:verbose,:default] }
190
+ SupplejackRecord.should_receive(:get).with('/records/1', {:fields => 'verbose,default'}).and_return({'record' => {}})
191
+ SupplejackRecord.find(1)
192
+ end
193
+
194
+ it 'sets the correct search options' do
195
+ SupplejackRecord.should_receive(:get).with('/records/1', hash_including(:search => hash_including({:text=>'dog'}))).and_return({'record' => {}})
196
+ SupplejackRecord.find(1, {:text => 'dog'})
197
+ end
198
+
199
+ context '#using a special search klass' do
200
+ before(:each) do
201
+ @search = ::Search.new
202
+ end
203
+
204
+ it 'uses the specified search klass' do
205
+ SupplejackRecord.stub(:get) { {'record' => {}} }
206
+ Supplejack.stub(:search_klass) { 'Search' }
207
+ ::Search.should_receive(:new).with({:i => {:location => 'Wellington'}}).and_return(@search)
208
+ SupplejackRecord.find(1, {:i => {:location => 'Wellington'}})
209
+ end
210
+
211
+ it 'uses the default search klass' do
212
+ SupplejackRecord.stub(:get) { {'record' => {}} }
213
+ Supplejack.stub(:search_klass) { nil }
214
+ Supplejack::Search.should_receive(:new).with({:i => {:location => 'Wellington'}}).and_return(@search)
215
+ SupplejackRecord.find(1, {:i => {:location => 'Wellington'}})
216
+ end
217
+
218
+ it 'sends the params from the subclassed search to the API' do
219
+ Supplejack.stub(:search_klass) { 'Search' }
220
+ SupplejackRecord.should_receive(:get).with("/records/1", hash_including(:search => hash_including(:and=>{:name=>'John'}, :or=>{:type=>['Person']}))).and_return({'record' => {}})
221
+ SupplejackRecord.find(1, {:i => {:name => 'John'}})
222
+ end
223
+
224
+ it 'sends any changes to the api_params made on the subclassed search object' do
225
+ Supplejack.stub(:search_klass) { 'SpecialSearch' }
226
+ SupplejackRecord.should_receive(:get).with('/records/1', hash_including(:search => hash_including(:and=>{}))).and_return({'record' => {}})
227
+ SupplejackRecord.find(1, {:i => {:format => 'Images'}})
228
+ end
229
+ end
230
+ end
231
+
232
+ context 'multiple records' do
233
+ it 'sends a request to /records/multiple endpoint with an array of record ids' do
234
+ SupplejackRecord.should_receive(:get).with('/records/multiple', {:record_ids => [1,2], :fields => 'default'}).and_return({'records' => []})
235
+ SupplejackRecord.find([1,2])
236
+ end
237
+
238
+ it 'initializes multiple SupplejackRecord objects' do
239
+ SupplejackRecord.stub(:get).and_return({'records' => [{'id' => '1'}, {'id' => '2'}]})
240
+ records = SupplejackRecord.find([1,2])
241
+ records.size.should eq 2
242
+ records.first.class.should eq SupplejackRecord
243
+ records.first.id.should eq 1
244
+ end
245
+
246
+ it 'requests the fields in Supplejack.fields' do
247
+ Supplejack.stub(:fields) { [:verbose,:description] }
248
+ SupplejackRecord.should_receive(:get).with('/records/multiple', {:record_ids => [1,2], :fields => 'verbose,description'}).and_return({'records' => []})
249
+ SupplejackRecord.find([1,2])
250
+ end
251
+ end
252
+ end
253
+
254
+ end
255
+ end