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,99 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../../lib/response'
3
+
4
+ module RescueGroups
5
+ describe Response do
6
+ subject { described_class.new(raw_response) }
7
+
8
+ describe 'instance variables' do
9
+ let(:raw_response) do
10
+ Class.new(Object) do
11
+ attr_reader :code
12
+ def parsed_response; {}; end
13
+ end.new
14
+ end
15
+
16
+ it 'has a parsed body' do
17
+ expect(subject.instance_variables).to include(:@parsed_body)
18
+ end
19
+
20
+ it 'has an http status code' do
21
+ expect(subject.instance_variables).to include(:@http_status_code)
22
+ end
23
+ end
24
+
25
+ describe '#success?' do
26
+ context 'a successful response' do
27
+ let(:raw_response) do
28
+ Class.new(Object) do
29
+ attr_reader :code
30
+ def parsed_response
31
+ {
32
+ 'status' => "ok",
33
+ 'data' => []
34
+ }
35
+ end
36
+ end.new
37
+ end
38
+
39
+ specify do
40
+ expect(subject).to be_success
41
+ end
42
+ end
43
+
44
+ context 'an errorful response' do
45
+ let(:raw_response) do
46
+ Class.new(Object) do
47
+ attr_reader :code
48
+ def parsed_response
49
+ JSON.parse(File.read("#{ File.dirname(__FILE__) }/../fixtures/error.json"))
50
+ end
51
+ end.new
52
+ end
53
+
54
+ specify do
55
+ expect(subject).to_not be_success
56
+ end
57
+ end
58
+ end
59
+
60
+ describe '#error' do
61
+ context 'errorful response' do
62
+ let(:raw_response) do
63
+ Class.new(Object) do
64
+ attr_reader :code
65
+ def parsed_response
66
+ JSON.parse(File.read("#{ File.dirname(__FILE__) }/../fixtures/error.json"))
67
+ end
68
+ end.new
69
+ end
70
+
71
+ it 'returns the error message' do
72
+ expect(subject.error).to_not be_nil
73
+ end
74
+
75
+ it 'does not return warning messages' do
76
+ expect(subject.error).to_not match(/You provided an invalid result sort field/)
77
+ end
78
+ end
79
+
80
+ context 'successful response' do
81
+ let(:raw_response) do
82
+ Class.new(Object) do
83
+ attr_reader :code
84
+ def parsed_response
85
+ {
86
+ 'status' => 'ok',
87
+ 'data' => {}
88
+ }
89
+ end
90
+ end.new
91
+ end
92
+
93
+ it 'returns nil ' do
94
+ expect(subject.error).to be_nil
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,131 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../../models/animal'
3
+ require_relative '../support/model_spec'
4
+
5
+ module RescueGroups
6
+ describe Animal do
7
+ known_attributes = { animalID: 12, animalDeclawed: true, animalName: :snuffles }
8
+ it_behaves_like 'a model', known_attributes
9
+
10
+ describe '#initialize' do
11
+ context 'picture are present' do
12
+
13
+ it 'extracts them' do
14
+ expect_any_instance_of(described_class).to receive(:extract_pictures)
15
+ described_class.find(TEST_ANIMAL_ID)
16
+ end
17
+
18
+ it 'is actual pictures' do
19
+ animal = described_class.find(TEST_ANIMAL_ID)
20
+ expect(animal.pictures).to_not be_empty
21
+ expect(animal.pictures.all? { |p| p.is_a?(Picture) }).to eq(true)
22
+ end
23
+ end
24
+ end
25
+
26
+ describe '.find' do
27
+ context 'animal is found' do
28
+ it 'does not raise error' do
29
+ expect do
30
+ described_class.find(TEST_ANIMAL_ID)
31
+ end.to_not raise_error
32
+ end
33
+
34
+ it 'finds and fills the animal' do
35
+ animal = described_class.find(TEST_ANIMAL_ID)
36
+ expect(animal.id.to_i).to eq(TEST_ANIMAL_ID)
37
+ end
38
+ end
39
+
40
+ context 'animal is not found' do
41
+ it 'raises an error' do
42
+ expect do
43
+ described_class.find(NOT_FOUND_ANIMAL_ID)
44
+ end.to raise_error("Unable to find #{ described_class } with id: #{ NOT_FOUND_ANIMAL_ID }")
45
+ end
46
+ end
47
+ end
48
+
49
+ describe '.where' do
50
+ context 'when only id is present' do
51
+ let(:id) { 1 }
52
+
53
+ it 'calls find instead' do
54
+ expect(described_class).to receive(:find).with(id)
55
+ described_class.where(id: id)
56
+ end
57
+ end
58
+
59
+ context 'with other conditions' do
60
+ let(:conditions) { { breed: TEST_ANIMAL_BREED } }
61
+
62
+ it 'adds conditions to search' do
63
+ expect_any_instance_of(AnimalSearch)
64
+ .to receive(:add_filter)
65
+ .with(:animalBreed, :equal, TEST_ANIMAL_BREED).and_call_original
66
+
67
+ described_class.where(conditions)
68
+ end
69
+
70
+ context 'when animals are found' do
71
+ it 'does not error' do
72
+ expect do
73
+ described_class.where(breed: TEST_ANIMAL_BREED)
74
+ end.to_not raise_error
75
+ end
76
+
77
+ it 'sets the breed correctly' do
78
+ animals = described_class.where(breed: TEST_ANIMAL_BREED)
79
+
80
+ expect(animals).to_not eq([])
81
+
82
+ animals.each do |animal|
83
+ expect(animal.breed).to_not be_nil
84
+ end
85
+ end
86
+ end
87
+
88
+ context 'when animals are not found' do
89
+ it 'does not error' do
90
+ expect do
91
+ described_class.where(breed: NOT_FOUND_ANIMAL_BREED)
92
+ end.to_not raise_error
93
+ end
94
+
95
+ it 'returns an empty array' do
96
+ animals = described_class.where(breed: NOT_FOUND_ANIMAL_BREED)
97
+ expect(animals).to eq([])
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ describe 'organization' do
104
+ it 'defines #organization' do
105
+ expect(subject).to respond_to(:organization)
106
+ end
107
+
108
+ it 'defines #organization=' do
109
+ expect(subject).to respond_to(:organization=)
110
+ end
111
+ end
112
+
113
+ describe '!#extract_pictures' do
114
+ let(:picture_attribute) { described_class.object_fields::FIELDS[:pictures] }
115
+ let(:pictures) { [{ test: :picture }, { foo: :bar }, { baz: :qux }] }
116
+
117
+ it 'intializes a new picture class per picture' do
118
+ pictures.each do |picture|
119
+ expect(RescueGroups::Picture).to receive(:new).with(picture).and_call_original
120
+ end
121
+
122
+ described_class.new(picture_attribute => pictures)
123
+ end
124
+
125
+ it 'turns the @pictures for the model into pictures' do
126
+ model = described_class.new(picture_attribute => pictures)
127
+ expect(model.pictures.all? { |p| p.is_a?(RescueGroups::Picture) }).to eq(true)
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,105 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../support/model_spec'
3
+ require_relative '../../models/event'
4
+
5
+ module RescueGroups
6
+ describe Event do
7
+ known_attributes = begin
8
+ { eventID: 12, eventName: 'Great Event!', eventDescription: 'This is the great event.' }
9
+ end
10
+ it_behaves_like 'a model', known_attributes
11
+
12
+ describe '.find' do
13
+ context 'event is found' do
14
+ it 'does not raise error' do
15
+ expect do
16
+ described_class.find(TEST_EVENT_ID)
17
+ end.to_not raise_error
18
+ end
19
+
20
+ it 'finds and fills the event' do
21
+ event = described_class.find(TEST_EVENT_ID)
22
+ expect(event.id.to_i).to eq(TEST_EVENT_ID)
23
+ end
24
+ end
25
+
26
+ context 'event is not found' do
27
+ it 'raises an error' do
28
+ expect do
29
+ described_class.find(NOT_FOUND_EVENT_ID)
30
+ end.to raise_error("Unable to find #{ described_class } with id: #{ NOT_FOUND_EVENT_ID }")
31
+ end
32
+ end
33
+ end
34
+
35
+ describe '.where' do
36
+ context 'when only id is present' do
37
+ let(:id) { 1 }
38
+
39
+ it 'calls find instead' do
40
+ expect(described_class).to receive(:find).with(id)
41
+ described_class.where(id: id)
42
+ end
43
+ end
44
+
45
+ context 'with other conditions' do
46
+ let(:conditions) { { name: TEST_EVENT_NAME } }
47
+
48
+ it 'adds conditions to search' do
49
+ expect_any_instance_of(EventSearch)
50
+ .to receive(:add_filter)
51
+ .with(:eventName, :equal, TEST_EVENT_NAME)
52
+
53
+ allow_any_instance_of(RemoteClient)
54
+ .to receive_message_chain(:post_and_respond, :success?) { true }
55
+
56
+ allow_any_instance_of(RemoteClient)
57
+ .to receive_message_chain(:post_and_respond, :[]) { {} }
58
+
59
+ described_class.where(conditions)
60
+ end
61
+
62
+ context 'when events are found' do
63
+ it 'does not error' do
64
+ expect do
65
+ described_class.where(name: TEST_EVENT_NAME)
66
+ end.to_not raise_error
67
+ end
68
+
69
+ it 'sets the name correctly' do
70
+ events = described_class.where(name: TEST_EVENT_NAME)
71
+
72
+ expect(events).to_not eq([])
73
+
74
+ events.each do |event|
75
+ expect(event.name).to_not be_nil
76
+ end
77
+ end
78
+ end
79
+
80
+ context 'when events are not found' do
81
+ it 'does not error' do
82
+ expect do
83
+ described_class.where(name: NOT_FOUND_EVENT_NAME)
84
+ end.to_not raise_error
85
+ end
86
+
87
+ it 'returns an empty array' do
88
+ events = described_class.where(name: NOT_FOUND_EVENT_NAME)
89
+ expect(events).to eq([])
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ describe 'organization' do
96
+ it 'should define #organization' do
97
+ expect(subject).to respond_to(:organization)
98
+ end
99
+
100
+ it 'should define #organization=' do
101
+ expect(subject).to respond_to(:organization=)
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,112 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../support/model_spec'
3
+ require_relative '../../models/organization'
4
+
5
+ module RescueGroups
6
+ describe Organization do
7
+ known_attributes = { orgCountry: 'USA', orgName: 'That one dog house'}
8
+ it_behaves_like 'a model', known_attributes
9
+
10
+ describe '.find' do
11
+ context 'org is found' do
12
+ it 'does not raise error' do
13
+ expect do
14
+ described_class.find(TEST_ORG_ID)
15
+ end.to_not raise_error
16
+ end
17
+
18
+ it 'finds and fills the organization' do
19
+ org = described_class.find(TEST_ORG_ID)
20
+ expect(org.id.to_i).to eq(TEST_ORG_ID)
21
+ end
22
+ end
23
+
24
+ context 'org is not found' do
25
+ it 'raises an error' do
26
+ expect do
27
+ described_class.find(NOT_FOUND_ORG_ID)
28
+ end.to raise_error("Unable to find #{ described_class } with id: #{ NOT_FOUND_ORG_ID }")
29
+ end
30
+ end
31
+ end
32
+
33
+ describe '.where' do
34
+ context 'when only id is present' do
35
+ let(:id) { 1 }
36
+
37
+ it 'calls find instead' do
38
+ expect(described_class).to receive(:find).with(id)
39
+ described_class.where(id: id)
40
+ end
41
+ end
42
+
43
+ context 'with other conditions' do
44
+ let(:conditions) do
45
+ {
46
+ name: 'test name',
47
+ location_city: 'test city',
48
+ }
49
+ end
50
+
51
+ it 'adds conditions to search' do
52
+ expect_any_instance_of(OrganizationSearch)
53
+ .to receive(:add_filter)
54
+ .with(:orgName, :equal, 'test name')
55
+
56
+ expect_any_instance_of(OrganizationSearch)
57
+ .to receive(:add_filter)
58
+ .with(:orgCity, :equal, 'test city')
59
+
60
+ allow_any_instance_of(RemoteClient)
61
+ .to receive_message_chain(:post_and_respond, :success?) { true }
62
+
63
+ allow_any_instance_of(RemoteClient)
64
+ .to receive_message_chain(:post_and_respond, :[]) { {} }
65
+
66
+ described_class.where(conditions)
67
+ end
68
+
69
+ context 'when orgs are found' do
70
+ it 'does not error' do
71
+ expect do
72
+ described_class.where(name: TEST_ORG_NAME)
73
+ end.to_not raise_error
74
+ end
75
+
76
+ it 'sets the names correctly' do
77
+ orgs = described_class.where(name: TEST_ORG_NAME)
78
+
79
+ expect(orgs).to_not eq([])
80
+
81
+ orgs.each do |org|
82
+ expect(org.name).to_not be_nil
83
+ end
84
+ end
85
+ end
86
+
87
+ context 'when orgs are not found' do
88
+ it 'does not error' do
89
+ expect do
90
+ described_class.where(name: NOT_FOUND_ORG_NAME)
91
+ end.to_not raise_error
92
+ end
93
+
94
+ it 'returns an empty array' do
95
+ orgs = described_class.where(name: NOT_FOUND_ORG_NAME)
96
+ expect(orgs).to eq([])
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ describe '#animals' do
103
+ it 'defines #animals' do
104
+ expect(subject).to respond_to(:animals)
105
+ end
106
+
107
+ it 'defines #animals=' do
108
+ expect(subject).to respond_to(:animals=)
109
+ end
110
+ end
111
+ end
112
+ end