rescue_groups_v5 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5ea61705637c022af68b02bc1f3f910535afa53911fc05a7f48fd976e1fc2c89
4
+ data.tar.gz: 3b9d272eca1e98ed9e0f27fd0f0b811b0960171cfd043df90a6293803b069d12
5
+ SHA512:
6
+ metadata.gz: b4251007295ee4ddf4a0e19efba8e38a6aecfa32b7dd1b532fb0577d5b42b6eb0d03a79a5fe950959336393635c67ccda8679a40ea03b66a54699a07d59aeae6
7
+ data.tar.gz: 111fd9049ea4b357f7eec2693307728896678c5579d7897e3f1a189dad9ad0d957603a5d55a52ddb5083d29c51cdaf4898c44cdaad557c72f790fca761852d35
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .byebug_history
13
+
14
+ TODO.txt
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in rescue_groups_v5.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rescue_groups_v5 (0.1.1)
5
+ faraday
6
+ json
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.9.2)
12
+ public_suffix (>= 2.0.2, < 8.0)
13
+ bigdecimal (4.1.3)
14
+ byebug (13.0.0)
15
+ reline (>= 0.6.0)
16
+ crack (1.0.1)
17
+ bigdecimal
18
+ rexml
19
+ diff-lcs (1.6.2)
20
+ faraday (2.15.1)
21
+ faraday-net_http (>= 2.0, < 3.6)
22
+ json
23
+ logger
24
+ faraday-net_http (3.5.0)
25
+ net-http (~> 0.5)
26
+ hashdiff (1.2.2)
27
+ io-console (0.9.5)
28
+ json (3.1.0)
29
+ logger (1.7.0)
30
+ net-http (0.10.0)
31
+ net-protocol (>= 0.3.0)
32
+ uri (>= 0.11.1)
33
+ net-protocol (0.4.0)
34
+ timeout
35
+ public_suffix (7.0.5)
36
+ rake (13.4.2)
37
+ reline (0.7.0)
38
+ io-console (~> 0.5)
39
+ rexml (3.4.4)
40
+ rspec (3.13.2)
41
+ rspec-core (~> 3.13.0)
42
+ rspec-expectations (~> 3.13.0)
43
+ rspec-mocks (~> 3.13.0)
44
+ rspec-core (3.13.7)
45
+ rspec-support (~> 3.13.0)
46
+ rspec-expectations (3.13.6)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.13.0)
49
+ rspec-mocks (3.13.9)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.13.0)
52
+ rspec-support (3.13.8)
53
+ timeout (0.6.1)
54
+ uri (1.1.2)
55
+ vcr (6.4.1)
56
+ webmock (3.34.0)
57
+ addressable (>= 2.8.0)
58
+ crack (>= 0.3.2)
59
+ hashdiff (>= 0.4.0, < 2.0.0)
60
+
61
+ PLATFORMS
62
+ ruby
63
+
64
+ DEPENDENCIES
65
+ byebug
66
+ rake (>= 12.3.3)
67
+ rescue_groups_v5!
68
+ rspec (~> 3.0)
69
+ vcr
70
+ webmock (~> 3.7)
71
+
72
+ BUNDLED WITH
73
+ 4.1.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Ben Wanicur
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,226 @@
1
+ # RescueGroupsV5
2
+
3
+ A Ruby wrapper for the <a target="_blank" href="https://api.rescuegroups.org/v5/public/docs">RescueGroups.org API v5</a> (public endpoints).
4
+
5
+ **Note:** the v5 public API is primarily read-only for now. It does not support creating, updating, or deleting animals or organizations, so this gem only provides methods for searching and reading data.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rescue_groups_v5'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rescue_groups_v5
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ client = RescueGroupsV5::Client.new(API_KEY_HERE)
27
+
28
+ result = client.search_animals(
29
+ views: [:available, :dogs],
30
+ postalcode: "92107",
31
+ miles: 25,
32
+ fields: {
33
+ animals: [:name, :breedPrimary, :distance],
34
+ orgs: [:name, :city]
35
+ },
36
+ include: [:orgs],
37
+ sort: [{ object: :animals, sort_value: :distance, direction: :ascending }],
38
+ limit: 10
39
+ )
40
+
41
+ result["meta"]["count"] # total matching animals
42
+ result["data"] # array of animals
43
+ result["included"] # related objects requested with `include`
44
+ ```
45
+
46
+ Every method returns the parsed JSON response as a Hash with `meta`, `data` and (when requested) `included` keys. API errors are not raised; they are returned under an `errors` key:
47
+
48
+ ```ruby
49
+ result["errors"] # => [{ "status" => 400, "title" => "Invalid Sort", "detail" => "..." }]
50
+ ```
51
+
52
+ ### Client Methods
53
+
54
+ | Method | API endpoint |
55
+ |---|---|
56
+ | `search_animals(opts)` | `POST /public/animals/search/{views}` |
57
+ | `get_animal(id, opts)` | `GET /public/animals/{id}` |
58
+ | `get_animal_breeds(opts)` | `GET /public/animals/breeds` |
59
+ | `search_orgs(opts)` | `POST /public/orgs/search/{views}` |
60
+ | `get_org(id, opts)` | `GET /public/orgs/{id}` |
61
+ | `get_org_animals(org_id, opts)` | `GET /public/orgs/{id}/animals` |
62
+ | `search_org_animals(org_id, opts)` | `POST /public/orgs/{id}/animals/search/{views}` |
63
+
64
+ ### Nesting Included Data
65
+
66
+ The client can copy each `included` object's attributes into the matching entry under the parent's `relationships`. Set `nest_data` to true to enable this. The `included` section is still returned either way.
67
+
68
+ ```ruby
69
+ client = RescueGroupsV5::Client.new(API_KEY_HERE, nest_data: true)
70
+
71
+ animal = client.search_animals(include: [:orgs])["data"].first
72
+ animal["relationships"]["orgs"]["data"].first["name"] # => "Second Chance Dog Rescue"
73
+ ```
74
+
75
+ ## Request Options
76
+
77
+ ### Views
78
+
79
+ _Search methods only._
80
+
81
+ Views are predefined searches from the API, passed as an array of names. Multiple views are combined (AND).
82
+
83
+ ```ruby
84
+ client.search_animals(views: [:available, :cats, :isspecialneeds])
85
+ ```
86
+
87
+ Animal searches (`search_animals` and `search_org_animals`) default to `[:available]`. To search all animals, including adopted ones, pass an empty array:
88
+
89
+ ```ruby
90
+ client.search_animals # available animals only
91
+ client.search_animals(views: []) # every animal, any status
92
+ ```
93
+
94
+ Org searches have no default view.
95
+
96
+ An unknown view raises `RescueGroupsV5::InvalidViewError` (an `ArgumentError`) before any request is sent.
97
+
98
+ **Animal views:** `available`, `adopted`, `isspecialneeds`, `isneedingfoster`, `urgent`, `iscourtesylisting`, and one view per species: `alpacas`, `birds`, `cats`, `chickens`, `chinchillas`, `cows`, `degus`, `dogs`, `donkeys`, `ducks`, `ferrets`, `fish`, `frogs`, `geckos`, `geese`, `gerbils`, `goats`, `groundhogs`, `hamsters`, `hedgehogs`, `horses`, `iguanas`, `lizards`, `llama`, `mice`, `otters`, `pigs`, `ponies`, `rabbits`, `rats`, `sheep`, `skunks`, `snakes`, `tarantulas`, `tortoises`, `turkeys`, `turtles`
99
+
100
+ Some documented animal views are broken in the API and are not supported. Use a filter instead:
101
+
102
+ ```ruby
103
+ # haspic (has no effect in the API)
104
+ { object: :animals, field_name: :pictureCount, operation: :greater_than_or_equal, criteria: 1 }
105
+
106
+ # guineapigs, hermitcrabs, prairiedogs, sugargliders (always return no results)
107
+ { object: :species, field_name: :plural, criteria: "Guinea Pigs" } # or "Hermit Crabs", "Prairie Dogs", "Sugar Gliders"
108
+ ```
109
+
110
+ The API treats a criteria of `0` as blank, so use `operation: :greater_than_or_equal, criteria: 1` rather than `operation: :greater_than, criteria: 0`.
111
+
112
+ **Org views:** `rescue`. The API's documented `shelter` view is broken (it always returns no results), so use a filter instead:
113
+
114
+ ```ruby
115
+ client.search_orgs(filters: [{ object: :orgs, field_name: :type, criteria: "Shelter" }])
116
+ ```
117
+
118
+ ### Location
119
+
120
+ _Search methods only._
121
+
122
+ Search within a radius of a postal code (US and Canada) or a lat/lon point. The distance is `miles` or `kilometers` and defaults to 10 miles. Results include a `distance` attribute.
123
+
124
+ ```ruby
125
+ client.search_animals(postalcode: "92107", miles: 25)
126
+ client.search_animals(lat: 32.7174, lon: -117.1628, kilometers: 10)
127
+ ```
128
+
129
+ ### Filters
130
+
131
+ _Search methods only._
132
+
133
+ An array of hashes with these keys:
134
+
135
+ - `object`: `:animals` or `:orgs`
136
+ - `field_name`: the object's field
137
+ - `operation`: defaults to `:equals`. One of `:equals`, `:not_equal`, `:less_than`, `:less_than_or_equal`, `:greater_than`, `:greater_than_or_equal`, `:contains`, `:not_contain`, `:blank`, `:not_blank`
138
+ - `criteria`: the value to compare against. An array with `:equals` matches any of the values.
139
+
140
+ Multiple filters are combined (AND).
141
+
142
+ ```ruby
143
+ client.search_animals(filters: [
144
+ { object: :animals, field_name: :breedPrimary, operation: :contains, criteria: "Staffordshire" },
145
+ { object: :animals, field_name: :sizeGroup, criteria: ["Small", "Medium"] },
146
+ { object: :animals, field_name: :birthDate, operation: :greater_than, criteria: "2020-01-01T00:00:00Z" }
147
+ ])
148
+ ```
149
+
150
+ Date criteria must be full ISO 8601 timestamps (`"2020-01-01T00:00:00Z"`). A bare date like `"2020-01-01"` makes the API return a 500 error.
151
+
152
+ ### Fields
153
+
154
+ A hash where each key is an object name and each value is an array of fields to return. Use it to reduce the size of the response. All fields are returned when it is omitted.
155
+
156
+ ```ruby
157
+ client.search_animals(fields: {
158
+ animals: [:name, :ageGroup, :breedPrimary],
159
+ orgs: [:name, :city]
160
+ })
161
+ ```
162
+
163
+ Available fields:
164
+ - [Animals](https://github.com/bwanicur/rescue_groups_v5/blob/master/lib/rescue_groups_v5/fields/animal.rb)
165
+ - [Breeds](https://github.com/bwanicur/rescue_groups_v5/blob/master/lib/rescue_groups_v5/fields/breed.rb)
166
+ - [Colors](https://github.com/bwanicur/rescue_groups_v5/blob/master/lib/rescue_groups_v5/fields/color.rb)
167
+ - [Contacts](https://github.com/bwanicur/rescue_groups_v5/blob/master/lib/rescue_groups_v5/fields/contact.rb)
168
+ - [Orgs](https://github.com/bwanicur/rescue_groups_v5/blob/master/lib/rescue_groups_v5/fields/org.rb)
169
+ - [Statuses](https://github.com/bwanicur/rescue_groups_v5/blob/master/lib/rescue_groups_v5/fields/status.rb)
170
+
171
+ ### Include
172
+
173
+ An array of related objects to return in the `included` section.
174
+
175
+ ```ruby
176
+ client.search_animals(include: [:orgs, :breeds, :pictures])
177
+ ```
178
+
179
+ When `include` is omitted, the API returns its default set of related objects (pictures, breeds, colors, locations, orgs and more), which can make responses large. Pass an empty array to return no related objects:
180
+
181
+ ```ruby
182
+ client.search_animals(include: [])
183
+ ```
184
+
185
+ ### Sort
186
+
187
+ An array of hashes with these keys:
188
+
189
+ - `object`: `:animals` or `:orgs`
190
+ - `sort_value`: the field to sort by
191
+ - `direction`: `:ascending` (default) or `:descending`
192
+
193
+ ```ruby
194
+ # animals by distance, then by name descending
195
+ client.search_animals(
196
+ postalcode: "92107",
197
+ sort: [
198
+ { object: :animals, sort_value: :distance, direction: :ascending },
199
+ { object: :animals, sort_value: :name, direction: :descending }
200
+ ]
201
+ )
202
+ ```
203
+
204
+ ### Page
205
+
206
+ The page number for paginated results. Defaults to 1.
207
+
208
+ ```ruby
209
+ client.search_animals(page: 2)
210
+ ```
211
+
212
+ ### Limit
213
+
214
+ The number of results per page. Defaults to 25; the API maximum is 250.
215
+
216
+ ```ruby
217
+ client.search_animals(limit: 50)
218
+ ```
219
+
220
+ ## Contributing
221
+
222
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bwanicur/rescue_groups_v5.
223
+
224
+ ## License
225
+
226
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RescueGroupsV5
4
+ class Client
5
+ def initialize(api_key, global_opts = {})
6
+ @api_key = api_key
7
+ @global_opts = global_opts
8
+ end
9
+
10
+ def search_animals(opts = {})
11
+ process_response(animal_request.search(opts))
12
+ end
13
+
14
+ def get_animal(id, opts = {})
15
+ process_response(animal_request.find(id, opts))
16
+ end
17
+
18
+ def get_animal_breeds(opts = {})
19
+ process_response(animal_request.get_breeds(opts))
20
+ end
21
+
22
+ def search_orgs(opts = {})
23
+ process_response(org_request.search(opts))
24
+ end
25
+
26
+ def get_org(id, opts = {})
27
+ process_response(org_request.find(id, opts))
28
+ end
29
+
30
+ def get_org_animals(org_id, opts = {})
31
+ process_response(org_request.list_animals(org_id, opts))
32
+ end
33
+
34
+ def search_org_animals(org_id, opts = {})
35
+ process_response(org_request.search_animals(org_id, opts))
36
+ end
37
+
38
+ private
39
+
40
+ def process_response(response)
41
+ Requests::Response.new(response.body, @global_opts[:nest_data]).run
42
+ end
43
+
44
+ def animal_request
45
+ @animal_request ||= Requests::Animal.new(@api_key)
46
+ end
47
+
48
+ def org_request
49
+ @org_request ||= Requests::Org.new(@api_key)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RescueGroupsV5
4
+ module Fields
5
+ module Animal
6
+ FIELDS = %w[
7
+ activityLevel
8
+ adoptedDate
9
+ adoptionFeeString
10
+ adultSexesOk
11
+ ageGroup
12
+ availableDate
13
+ birthDate
14
+ breedPrimary
15
+ breedPrimaryId
16
+ breedSecondary
17
+ breedSecondaryId
18
+ coatLength
19
+ colorDetails
20
+ createdDate
21
+ descriptionHtml
22
+ descriptionText
23
+ distinguishingMarks
24
+ earType
25
+ energyLevel
26
+ exerciseNeeds
27
+ eyeColor
28
+ fenceNeeds
29
+ foundDate
30
+ foundPostalcode
31
+ groomingNeeds
32
+ housetrainedReasonNot
33
+ id
34
+ indoorOutdoor
35
+ isAdoptionPending
36
+ isAltered
37
+ isBirthDateExact
38
+ isBreedMixed
39
+ isCatsOk
40
+ isCourtesyListing
41
+ isCurrentVaccinations
42
+ isDeclawed
43
+ isDogsOk
44
+ isFarmAnimalsOk
45
+ isFound
46
+ isHousetrained
47
+ isKidsOk
48
+ isMicrochipped
49
+ isNeedingFoster
50
+ isSpecialNeeds
51
+ isSponsorable
52
+ isYardRequired
53
+ killDate
54
+ killReason
55
+ name
56
+ newPeopleReaction
57
+ obedienceTraining
58
+ ownerExperience
59
+ pictureCount
60
+ pictureThumbnailUrl
61
+ priority
62
+ qualities
63
+ rescueId
64
+ searchString
65
+ sex
66
+ sheddingLevel
67
+ sizeCurrent
68
+ sizeGroup
69
+ sizePotential
70
+ sizeUOM
71
+ slug
72
+ specialNeedsDetails
73
+ sponsors
74
+ sponsorshipDetails
75
+ sponsorshipMinimum
76
+ summary
77
+ tailType
78
+ trackerimageUrl
79
+ updatedDate
80
+ url
81
+ videoCount
82
+ videoUrlCount
83
+ vocalLevel
84
+ ].freeze
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RescueGroupsV5
4
+ module Fields
5
+ module Breed
6
+ FIELDS = %w[
7
+ id
8
+ name
9
+ ].freeze
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RescueGroupsV5
4
+ module ColorFields
5
+ FIELDS = %w[
6
+ id
7
+ name
8
+ ].freeze
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RescueGroupsV5
4
+ module Fields
5
+ module Contact
6
+ FIELDS = %w[
7
+ email
8
+ firstname
9
+ fullname
10
+ id
11
+ lastname
12
+ name
13
+ phoneCell
14
+ phoneHome
15
+ salutation
16
+ ].freeze
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RescueGroupsV5
4
+ module Fields
5
+ module Org
6
+ FIELDS = %w[
7
+ about
8
+ adoptionProcess
9
+ adoptionUrl
10
+ city
11
+ citystate
12
+ coordinates
13
+ country
14
+ donationUrl
15
+ email
16
+ facebookUrl
17
+ fax
18
+ id
19
+ isCommonapplicationAccepted
20
+ lat
21
+ lon
22
+ meetPets
23
+ name
24
+ phone
25
+ postalcode
26
+ postalcodePlus4
27
+ serveAreas
28
+ services
29
+ sponsorshipUrl
30
+ state
31
+ street
32
+ type
33
+ url
34
+ ].freeze
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RescueGroupsV5
4
+ module StatusFields
5
+ FIELDS = %(
6
+ description
7
+ id
8
+ name
9
+ )
10
+ end
11
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RescueGroupsV5
4
+ module Requests
5
+ class Animal
6
+ PATH = "/animals"
7
+ DEFAULT_VIEWS = [:available].freeze
8
+ # From the per-endpoint view table at https://api.rescuegroups.org/v5/public/docs.
9
+ # Documented views that are broken in the API and therefore excluded:
10
+ # - attending: returns a 500
11
+ # - haspic: has no effect (filter on animals.pictureCount >= 1 instead)
12
+ # - guineapigs, hermitcrabs, prairiedogs, sugargliders: always return 0 results
13
+ # (filter on species.plural, e.g. "Guinea Pigs", instead)
14
+ VIEWS = %w[
15
+ available adopted isspecialneeds isneedingfoster urgent iscourtesylisting
16
+ alpacas birds cats chickens chinchillas cows degus dogs donkeys ducks ferrets fish
17
+ frogs geckos geese gerbils goats groundhogs hamsters hedgehogs horses iguanas
18
+ lizards llama mice otters pigs ponies rabbits rats sheep skunks snakes tarantulas
19
+ tortoises turkeys turtles
20
+ ].freeze
21
+
22
+ def initialize(api_key)
23
+ @api_key = api_key
24
+ end
25
+
26
+ def list(opts = {})
27
+ request.get(PATH, opts)
28
+ end
29
+
30
+ def search(opts = {})
31
+ views = opts.fetch(:views, DEFAULT_VIEWS)
32
+ request.post(Services::SearchPathBuilder.run(PATH, views, VIEWS), opts)
33
+ end
34
+
35
+ def find(id, opts = {})
36
+ request.get("#{PATH}/#{id}", opts)
37
+ end
38
+
39
+ def get_breeds(opts = {})
40
+ request.get("#{PATH}/breeds", opts)
41
+ end
42
+
43
+ private
44
+
45
+ def request
46
+ @request ||= Request.new(@api_key)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RescueGroupsV5
4
+ module Requests
5
+ class Org
6
+ PATH = "/orgs"
7
+ # "shelter" is documented but filters on orgs.type "Shelters" (actual value is "Shelter"),
8
+ # so it always returns 0 results. Filter on orgs.type = "Shelter" instead.
9
+ VIEWS = %w[rescue].freeze
10
+
11
+ def initialize(api_key)
12
+ @api_key = api_key
13
+ end
14
+
15
+ def find(id, opts = {})
16
+ request.get("#{PATH}/#{id}", opts)
17
+ end
18
+
19
+ def list(opts = {})
20
+ request.get(PATH, opts)
21
+ end
22
+
23
+ def search(opts = {})
24
+ request.post(Services::SearchPathBuilder.run(PATH, opts[:views], VIEWS), opts)
25
+ end
26
+
27
+ def list_animals(id, opts = {})
28
+ request.get("#{PATH}/#{id}/animals", opts)
29
+ end
30
+
31
+ def search_animals(id, opts = {})
32
+ views = opts.fetch(:views, Animal::DEFAULT_VIEWS)
33
+ request.post(Services::SearchPathBuilder.run("#{PATH}/#{id}/animals", views, Animal::VIEWS), opts)
34
+ end
35
+
36
+ private
37
+
38
+ def request
39
+ @request ||= Request.new(@api_key)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require_relative "./response"
5
+ require_relative "../services/filter_builder"
6
+ require_relative "../services/sort_builder"
7
+ require_relative "../services/fields_builder"
8
+ require_relative "../services/radius_filter_builder"
9
+ require_relative "../services/search_path_builder"
10
+
11
+ module RescueGroupsV5
12
+ module Requests
13
+ class Request
14
+ URL = "https://api.rescuegroups.org/v5/public"
15
+
16
+ def initialize(api_key)
17
+ @api_key = api_key
18
+ @conn = Faraday.new
19
+ end
20
+
21
+ def get(path, opts = {})
22
+ base_request(verb: :get, path: path, opts: opts)
23
+ end
24
+
25
+ def post(path, opts = {})
26
+ base_request(verb: :post, path: path, opts: opts)
27
+ end
28
+
29
+ private
30
+
31
+ def base_request(verb:, path:, opts: {})
32
+ path += "?#{fields_str(opts[:fields])}" if opts[:fields]
33
+ @conn.send(verb, "#{URL}#{path}") do |req|
34
+ req.headers["Content-Type"] = "application/vnd.api+json"
35
+ req.headers["Authorization"] = @api_key
36
+ req.params["include"] = include_str(opts[:include]) if opts[:include]
37
+ req.params["sort"] = sort_str(opts[:sort]) if opts[:sort]
38
+ req.params["page"] = opts[:page] if opts[:page]
39
+ req.params["limit"] = opts[:limit] if opts[:limit]
40
+ if verb == :post
41
+ body_data = { "data" => {} }
42
+ body_data["data"]["filters"] = filters_data(opts[:filters]) if opts[:filters]
43
+ body_data["data"]["filterRadius"] = filter_radius_data(opts) if radius_data?(opts)
44
+ req.body = body_data.to_json
45
+ end
46
+ end
47
+ end
48
+
49
+ def include_str(includes = [])
50
+ includes.map(&:to_s).join(",")
51
+ end
52
+
53
+ def sort_str(sort_data = {})
54
+ Services::SortBuilder.run(sort_data)
55
+ end
56
+
57
+ def fields_str(fields_data = {})
58
+ Services::FieldsBuilder.run(fields_data)
59
+ end
60
+
61
+ def filter_radius_data(data = {})
62
+ Services::RadiusFilterBuilder.run(data)
63
+ end
64
+
65
+ def radius_data?(opts)
66
+ opts[:postalcode] || (opts[:lat] && opts[:lon])
67
+ end
68
+
69
+ def filters_data(filter_data = {})
70
+ Services::FilterBuilder.run(filter_data)
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module RescueGroupsV5
6
+ module Requests
7
+ class Response
8
+ def initialize(json, nest_data = nil)
9
+ @json = json
10
+ @nest_data = nest_data
11
+ end
12
+
13
+ def run
14
+ data = JSON.parse(@json)
15
+ if @nest_data && data["included"]&.any?
16
+ data["data"].each do |data_hash|
17
+ data_hash.fetch("relationships", {}).each do |_obj_name, rel_hash|
18
+ rel_hash["data"].each do |rel_data_hash|
19
+ rel_data_hash.merge!(get_included_data(data["included"], rel_data_hash))
20
+ end
21
+ end
22
+ end
23
+ end
24
+ data
25
+ end
26
+
27
+ private
28
+
29
+ def get_included_data(included_data, my_hash)
30
+ my_data = included_data.select do |included_hash|
31
+ included_hash["type"] == my_hash.fetch("type") &&
32
+ included_hash["id"] == my_hash.fetch("id")
33
+ end.first
34
+ my_data ? my_data["attributes"] : {}
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RescueGroupsV5
4
+ module Services
5
+ class FieldsBuilder
6
+ def self.run(data)
7
+ fields_strs = []
8
+ data.each do |field_name, attributes|
9
+ fields_strs << "fields[#{field_name}]=#{attributes.join(',')}"
10
+ end
11
+ fields_strs.join("&")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RescueGroupsV5
4
+ module Services
5
+ class FilterBuilder
6
+ OPERATIONS = {
7
+ equals: "equal",
8
+ not_equal: "notequal",
9
+ less_than: "lessthan",
10
+ less_than_or_equal: "lessthanorequal",
11
+ greater_than: "greaterthan",
12
+ greater_than_or_equal: "greaterthanorequal",
13
+ contains: "contains",
14
+ not_contain: "notcontain",
15
+ blank: "blank",
16
+ not_blank: "notblank"
17
+ }.freeze
18
+
19
+ def self.run(filters_data)
20
+ filters_data.map do |hash|
21
+ operation = hash[:operation] || :equals
22
+ {
23
+ fieldName: "#{hash[:object]}.#{hash[:field_name]}",
24
+ operation: translate_operation(operation),
25
+ criteria: hash[:criteria]
26
+ }
27
+ end
28
+ end
29
+
30
+ def self.translate_operation(operation_key)
31
+ OPERATIONS[operation_key.to_sym]
32
+ end
33
+ private_class_method :translate_operation
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RescueGroupsV5
4
+ module Services
5
+ class RadiusFilterBuilder
6
+ DEFAULT_RADIUS_MILES = 10
7
+
8
+ def self.run(data)
9
+ if data[:postalcode]
10
+ { "postalcode" => data[:postalcode] }.merge(distance_info(data))
11
+ elsif data[:lat] && data[:lon]
12
+ { "lat" => data[:lat], "lon" => data[:lon] }.merge(distance_info(data))
13
+ end
14
+ end
15
+
16
+ def self.distance_info(data)
17
+ if data[:kilometers]
18
+ { "kilometers" => data[:kilometers] }
19
+ else
20
+ { "miles" => data[:miles] || DEFAULT_RADIUS_MILES }
21
+ end
22
+ end
23
+ private_class_method :distance_info
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RescueGroupsV5
4
+ module Services
5
+ class SearchPathBuilder
6
+ def self.run(base_path, views, valid_views)
7
+ view_names = Array(views).map(&:to_s)
8
+ invalid = view_names - valid_views
9
+ if invalid.any?
10
+ raise InvalidViewError, "Invalid view(s): #{invalid.join(', ')}. Valid views: #{valid_views.join(', ')}"
11
+ end
12
+
13
+ ["#{base_path}/search", *view_names].join("/")
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RescueGroupsV5
4
+ module Services
5
+ class SortBuilder
6
+ def self.run(sort_data)
7
+ sort_strings = []
8
+ sort_data.each do |hash|
9
+ dir = hash[:direction] == :descending ? "-" : ""
10
+ sort_strings << "#{dir}#{hash[:object]}.#{hash[:sort_value]}"
11
+ end
12
+ sort_strings.join(",")
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RescueGroupsV5
4
+ VERSION = "0.1.1"
5
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rescue_groups_v5/version"
4
+ require "rescue_groups_v5/requests/request"
5
+ require "rescue_groups_v5/requests/response"
6
+ require "rescue_groups_v5/requests/org"
7
+ require "rescue_groups_v5/requests/animal"
8
+ require "rescue_groups_v5/client"
9
+
10
+ module RescueGroupsV5
11
+ class Error < StandardError; end
12
+ class InvalidViewError < ArgumentError; end
13
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "rescue_groups_v5/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "rescue_groups_v5"
9
+ spec.version = RescueGroupsV5::VERSION
10
+ spec.authors = ["Ben Wanicur"]
11
+ spec.email = ["bwanicur@gmail.com"]
12
+
13
+ spec.summary = "Wrapper for Rescue Groups V5 API."
14
+ spec.description = "Wrapper for Rescue Groups V5 API - https://userguide.rescuegroups.org/display/APIDG/v5"
15
+ spec.homepage = "https://github.com/bwanicur/rescue_groups_v5"
16
+ spec.license = "MIT"
17
+
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = spec.homepage
23
+ spec.metadata["changelog_uri"] = spec.homepage
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_dependency "faraday"
39
+ spec.add_dependency "json"
40
+
41
+ spec.add_development_dependency "byebug"
42
+ spec.add_development_dependency "rake", ">= 12.3.3"
43
+ spec.add_development_dependency "rspec", "~> 3.0"
44
+ spec.add_development_dependency "vcr"
45
+ spec.add_development_dependency "webmock", "~> 3.7"
46
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rescue_groups_v5
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Wanicur
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: faraday
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: byebug
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: rake
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 12.3.3
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 12.3.3
68
+ - !ruby/object:Gem::Dependency
69
+ name: rspec
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ - !ruby/object:Gem::Dependency
83
+ name: vcr
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ - !ruby/object:Gem::Dependency
97
+ name: webmock
98
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '3.7'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '3.7'
110
+ description: Wrapper for Rescue Groups V5 API - https://userguide.rescuegroups.org/display/APIDG/v5
111
+ email:
112
+ - bwanicur@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - ".gitignore"
118
+ - ".rspec"
119
+ - Gemfile
120
+ - Gemfile.lock
121
+ - LICENSE.txt
122
+ - README.md
123
+ - lib/rescue_groups_v5.rb
124
+ - lib/rescue_groups_v5/client.rb
125
+ - lib/rescue_groups_v5/fields/animal.rb
126
+ - lib/rescue_groups_v5/fields/breed.rb
127
+ - lib/rescue_groups_v5/fields/color.rb
128
+ - lib/rescue_groups_v5/fields/contact.rb
129
+ - lib/rescue_groups_v5/fields/org.rb
130
+ - lib/rescue_groups_v5/fields/status.rb
131
+ - lib/rescue_groups_v5/requests/animal.rb
132
+ - lib/rescue_groups_v5/requests/org.rb
133
+ - lib/rescue_groups_v5/requests/request.rb
134
+ - lib/rescue_groups_v5/requests/response.rb
135
+ - lib/rescue_groups_v5/services/fields_builder.rb
136
+ - lib/rescue_groups_v5/services/filter_builder.rb
137
+ - lib/rescue_groups_v5/services/radius_filter_builder.rb
138
+ - lib/rescue_groups_v5/services/search_path_builder.rb
139
+ - lib/rescue_groups_v5/services/sort_builder.rb
140
+ - lib/rescue_groups_v5/version.rb
141
+ - rescue_groups_v5.gemspec
142
+ homepage: https://github.com/bwanicur/rescue_groups_v5
143
+ licenses:
144
+ - MIT
145
+ metadata:
146
+ allowed_push_host: https://rubygems.org
147
+ homepage_uri: https://github.com/bwanicur/rescue_groups_v5
148
+ source_code_uri: https://github.com/bwanicur/rescue_groups_v5
149
+ changelog_uri: https://github.com/bwanicur/rescue_groups_v5
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubygems_version: 4.0.5
165
+ specification_version: 4
166
+ summary: Wrapper for Rescue Groups V5 API.
167
+ test_files: []