adopt_a_pet 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 85768d77d009bb82f767f155bcbd3cab6d27bcb3
4
+ data.tar.gz: 3cd64da528b83b8dfaa9d727a16728f59d1cf9bf
5
+ SHA512:
6
+ metadata.gz: cd0686a40bdbe77b722b9abafa72eecc35aba9d9b8f61f996f4b43f031224b1dd5f078c7a9cf056a5ed50e6f53122e976396eafc0ca3482d82e027db7e4a9903
7
+ data.tar.gz: f871bb63fec835c047808c590f5c07c6f0b3c86925df6ed109780f201c69e5c92440db785acbeb911d8e622304eccaac87b46f5bdc72af6dd61259a747fd38ba
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,9 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ MethodLength:
5
+ Max: 20
6
+ Style/FrozenStringLiteralComment:
7
+ Enabled: false
8
+ Metrics/LineLength:
9
+ Max: 100
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at dolan.stephen1@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in adopt_a_pet.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Stephen Dolan
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.
@@ -0,0 +1,109 @@
1
+ # AdoptAPet
2
+
3
+ Ruby gem wrapper for the [Adopt-A-Pet API](https://www.adoptapet.com/public/apis/pet_list.html)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'adopt_a_pet'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install adopt_a_pet
20
+
21
+ ## Get Your API Key
22
+
23
+ Get your Adopt-A-Pet API key by logging into a shelter account, then visiting [the API signup page](http://www.adoptapet.com/shelter/portable_pet_list_api).
24
+
25
+ ## Usage
26
+
27
+ ### Instantiate a Client
28
+
29
+ `client = AdoptAPet::Client.new('your_api_key')`
30
+
31
+ ## Examples
32
+
33
+ #### Return a list of pets at shelter '12345'
34
+
35
+ ```
36
+ pet_response = client.pets_at_shelter('12345')
37
+
38
+ # Determine the number of returned pets
39
+ pet_response.count
40
+ # => 50
41
+
42
+ # Get the first pet's name
43
+ pet_response.first.name
44
+ # => "Rocky"
45
+
46
+ # Get the first pet's photo
47
+ pet_response.first.photos.first.dig(:original).url
48
+ # => "https://s3.amazonaws.com/pet-uploads.adoptapet.com/6/f/3/92702234.jpg"
49
+ ```
50
+
51
+ #### Return a list of pets at shelters '12345' and '56789'
52
+ ```
53
+ pet_response = client.pets_at_shelters(['12345', '56789'])
54
+
55
+ # Determine the number of returned pets
56
+ pet_response.count
57
+ # => 50
58
+
59
+ # Get the first pet's name
60
+ pet_response.first.name
61
+ # => "Rocky"
62
+
63
+ # Get the first pet's photo
64
+ pet_response.first.photos.first.dig(:original).url
65
+ # => "https://s3.amazonaws.com/pet-uploads.adoptapet.com/6/f/3/92702434.jpg"
66
+ ```
67
+
68
+ #### Return limited details for the pet with ID '1234'
69
+ ```
70
+ pet = client.limited_pet_details('1234')
71
+
72
+ pet.name
73
+ # => "Rocky"
74
+
75
+ pet.id
76
+ # => "123456"
77
+
78
+ pet.purebred
79
+ # => "0"
80
+ ```
81
+
82
+ #### Get full pet details for the pet with ID '1234'
83
+ ```
84
+ pet = client.pet_details('1234')
85
+
86
+ pet.name
87
+ # => "Rocky"
88
+
89
+ pet.id
90
+ # => "123456"
91
+
92
+ pet.housetrained
93
+ # => "1"
94
+ ```
95
+
96
+ ## Development
97
+
98
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
99
+
100
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
101
+
102
+ ## Contributing
103
+
104
+ Bug reports and pull requests are welcome on GitHub at https://gitlab.com/stephendolan/adopt_a_pet. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
105
+
106
+
107
+ ## License
108
+
109
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'adopt_a_pet/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'adopt_a_pet'
8
+ spec.version = AdoptAPet::VERSION
9
+ spec.authors = ['Stephen Dolan']
10
+ spec.email = ['stephen@stephencodes.com']
11
+
12
+ spec.summary = 'Interface with the Adopt-A-Pet API'
13
+ spec.description = 'Interface with the Adopt-A-Pet API'
14
+ spec.homepage = 'https://gitlab.com/stephendolan/adopt_a_pet_api'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'faraday', '~> 0.13.1'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.14'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.7'
29
+ spec.add_development_dependency 'pry', '~> 0.11.3'
30
+ spec.add_development_dependency 'rb-readline', '~> 0.5.3'
31
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'adopt_a_pet'
5
+
6
+ require 'pry'
7
+ Pry.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,20 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ require 'adopt_a_pet/version'
5
+ require 'adopt_a_pet/client'
6
+ require 'adopt_a_pet/pet'
7
+
8
+ # Top-level module for the Adopt-A-Pet API Wrapper
9
+ module AdoptAPet
10
+ class Error < StandardError; end
11
+
12
+ class << self
13
+ attr_accessor :api_key, :api_secret
14
+ end
15
+
16
+ def self.configure
17
+ yield self
18
+ true
19
+ end
20
+ end
@@ -0,0 +1,206 @@
1
+ module AdoptAPet
2
+ # Class for interfacing with the Adopt-A-Pet API
3
+ # @author Stephen Dolan
4
+ #
5
+ # @!attribute api_key
6
+ # @return [String] the user's API key to use in connections
7
+ # @!attribute conn
8
+ # @return [Faraday::Connection] a Faraday object to make requests
9
+ class Client
10
+ attr_accessor :api_key, :conn
11
+
12
+ # Create a single instance of a Pet Photo
13
+ # @author Stephen Dolan
14
+ #
15
+ # @param [String] api_key the API key to authenticate with
16
+ #
17
+ # @return [void]
18
+ #
19
+ # @example Initialize a new Adopt-A-Pet Client
20
+ # client = AdoptAPet::Client.new('my_api_key')
21
+ # pets = client.pets_at_shelter('12345')
22
+ def initialize(api_key = AdoptAPet.api_key)
23
+ raise(AdoptAPet::Error, 'API key is required.') unless api_key
24
+
25
+ self.api_key = api_key
26
+ self.conn = setup_connection
27
+ end
28
+
29
+ # Retrieves all pets at a shelter
30
+ # @author Stephen Dolan
31
+ #
32
+ # @param [Integer] shelter_id the ID of the shelter to list pets at
33
+ # @param [Hash] opts the options to include with the request
34
+ # @option opts [String] :start_number (1) where to start results
35
+ # @option opts [String] :end_number (50) where to end results
36
+ #
37
+ # @return [Array<Pet>] a collection of Pets
38
+ #
39
+ # @example
40
+ # client = AdoptAPet::Client.new('my_key')
41
+ # pets = client.pets_at_shelter(12345)
42
+ # my_pet_name = pets.first.name
43
+ def pets_at_shelter(shelter_id, options = {})
44
+ query = options.merge(shelter_id: shelter_id)
45
+ response = perform_get('/search/pets_at_shelter', query)
46
+ Pet.multiple(response)
47
+ end
48
+
49
+ # Retrieves all pets at multiple shelters
50
+ # @author Stephen Dolan
51
+ #
52
+ # @param [Array<Integer>] shelter_ids set of shelter IDs to search for pets at
53
+ # @param [Hash] opts the options to include with the request
54
+ # @option opts [String] :start_number (1) where to start results
55
+ # @option opts [String] :end_number (50) where to end results
56
+ #
57
+ # @return [Array<Pet>] a collection of Pets
58
+ #
59
+ # @example
60
+ # client = AdoptAPet::Client.new('my_key')
61
+ # pets = client.pets_at_shelter(12345, end_number: 1000)
62
+ # my_pet_name = pets.first.name
63
+ def pets_at_shelters(shelter_ids, options = {})
64
+ query = options.merge(shelter_id: shelter_ids)
65
+ response = perform_get('/search/pets_at_shelters', query)
66
+ Pet.multiple(response)
67
+ end
68
+
69
+ # Retrieves a subset of the total pet data for a single pet
70
+ # @author Stephen Dolan
71
+ #
72
+ # @param [Integer] pet_id the ID of a single pet
73
+ #
74
+ # @return [Pet] a Pet object
75
+ #
76
+ # @example
77
+ # client = AdoptAPet::Client.new('my_key')
78
+ # pet = client.limited_pet_details(123456)
79
+ # my_pet_special_needs = pet.special_needs
80
+ def limited_pet_details(pet_id)
81
+ query = { pet_id: pet_id }
82
+ response = perform_get('/search/limited_pet_details', query)
83
+ Pet.new(response.dig('pet'))
84
+ end
85
+
86
+ # Retrieves a complete data set for a single pet
87
+ # @author Stephen Dolan
88
+ #
89
+ # @param [Integer] pet_id the ID of a single pet
90
+ #
91
+ # @return [Pet] a Pet object
92
+ #
93
+ # @example
94
+ # client = AdoptAPet::Client.new('my_key')
95
+ # pet = client.pet_details(123456)
96
+ # my_pet_housetrained = pet.housetrained
97
+ def pet_details(pet_id)
98
+ query = { pet_id: pet_id }
99
+ response = perform_get('/search/pet_details', query)
100
+ Pet.new(response.dig('pet'))
101
+ end
102
+
103
+ private
104
+
105
+ # Instantiate a Faraday connector with a base URL and adapter
106
+ # @author stephen dolan
107
+ #
108
+ # @return [faraday::connection] a faraday object to make requests
109
+ def setup_connection
110
+ # Set up a flexible connection to append the URI to and make requests with
111
+ Faraday.new(url: 'https://api.adoptapet.com') do |faraday|
112
+ # Include to log responses to STDOUT
113
+ faraday.response :logger
114
+
115
+ # Don't encode identical URL paramaeters with []
116
+ faraday.options.params_encoder = Faraday::FlatParamsEncoder
117
+
118
+ # Use the default Net::HTTP adapter
119
+ faraday.adapter Faraday.default_adapter
120
+ end
121
+ end
122
+
123
+ # Helper method to fetch data from the Adopt-A-Pet API endpoint
124
+ # @author Stephen Dolan
125
+ #
126
+ # @param [String] the URI to make the GET request to
127
+ # @param [Hash] opts the options to include with the request
128
+ # @option opts [String] :start_number (1) where to start results
129
+ # @option opts [String] :end_number (50) where to end results
130
+ #
131
+ # @return [Hash] an Adopt-A-Pet JSON API response parsed to a Ruby Hash
132
+ def perform_get(uri, options = {})
133
+ # Always fetch the output in JSON format
134
+ options = options.merge(output: :json)
135
+
136
+ # Always include the API key
137
+ options = options.merge(key: self.api_key)
138
+
139
+ # Always set the API version to version 1
140
+ options = options.merge(v: 1)
141
+
142
+ # Make the request
143
+ response = self.conn.get(uri, options)
144
+
145
+ # Validate the body and transform into a Ruby object
146
+ process_json_response(response.body)
147
+ end
148
+
149
+ # Parses a JSON body into a Ruby object if it is a valid response
150
+ # @author Stephen Dolan
151
+ #
152
+ # @param [String] the JSON response body from an Adopt-A-Pet API request
153
+ #
154
+ # @return [Hash] an Adopt-A-Pet JSON API response parsed to a Ruby Hash
155
+ def process_json_response(response_body)
156
+ # Parse the response into a Ruby object
157
+ response_object = JSON.parse(response_body)
158
+
159
+ # Return the Ruby object if the response was valid
160
+ return response_object if valid_json_response(response_object)
161
+ end
162
+
163
+ # Ensure that the JSON HTTP response received is valid
164
+ # @author Stephen Dolan
165
+ #
166
+ # @param [Hash] a Ruby hash representing an Adopt-A-Pet API response
167
+ #
168
+ # @raise [Exception] if the response is not valid, throw an exception
169
+ #
170
+ # @return [true] always return true if the method completes
171
+ def valid_json_response(response_object)
172
+ # Grab the status from the response
173
+ response_status = response_object.dig('status')
174
+
175
+ # If we receive a status 'ok', make sure there are no exceptions
176
+ # If we receive a status 'fail', raise an exception
177
+ # If we receive anything else, raise an exception
178
+ if response_status == 'ok'
179
+ check_ok_response(response_object)
180
+ elsif response_status == 'fail'
181
+ puts response_object
182
+ raise 'Bad request. Response returned status "fail".'
183
+ else
184
+ raise 'Bad request. Invalid response status returned.'
185
+ end
186
+
187
+ # Return true if all validations pass
188
+ true
189
+ end
190
+
191
+ # Verify that there was no issue with an 'ok' response
192
+ # @author Stephen Dolan
193
+ #
194
+ # @param [Hash] a Ruby hash representing an Adopt-A-Pet API response
195
+ #
196
+ # @raise [Exception] if the response is not valid, throw an exception
197
+ #
198
+ # @return [true] always return true if there is no exception
199
+ def check_ok_response(response_object)
200
+ return true unless response_object.key? 'exception'
201
+
202
+ exception_message = response_object.dig('exception', 'details')
203
+ raise "Bad request. Exception occurred: #{exception_message}"
204
+ end
205
+ end
206
+ end
@@ -0,0 +1,268 @@
1
+ # Namespace for classes and modules that handle requests to the Adopt-A-Pet API
2
+ module AdoptAPet
3
+ # Base class for Adopt-A-Pet Pet objects
4
+ # @author Stephen Dolan
5
+ #
6
+ # @!attribute id
7
+ # @return [String] the AdoptAPet identifier for the pet
8
+ # @!attribute age
9
+ # @return [String] a description of the age of the pet
10
+ # @!attribute name
11
+ # @return [String] the name of the pet
12
+ # @!attribute size
13
+ # @return [String] a description of the size of the pet
14
+ # @!attribute species
15
+ # @return [String] a description of the pet's species
16
+ # @!attribute primary_breed
17
+ # @return [String] a description of the pet's primary breed
18
+ # @!attribute secondary_breed
19
+ # @return [String] a description of the pet's secondary breed
20
+ # @!attribute sex
21
+ # @return [String] the pet's gender
22
+ # @!attribute state_code
23
+ # @return [String] the state where the pet resides
24
+ # @!attribute city
25
+ # @return [String] the city where the pet resides
26
+ # @!attribute details_url
27
+ # @return [String] a link to the details page for the pet on Adopt-A-Pet
28
+ # @!attribute photos
29
+ # @return [Array<Photo>] a set of photos for the pet
30
+ # @!attribute color
31
+ # @return [String] a description of the color of the pet
32
+ # @!attribute description
33
+ # @return [String] a description of the pet
34
+ # @!attribute act_quickly
35
+ # @return [String] a string denoting whether action is required quickly for adoption
36
+ # @!attribute special_needs
37
+ # @return [String] a string denoting whether or not the pet has special needs
38
+ # @!attribute good_with_dogs
39
+ # @return [String] a string denoting whether or not the pet is good with dogs
40
+ # @!attribute purebred
41
+ # @return [String] a string denoting whether or not the pet is purebred
42
+ # @!attribute good_with_kids
43
+ # @return [String] a string denoting whether or not the pet is good with kids
44
+ # @!attribute declawed
45
+ # @return [String] a string denoting whether or not the pet is declawed
46
+ # @!attribute spayed_neutered
47
+ # @return [String] a string denoting whether or not the pet is fixed
48
+ # @!attribute housetrained
49
+ # @return [String] a string denoting whether or not the pet is housetrained
50
+ # @!attribute shots_current
51
+ # @return [String] a string denoting whether or not the pet is current with their shots
52
+ class Pet
53
+ attr_accessor :id,
54
+ :age,
55
+ :name,
56
+ :size,
57
+ :species,
58
+ :primary_breed,
59
+ :secondary_breed,
60
+ :sex,
61
+ :state_code,
62
+ :city,
63
+ :details_url,
64
+ :photos,
65
+ :act_quickly,
66
+ :color,
67
+ :special_needs,
68
+ :purebred,
69
+ :description,
70
+ :good_with_dogs,
71
+ :declawed,
72
+ :spayed_neutered,
73
+ :housetrained,
74
+ :shots_current,
75
+ :good_with_kids
76
+
77
+ # Create a single instance of a Pet
78
+ # @author Stephen Dolan
79
+ #
80
+ # @param [Hash] object a JSON response object from the Adopt-A-Pet API
81
+ #
82
+ # @return [Pet] a new Pet
83
+ def initialize(object) # rubocop:disable all
84
+ extract(object, %w[pet_id], 'id')
85
+ extract(object, %w[sex], 'sex')
86
+ extract(object, %w[size], 'size')
87
+ extract(object, %w[species], 'species')
88
+ extract(object, %w[act_quickly], 'act_quickly')
89
+ extract(object, %w[color], 'color')
90
+ extract(object, %w[special_needs], 'special_needs')
91
+ extract(object, %w[purebred], 'purebred')
92
+ extract(object, %w[description], 'description')
93
+ extract(object, %w[good_with_dogs], 'good_with_dogs')
94
+ extract(object, %w[declawed], 'declawed')
95
+ extract(object, %w[spayed_neutered], 'spayed_neutered')
96
+ extract(object, %w[housetrained], 'housetrained')
97
+ extract(object, %w[shots_current], 'shots_current')
98
+ extract(object, %w[good_with_kids], 'good_with_kids')
99
+ extract(object, %w[primary_breed], 'primary_breed')
100
+ extract(object, %w[secondary_breed], 'secondary_breed')
101
+ extract(object, %w[state_code], 'addr_state_code')
102
+ extract(object, %w[city], 'addr_city')
103
+ extract(object, %w[name pet_name], 'name')
104
+ extract(object, %w[details_url pet_details_url], 'details_url')
105
+ self.photos = ResponseHelper.extract_photos(object)
106
+ end
107
+
108
+ # Create multiple instances of Pet with one response
109
+ # @author Stephen Dolan
110
+ #
111
+ # @param [Hash] object a JSON response object from the Adopt-A-Pet API
112
+ #
113
+ # @return [Array<Pet>] Initialize a collection of Pets
114
+ def self.multiple(object)
115
+ pets = []
116
+
117
+ object.dig('pets').each do |pet|
118
+ pets = pets.push(Pet.new(pet))
119
+ end
120
+
121
+ pets
122
+ end
123
+
124
+ # Extract a value from a hash, and store it in an object key
125
+ # @author Stephen Dolan
126
+ #
127
+ # @param [Hash] object a JSON response object from the Adopt-A-Pet API
128
+ # @param [Array<String>] from_keys an array of keys to try and extract a value from
129
+ # @param [String] to_key the name of the key to store the value in on the Pet
130
+ #
131
+ # @return [void]
132
+ def extract(object, from_keys, to_key)
133
+ response_item = nil
134
+
135
+ # For each source key:
136
+ # - Search for a value in the object
137
+ from_keys&.each do |source|
138
+ response_item = object.dig(source) unless object.dig(source).nil?
139
+ end
140
+
141
+ # Don't set the key if we couldn't find a value
142
+ return if response_item.nil?
143
+
144
+ # Set the Pet attribute at to_key to the object value
145
+ send("#{to_key}=", response_item)
146
+ end
147
+
148
+ # Class for Adopt-A-Pet Pet API response object helpers
149
+ # @author Stephen Dolan
150
+ class ResponseHelper
151
+ # Extracts photos from a hash and pushes them into the Pet's photos
152
+ # @author Stephen Dolan
153
+ #
154
+ # @param [Hash] object a JSON response object from the Adopt-A-Pet API
155
+ #
156
+ # @return [Array<Pet::Photo>] an array of Pet Photos
157
+ def self.extract_photos(object)
158
+ photos = []
159
+
160
+ # Get the various photos that could be returned for a pet
161
+ photos = get_results_photo(object, photos)
162
+ photos = get_large_results_photo(object, photos)
163
+ get_other_photos(object, photos)
164
+ end
165
+
166
+ # Pull a results photo, if it exists in the API return object
167
+ # @author Stephen Dolan
168
+ #
169
+ # @param [Hash] object a JSON response object from the Adopt-A-Pet API
170
+ # @param [Array<Pet::Photo>] photos the current collection of Pet Photos
171
+ #
172
+ # @return [Array<Pet::Photo>] a (possibly expanded) array of Pet Photos
173
+ def self.get_results_photo(object, photos)
174
+ unless object.dig('results_photo_url').nil?
175
+ photos = photos.push(
176
+ original: Photo.new(
177
+ object.dig('results_photo_height'),
178
+ object.dig('results_photo_width'),
179
+ object.dig('results_photo_url')
180
+ )
181
+ )
182
+ end
183
+
184
+ photos
185
+ end
186
+
187
+ # Pull a large results photo, if it exists in the API return object
188
+ # @author Stephen Dolan
189
+ #
190
+ # @param [Hash] object a JSON response object from the Adopt-A-Pet API
191
+ # @param [Array<Pet::Photo>] photos the current collection of Pet Photos
192
+ #
193
+ # @return [Array<Pet::Photo>] a (possibly expanded) array of Pet Photos
194
+ def self.get_large_results_photo(object, photos)
195
+ unless object.dig('large_results_photo_url').nil?
196
+ photos = photos.push(
197
+ original: Photo.new(
198
+ object.dig('large_results_photo_height'),
199
+ object.dig('large_results_photo_width'),
200
+ object.dig('large_results_photo_url')
201
+ )
202
+ )
203
+ end
204
+
205
+ photos
206
+ end
207
+
208
+ # Pull any additional photos, if they exists in the API return object
209
+ # @author Stephen Dolan
210
+ #
211
+ # @param [Hash] object a JSON response object from the Adopt-A-Pet API
212
+ # @param [Array<Pet::Photo>] photos the current collection of Pet Photos
213
+ #
214
+ # @return [Array<Pet::Photo>] a (possibly expanded) array of Pet Photos
215
+ def self.get_other_photos(object, photos)
216
+ if (image_objects = object.dig('images'))
217
+ image_objects.each do |image|
218
+ photos = photos.push(
219
+ original: Photo.new(
220
+ image.dig('original_height'),
221
+ image.dig('original_width'),
222
+ image.dig('original_url')
223
+ ),
224
+ thumbnail: Photo.new(
225
+ image.dig('thumbnail_height'),
226
+ image.dig('thumbnail_width'),
227
+ image.dig('thumbnail_url')
228
+ )
229
+ )
230
+ end
231
+ end
232
+
233
+ photos
234
+ end
235
+ end
236
+
237
+ # Class for Adopt-A-Pet Pet Photos
238
+ # @author Stephen Dolan
239
+ #
240
+ # @!attribute height
241
+ # @return [Integer] the height of the Photo
242
+ # @!attribute width
243
+ # @return [Integer] the width of the Photo
244
+ # @!attribute url
245
+ # @return [String] a URL where you can view the Photo
246
+ class Photo
247
+ attr_accessor :height, :width, :url
248
+
249
+ # Create a single instance of a Pet Photo
250
+ # @author Stephen Dolan
251
+ #
252
+ # @param [Integer] height the height of the photo
253
+ # @param [Integer] width the width of the Photo
254
+ # @param [String] url a URL where you can view the photo
255
+ #
256
+ # @return [Pet::Photo] a new Pet Photo
257
+ #
258
+ # @example Create a new Photo
259
+ # my_photo = Pet::Photo.new(100, 100, 'images.com/my_image')
260
+ # height = my_photo.height
261
+ def initialize(height, width, url)
262
+ self.height = height
263
+ self.width = width
264
+ self.url = url
265
+ end
266
+ end
267
+ end
268
+ end
@@ -0,0 +1,3 @@
1
+ module AdoptAPet
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adopt_a_pet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Dolan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.13.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.13.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.11.3
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.11.3
83
+ - !ruby/object:Gem::Dependency
84
+ name: rb-readline
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.5.3
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.5.3
97
+ description: Interface with the Adopt-A-Pet API
98
+ email:
99
+ - stephen@stephencodes.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".rubocop.yml"
107
+ - ".travis.yml"
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - adopt_a_pet.gemspec
114
+ - bin/console
115
+ - bin/setup
116
+ - lib/adopt_a_pet.rb
117
+ - lib/adopt_a_pet/client.rb
118
+ - lib/adopt_a_pet/pet.rb
119
+ - lib/adopt_a_pet/version.rb
120
+ homepage: https://gitlab.com/stephendolan/adopt_a_pet_api
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.6.13
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Interface with the Adopt-A-Pet API
144
+ test_files: []