rescue_groups 0.0.1

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.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/.travis.yml +3 -0
  4. data/Gemfile +3 -0
  5. data/Gemfile.lock +90 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +226 -0
  8. data/Rakefile +30 -0
  9. data/config/config.rb +26 -0
  10. data/config/initializer.rb +15 -0
  11. data/docs/animal_field.md +138 -0
  12. data/docs/event_field.md +20 -0
  13. data/docs/organization_field.md +25 -0
  14. data/fields/animal_field.rb +152 -0
  15. data/fields/event_field.rb +35 -0
  16. data/fields/organization_field.rb +40 -0
  17. data/fields/picture_field.rb +30 -0
  18. data/lib/api_client.rb +29 -0
  19. data/lib/queryable.rb +79 -0
  20. data/lib/relationable.rb +76 -0
  21. data/lib/remote_client.rb +29 -0
  22. data/lib/remote_model.rb +47 -0
  23. data/lib/requests/find.rb +29 -0
  24. data/lib/requests/invalid_client.rb +1 -0
  25. data/lib/requests/where.rb +94 -0
  26. data/lib/response.rb +48 -0
  27. data/models/animal.rb +57 -0
  28. data/models/event.rb +41 -0
  29. data/models/organization.rb +41 -0
  30. data/models/picture.rb +26 -0
  31. data/rescue_groups.gemspec +28 -0
  32. data/rescue_groups.rb +27 -0
  33. data/search/animal_search.rb +15 -0
  34. data/search/base_search.rb +72 -0
  35. data/search/event_search.rb +15 -0
  36. data/search/filter.rb +49 -0
  37. data/search/organization_search.rb +15 -0
  38. data/spec/fixtures/animal/find.json +1 -0
  39. data/spec/fixtures/animal/where.json +1 -0
  40. data/spec/fixtures/error.json +20 -0
  41. data/spec/fixtures/event/find.json +1 -0
  42. data/spec/fixtures/event/where.json +1 -0
  43. data/spec/fixtures/organization/find.json +1 -0
  44. data/spec/fixtures/organization/where.json +1 -0
  45. data/spec/fixtures/test_constants.rb +12 -0
  46. data/spec/integration/animal_spec.rb +55 -0
  47. data/spec/integration/event_spec.rb +33 -0
  48. data/spec/integration/organization_spec.rb +35 -0
  49. data/spec/lib/queryable_spec.rb +257 -0
  50. data/spec/lib/relationable_spec.rb +113 -0
  51. data/spec/lib/remote_client_spec.rb +27 -0
  52. data/spec/lib/requests/find_spec.rb +97 -0
  53. data/spec/lib/requests/where_spec.rb +267 -0
  54. data/spec/lib/response_spec.rb +99 -0
  55. data/spec/models/animal_spec.rb +131 -0
  56. data/spec/models/event_spec.rb +105 -0
  57. data/spec/models/organization_spec.rb +112 -0
  58. data/spec/models/picture_spec.rb +87 -0
  59. data/spec/search/animal_search_spec.rb +8 -0
  60. data/spec/search/event_search_spec.rb +8 -0
  61. data/spec/search/filter_spec.rb +39 -0
  62. data/spec/search/organization_search_spec.rb +8 -0
  63. data/spec/spec_helper.rb +340 -0
  64. data/spec/support/model_spec.rb +47 -0
  65. data/spec/support/searchable_spec.rb +15 -0
  66. data/support/animal_mock.rb +215 -0
  67. data/support/base_mock.rb +44 -0
  68. data/support/event_mock.rb +48 -0
  69. data/support/organization_mock.rb +53 -0
  70. data/version.rb +3 -0
  71. metadata +242 -0
@@ -0,0 +1,87 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../../models/picture'
3
+
4
+ module RescueGroups
5
+ describe Picture do
6
+ describe '#initialize' do
7
+ context 'given a list of rescue groups attributes' do
8
+ let(:attributes) do
9
+ {}.tap do |attrs|
10
+ PictureField::FIELDS.keys.sample(5).each do |key|
11
+ attrs[key] = anything
12
+ end
13
+ end
14
+ end
15
+
16
+ it 'maps the attributes' do
17
+ picture = described_class.new(attributes)
18
+
19
+ attributes.keys.each do |attr_name|
20
+ mapped_key = PictureField::FIELDS[attr_name]
21
+ expect(picture.send(mapped_key)).to_not be_nil
22
+ end
23
+ end
24
+
25
+ context 'unknown attributes are present' do
26
+ it 'does not map them' do
27
+ picture = described_class.new(attributes.merge(foo: :bar))
28
+
29
+ attributes.keys.each do |attr_name|
30
+ mapped_key = PictureField::FIELDS[attr_name]
31
+ expect(picture.send(mapped_key)).to_not be_nil
32
+ end
33
+
34
+ expect(picture.instance_variable_get(:@foo)).to be_nil
35
+ end
36
+ end
37
+ end
38
+
39
+ context 'given a list of random attributes' do
40
+ let(:attributes) {{ foo: :bar, baz: :qux }}
41
+
42
+ it 'does not make any instance variables' do
43
+ expect(described_class.new(attributes).instance_variables).to be_empty
44
+ end
45
+ end
46
+ end
47
+
48
+ describe '#url' do
49
+ context 'secure' do
50
+ it 'calls the secure attribute' do
51
+ expect(subject).to receive(:url_full)
52
+ subject.url(secure: true)
53
+ end
54
+ end
55
+
56
+ context 'default behaviour' do
57
+ it 'calls the default attribute' do
58
+ expect(subject).to receive(:insecure_url_full)
59
+ subject.url
60
+ end
61
+ end
62
+ end
63
+
64
+ describe '#url_thumb' do
65
+ context 'secure' do
66
+ it 'calls the secure attribute' do
67
+ expect(subject).to receive(:url_thumbnail)
68
+ subject.url_thumb(secure: true)
69
+ end
70
+ end
71
+
72
+ context 'default behaviour' do
73
+ it 'calls the default attribute' do
74
+ expect(subject).to receive(:insecure_url_thumb)
75
+ subject.url_thumb
76
+ end
77
+ end
78
+ end
79
+
80
+ describe '#animal' do
81
+ it { expect(subject).to respond_to(:animal) }
82
+ it { expect(subject).to respond_to(:animal=) }
83
+ it { expect(subject).to respond_to(:animal_id) }
84
+ it { expect(subject).to respond_to(:animal_id=) }
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,8 @@
1
+ require_relative '../support/searchable_spec'
2
+ require_relative '../../search/animal_search'
3
+
4
+ module RescueGroups
5
+ describe AnimalSearch do
6
+ it_behaves_like 'a searchable'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require_relative '../support/searchable_spec'
2
+ require_relative '../../search/event_search'
3
+
4
+ module RescueGroups
5
+ describe EventSearch do
6
+ it_behaves_like 'a searchable'
7
+ end
8
+ end
@@ -0,0 +1,39 @@
1
+ require_relative '../../search/filter'
2
+
3
+ module RescueGroups
4
+ describe Filter do
5
+ subject { described_class.new(name, operation, criteria) }
6
+ let(:name) { 'some attribute' }
7
+ let(:operation) { described_class::OPERATIONS.keys.sample }
8
+ let(:criteria) { 'the correct thing' }
9
+
10
+ describe '#initialize' do
11
+ it 'sets instance variables' do
12
+ expect(subject.instance_variables).to include(:@name)
13
+ expect(subject.name).to_not be_nil
14
+ expect(subject.instance_variables).to include(:@operation)
15
+ expect(subject.operation).to_not be_nil
16
+ expect(subject.instance_variables).to include(:@criteria)
17
+ expect(subject.criteria).to_not be_nil
18
+ end
19
+
20
+ context 'with an uknown operation' do
21
+ let(:operation) { :banana }
22
+ it 'raises an exception about an unknown operator' do
23
+ expect { subject }.to raise_error(described_class::InvalidFilter)
24
+ end
25
+ end
26
+ end
27
+
28
+ describe '#as_json' do
29
+ context 'with valid attributes' do
30
+ it 'returns a hash with all pertinent keys' do
31
+ result = subject.as_json
32
+ expect(result).to have_key(:fieldName)
33
+ expect(result).to have_key(:operation)
34
+ expect(result).to have_key(:criteria)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,8 @@
1
+ require_relative '../support/searchable_spec'
2
+ require_relative '../../search/organization_search'
3
+
4
+ module RescueGroups
5
+ describe OrganizationSearch do
6
+ it_behaves_like 'a searchable'
7
+ end
8
+ end
@@ -0,0 +1,340 @@
1
+ require_relative '../rescue_groups'
2
+
3
+ RescueGroups.configuration do |config|
4
+ config.apikey = 'test_api_key'
5
+ end
6
+
7
+ require 'pry'
8
+ require 'webmock/rspec'
9
+ require_relative './fixtures/test_constants'
10
+
11
+ WebMock.disable_net_connect!(allow_localhost: true)
12
+
13
+ class TestResponse
14
+ attr_reader :http_status_code, :parsed_body
15
+
16
+ def initialize(code, body)
17
+ @http_status_code = http_status_code
18
+ @parsed_body = body
19
+ end
20
+
21
+ def [](attribute)
22
+ parsed_body[attribute]
23
+ end
24
+
25
+ def success?
26
+ @parsed_body['status'] != 'error'
27
+ end
28
+
29
+ def error
30
+ 'It did not work'
31
+ end
32
+ end
33
+
34
+ RSpec.configure do |config|
35
+ fixtures_dir = "#{ File.dirname(__FILE__) }/fixtures/"
36
+ post_url = 'https://api.rescuegroups.org/http/json'
37
+ headers = { 'Content-Type' => 'application/json' }
38
+ SUCCESS = 200
39
+ NOT_FOUND = 404
40
+
41
+ config.before(:each) do
42
+ stub_request(
43
+ :post,
44
+ post_url)
45
+ .with(
46
+ body: JSON({
47
+ objectAction: :publicView,
48
+ objectType: :orgs,
49
+ fields: RescueGroups::OrganizationField.all,
50
+ values: [{ orgID: TEST_ORG_ID }],
51
+ apikey: 'test_api_key'
52
+ }),
53
+ headers: headers)
54
+ .to_return(
55
+ status: SUCCESS,
56
+ body: File.read("#{ fixtures_dir }/organization/find.json"),
57
+ headers: headers)
58
+
59
+ stub_request(
60
+ :post,
61
+ post_url)
62
+ .with(
63
+ body: JSON({
64
+ objectAction: :publicView,
65
+ objectType: :orgs,
66
+ fields: RescueGroups::OrganizationField.all,
67
+ values: [{ orgID: NOT_FOUND_ORG_ID }],
68
+ apikey: 'test_api_key'
69
+ }),
70
+ headers: headers)
71
+ .to_return(
72
+ status: NOT_FOUND,
73
+ body: '{}',
74
+ headers: headers)
75
+
76
+ stub_request(
77
+ :post,
78
+ post_url)
79
+ .with(
80
+ body: JSON({
81
+ objectAction: :publicSearch,
82
+ objectType: :orgs,
83
+ search: {
84
+ resultStart: 0,
85
+ resultLimit: 100,
86
+ resultSort: nil,
87
+ resultOrder: :asc,
88
+ calcFoundRows: 'Yes',
89
+ filters: [{
90
+ fieldName: :orgName,
91
+ operation: :equal,
92
+ criteria: TEST_ORG_NAME
93
+ }],
94
+ fields: RescueGroups::OrganizationField.all,
95
+ },
96
+ apikey: 'test_api_key'
97
+ }),
98
+ headers: headers)
99
+ .to_return(
100
+ status: SUCCESS,
101
+ body: File.read("#{ fixtures_dir }/organization/where.json"),
102
+ headers: headers)
103
+
104
+ stub_request(
105
+ :post,
106
+ post_url)
107
+ .with(
108
+ body: JSON({
109
+ objectAction: :publicSearch,
110
+ objectType: :orgs,
111
+ search: {
112
+ resultStart: 0,
113
+ resultLimit: 100,
114
+ resultSort: nil,
115
+ resultOrder: :asc,
116
+ calcFoundRows: 'Yes',
117
+ filters: [{
118
+ fieldName: :orgName,
119
+ operation: :equal,
120
+ criteria: NOT_FOUND_ORG_NAME
121
+ }],
122
+ fields: RescueGroups::OrganizationField.all,
123
+ },
124
+ apikey: 'test_api_key'
125
+ }),
126
+ headers: headers)
127
+ .to_return(
128
+ status: SUCCESS,
129
+ body: '{ "data": {} }',
130
+ headers: headers)
131
+
132
+ stub_request(
133
+ :post,
134
+ post_url)
135
+ .with(
136
+ body: JSON({
137
+ objectAction: :publicSearch,
138
+ objectType: :animals,
139
+ search: {
140
+ resultStart: 0,
141
+ resultLimit: 100,
142
+ resultSort: nil,
143
+ resultOrder: :asc,
144
+ calcFoundRows: 'Yes',
145
+ filters: [{
146
+ fieldName: :animalBreed,
147
+ operation: :equal,
148
+ criteria: TEST_ANIMAL_BREED
149
+ }],
150
+ fields: RescueGroups::AnimalField.all,
151
+ },
152
+ apikey: 'test_api_key'
153
+ }),
154
+ headers: headers)
155
+ .to_return(
156
+ status: SUCCESS,
157
+ body: File.read("#{ fixtures_dir }/animal/where.json"),
158
+ headers: headers)
159
+
160
+ stub_request(
161
+ :post,
162
+ post_url)
163
+ .with(
164
+ body: JSON({
165
+ objectAction: :publicSearch,
166
+ objectType: :animals,
167
+ search: {
168
+ resultStart: 0,
169
+ resultLimit: 100,
170
+ resultSort: nil,
171
+ resultOrder: :asc,
172
+ calcFoundRows: 'Yes',
173
+ filters: [{
174
+ fieldName: :animalOrgID,
175
+ operation: :equal,
176
+ criteria: TEST_ORG_ID
177
+ }],
178
+ fields: RescueGroups::AnimalField.all,
179
+ },
180
+ apikey: 'test_api_key'
181
+ }),
182
+ headers: headers)
183
+ .to_return(
184
+ status: SUCCESS,
185
+ body: File.read("#{ fixtures_dir }/animal/where.json"),
186
+ headers: headers)
187
+
188
+ stub_request(
189
+ :post,
190
+ post_url)
191
+ .with(
192
+ body: JSON({
193
+ objectAction: :publicSearch,
194
+ objectType: :animals,
195
+ search: {
196
+ resultStart: 0,
197
+ resultLimit: 100,
198
+ resultSort: nil,
199
+ resultOrder: :asc,
200
+ calcFoundRows: 'Yes',
201
+ filters: [{
202
+ fieldName: :animalBreed,
203
+ operation: :equal,
204
+ criteria: NOT_FOUND_ANIMAL_BREED
205
+ }],
206
+ fields: RescueGroups::AnimalField.all,
207
+ },
208
+ apikey: 'test_api_key'
209
+ }),
210
+ headers: headers)
211
+ .to_return(
212
+ status: SUCCESS,
213
+ body: '{ "data": {} }',
214
+ headers: headers)
215
+
216
+ stub_request(
217
+ :post,
218
+ post_url)
219
+ .with(
220
+ body: JSON({
221
+ objectAction: :publicView,
222
+ objectType: :animals,
223
+ fields: RescueGroups::AnimalField.all,
224
+ values: [{ animalID: TEST_ANIMAL_ID }],
225
+ apikey: 'test_api_key'
226
+ }),
227
+ headers: headers)
228
+ .to_return(
229
+ status: SUCCESS,
230
+ body: File.read("#{ fixtures_dir }/animal/find.json"),
231
+ headers: headers)
232
+
233
+ stub_request(
234
+ :post,
235
+ post_url)
236
+ .with(
237
+ body: JSON({
238
+ objectAction: :publicView,
239
+ objectType: :animals,
240
+ fields: RescueGroups::AnimalField.all,
241
+ values: [{ animalID: NOT_FOUND_ANIMAL_ID }],
242
+ apikey: 'test_api_key'
243
+ }),
244
+ headers: headers)
245
+ .to_return(
246
+ status: NOT_FOUND,
247
+ body: '{}',
248
+ headers: headers)
249
+
250
+ stub_request(
251
+ :post,
252
+ post_url)
253
+ .with(
254
+ body: JSON({
255
+ objectAction: :publicView,
256
+ objectType: :events,
257
+ fields: RescueGroups::EventField.all,
258
+ values: [{ eventID: TEST_EVENT_ID }],
259
+ apikey: 'test_api_key'
260
+ }),
261
+ headers: headers)
262
+ .to_return(
263
+ status: SUCCESS,
264
+ body: File.read("#{ fixtures_dir }/event/find.json"),
265
+ headers: headers)
266
+
267
+ stub_request(
268
+ :post,
269
+ post_url)
270
+ .with(
271
+ body: JSON({
272
+ objectAction: :publicView,
273
+ objectType: :events,
274
+ fields: RescueGroups::EventField.all,
275
+ values: [{ eventID: NOT_FOUND_EVENT_ID }],
276
+ apikey: 'test_api_key'
277
+ }),
278
+ headers: headers)
279
+ .to_return(
280
+ status: NOT_FOUND,
281
+ body: '{}',
282
+ headers: headers)
283
+
284
+ stub_request(
285
+ :post,
286
+ post_url)
287
+ .with(
288
+ body: JSON({
289
+ objectAction: :publicSearch,
290
+ objectType: :events,
291
+ search: {
292
+ resultStart: 0,
293
+ resultLimit: 100,
294
+ resultSort: nil,
295
+ resultOrder: :asc,
296
+ calcFoundRows: 'Yes',
297
+ filters: [{
298
+ fieldName: :eventName,
299
+ operation: :equal,
300
+ criteria: TEST_EVENT_NAME
301
+ }],
302
+ fields: RescueGroups::EventField.all,
303
+ },
304
+ apikey: 'test_api_key'
305
+ }),
306
+ headers: headers)
307
+ .to_return(
308
+ status: SUCCESS,
309
+ body: File.read("#{ fixtures_dir }/event/where.json"),
310
+ headers: headers)
311
+
312
+ stub_request(
313
+ :post,
314
+ post_url)
315
+ .with(
316
+ body: JSON({
317
+ objectAction: :publicSearch,
318
+ objectType: :events,
319
+ search: {
320
+ resultStart: 0,
321
+ resultLimit: 100,
322
+ resultSort: nil,
323
+ resultOrder: :asc,
324
+ calcFoundRows: 'Yes',
325
+ filters: [{
326
+ fieldName: :eventName,
327
+ operation: :equal,
328
+ criteria: NOT_FOUND_EVENT_NAME
329
+ }],
330
+ fields: RescueGroups::EventField.all,
331
+ },
332
+ apikey: 'test_api_key'
333
+ }),
334
+ headers: headers)
335
+ .to_return(
336
+ status: SUCCESS,
337
+ body: '{ "data": {} }',
338
+ headers: headers)
339
+ end
340
+ end