petfinder_V2 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: dd02de6a3afbd4cb0a38e81db9f708d9048a01464f5a72323882d758e69e29ab
4
+ data.tar.gz: '01809fe6ef68ff6dda3be1c04076ede962b963c6c2efe1afe47a08a822975032'
5
+ SHA512:
6
+ metadata.gz: 97c813fc7527263bfd5644fa0522bd2c5030f5b451891b8611bb72f217b682497b0ec520405d5c03f1d5d20b43d8bc1c658067a8acad42790ebbeb4582db225b
7
+ data.tar.gz: b7bb6499488c91787b9da054a5ca7dea9f23af15901878aed509fd59231611f79498e5242b80d4a89bdd78ee8b1cf5b78f92712dacf2144052137438860c7972
data/.gitignore ADDED
@@ -0,0 +1,12 @@
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
+ TODO.txt
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in petfinder_V2.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ petfinder_V2 (0.1.0)
5
+ faraday
6
+ json
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.6.0)
12
+ public_suffix (>= 2.0.2, < 4.0)
13
+ byebug (11.0.1)
14
+ crack (0.4.3)
15
+ safe_yaml (~> 1.0.0)
16
+ diff-lcs (1.3)
17
+ faraday (0.15.4)
18
+ multipart-post (>= 1.2, < 3)
19
+ hashdiff (0.3.9)
20
+ json (2.2.0)
21
+ multipart-post (2.0.0)
22
+ public_suffix (3.0.3)
23
+ rake (10.5.0)
24
+ rspec (3.8.0)
25
+ rspec-core (~> 3.8.0)
26
+ rspec-expectations (~> 3.8.0)
27
+ rspec-mocks (~> 3.8.0)
28
+ rspec-core (3.8.0)
29
+ rspec-support (~> 3.8.0)
30
+ rspec-expectations (3.8.3)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.8.0)
33
+ rspec-mocks (3.8.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.8.0)
36
+ rspec-support (3.8.0)
37
+ safe_yaml (1.0.5)
38
+ vcr (4.0.0)
39
+ webmock (3.5.1)
40
+ addressable (>= 2.3.6)
41
+ crack (>= 0.3.2)
42
+ hashdiff
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ bundler (~> 2.0)
49
+ byebug
50
+ petfinder_V2!
51
+ rake (~> 10.0)
52
+ rspec (~> 3.0)
53
+ vcr
54
+ webmock
55
+
56
+ BUNDLED WITH
57
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 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,164 @@
1
+ # PetfinderV2
2
+
3
+ Wrapper for the <a href="https://www.petfinder.com/developers/v2/docs/" target="_blank">Petfinder V2 API</a>
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile-
8
+
9
+ ```ruby
10
+ gem 'petfinder_V2'
11
+ ```
12
+
13
+ And then execute-
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as-
18
+
19
+ $ gem install petfinder_V2
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ # configure the client globally
25
+ PetfinderV2::Config.instance.set(client_id: <CLIENT_ID>)
26
+ PetfinderV2::Config.instance.set(client_secret: <CLIENT_SECRET>)
27
+ client = PetfinderV2::Client.new
28
+
29
+ # or per client instance
30
+ client = PetfinderV2::Client.new(CLIENT_ID, CLIENT_SECRET)
31
+
32
+ options = {
33
+ age: [ 'adult' 'senior' ]
34
+ gender: 'female'
35
+ size: [ 'medium' 'large' ]
36
+ }
37
+ result = client.search_animals(options)
38
+ ```
39
+
40
+ ## Methods
41
+
42
+ - `client.search_animals(options)`
43
+ - options- <a href="https://www.petfinder.com/developers/v2/docs/#get-animals" target="_blank">Petfinder V2 Docs</a>
44
+ - returns- collection of `Animal` objects
45
+
46
+ - `client.get_animal(id)`
47
+ - returns- `Animal` object
48
+
49
+ - `client.get_animal_types`
50
+ - returns- collection of `AnimalType` objects
51
+
52
+ - `client.get_animal_type(type_name)`
53
+ - `type_name` is a type of animal (e.g 'dog' 'cat' etc...)
54
+ - more documentation <a href="https://www.petfinder.com/developers/v2/docs/#get-animal-types" target="_blank">here</a>
55
+ - returns `AnimalType` object
56
+
57
+ - `client.get_animal_breeds(type_name)`
58
+ - `type_name` is a type of animal (e.g 'dog' 'cat' etc...)
59
+ - returns- a collection of `AnimalBreed` objects
60
+
61
+ - `client.search_organizations(options)`
62
+ - options- <a href="https://www.petfinder.com/developers/v2/docs/#get-organizations" target="_blank">Petfinder V2 Docs</a>
63
+ - returns- collection of `Organization` objects
64
+
65
+ - `client.get_organization(id)`
66
+ - returns- `Organization` object
67
+
68
+ ## Models
69
+ - Animal
70
+ - id
71
+ - name
72
+ - age
73
+ - coat
74
+ - contact
75
+ - description
76
+ - gender
77
+ - organization_id
78
+ - species
79
+ - size
80
+ - status
81
+ - type
82
+ - declawed
83
+ - house_trained
84
+ - shots_current
85
+ - spayed_neutered
86
+ - special_needs
87
+ - link
88
+ - organization_link
89
+ - breeds --> `Breed` object
90
+ - photos --> `Photos` object
91
+ - contact --> `Contact` object
92
+
93
+ - Breed
94
+ - primary
95
+ - secondary
96
+ - mixed
97
+ - unkown
98
+
99
+ - AnimalType
100
+ - name
101
+ - colors
102
+ - genders
103
+ - link
104
+ - breeds_link
105
+
106
+ - AnimalBreed (returned from `get_animal_breeds`)
107
+ - name
108
+ - type_link
109
+
110
+ - Organization
111
+ - id
112
+ - name
113
+ - email
114
+ - phone
115
+ - url
116
+ - website
117
+ - mission_statement
118
+ - adoption_policy
119
+ - adoption_url
120
+ - link
121
+ - animals_link
122
+ - address --> `Address` object
123
+ - hours --> `OrgHours` object
124
+ - social_media --> `SocialMedia` object
125
+
126
+ - Photos
127
+ - small
128
+ - medium
129
+ - large
130
+ - full
131
+
132
+ - Pagination
133
+ - count_per_page
134
+ - total_count
135
+ - current_page
136
+ - total_pages
137
+ - links
138
+
139
+ - Contact
140
+ - phone
141
+ - email
142
+ - address --> `Address` object
143
+
144
+ - Address
145
+ - address1
146
+ - address2
147
+ - city
148
+ - state
149
+ - postcode
150
+ - country
151
+
152
+ ## Development
153
+
154
+ 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.
155
+
156
+ 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).
157
+
158
+ ## Contributing
159
+
160
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bwanicur/petfinder_V2.
161
+
162
+ ## License
163
+
164
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "petfinder_V2"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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,107 @@
1
+ require 'json'
2
+
3
+ module PetfinderV2
4
+ class Client
5
+ @@access_token = nil
6
+ def self.reset_access_token!
7
+ @@access_token = nil
8
+ end
9
+
10
+ def initialize(client_id = nil, client_secret = nil)
11
+ @client_id = client_id || Config.instance.read(:client_id)
12
+ @client_secret = client_secret || Config.instance.read(:client_secret)
13
+ end
14
+
15
+ def search_animals(opts = {})
16
+ validate_animal_options!(opts)
17
+ response_data = base_request(:get, 'animals', opts)
18
+ Serializers::Animal.process_collection(response_data)
19
+ end
20
+
21
+ def get_animal(id)
22
+ response_data = base_request(:get, "animals/#{id}")
23
+ Serializers::Animal.new(response_data['animal'])
24
+ end
25
+
26
+ def get_animal_types
27
+ response_data = base_request(:get, 'types')
28
+ Serializers::AnimalType.process_collection(response_data)
29
+ end
30
+
31
+ def get_animal_type(name)
32
+ response_data = base_request(:get, "types/#{name}")
33
+ Serializers::AnimalType.new(response_data['type'])
34
+ end
35
+
36
+ def get_animal_breeds(animal_type)
37
+ response_data = base_request(:get, "types/#{animal_type}/breeds")
38
+ Serializers::AnimalBreed.process_collection(response_data)
39
+ end
40
+
41
+ def search_organizations(opts = {})
42
+ validate_organization_options!(opts)
43
+ response_data = base_request(:get, 'organizations', opts)
44
+ Serializers::Organization.process_collection(response_data)
45
+ end
46
+
47
+ def get_organization(id)
48
+ response_data = base_request(:get, "organizations/#{id}")
49
+ Serializers::Organization.new(response_data['organization'])
50
+ end
51
+
52
+ private
53
+
54
+ def base_request(http_verb, endpoint, opts = {})
55
+ res = Requests::Request.new(get_token.token)
56
+ .send(http_verb, endpoint, opts)
57
+ response_data = JSON.parse(res.body)
58
+ handle_errors!(response_data)
59
+ response_data
60
+ end
61
+
62
+ def validate_animal_options!(opts)
63
+ errors = Services::AnimalOptionsValidator.new(opts).run
64
+ invalid_options_error!(errors)
65
+ end
66
+
67
+ def validate_organization_options!(opts)
68
+ errors = Services::AnimalOptionsValidator.new(opts).run
69
+ invalid_options_error!(errors)
70
+ end
71
+
72
+ def invalid_options_error!(errors)
73
+ raise(InvalidRequestOptionsError, errors.join("\n")) unless errors.empty?
74
+ end
75
+
76
+ def handle_errors!(data)
77
+ return if !data['status'] || data['status'] < 400
78
+
79
+ msg = if data['invalid-params']
80
+ data['invalid-params']['message']
81
+ else
82
+ data['detail']
83
+ end
84
+ raise(PetfinderV2::Error, "STATUS: #{data['status']} - #{msg}")
85
+ end
86
+
87
+ def get_token
88
+ access_token_expired? ? refresh_access_token : @@access_token
89
+ end
90
+
91
+ def access_token_expired?
92
+ return true if @@access_token.nil?
93
+
94
+ @@access_token.expires_at < Time.now
95
+ end
96
+
97
+ def refresh_access_token
98
+ res = Requests::AccessTokenRequest.new(
99
+ @client_id,
100
+ @client_secret
101
+ ).get_access_token
102
+ response_data = JSON.parse(res.body)
103
+ handle_errors!(response_data)
104
+ Serializers::AccessToken.new(response_data)
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,19 @@
1
+ require 'singleton'
2
+
3
+ module PetfinderV2
4
+ class Config
5
+ include Singleton
6
+
7
+ def initialize
8
+ @conf = {}
9
+ end
10
+
11
+ def set(key, value)
12
+ @conf[key] = value
13
+ end
14
+
15
+ def read(key)
16
+ key ? (@conf[key.to_s] || @conf[key.to_sym]) : @conf
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ require 'faraday'
2
+
3
+ module PetfinderV2
4
+ module Requests
5
+ class AccessTokenRequest
6
+ URL = 'https://api.petfinder.com/v2/oauth2/token'.freeze
7
+
8
+ def initialize(client_id, client_secret)
9
+ @client_id = client_id
10
+ @client_secret = client_secret
11
+ @conn = Faraday.new
12
+ end
13
+
14
+ def get_access_token
15
+ @conn.post URL,
16
+ client_id: @client_id,
17
+ client_secret: @client_secret,
18
+ grant_type: 'client_credentials'
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,65 @@
1
+ require 'faraday'
2
+
3
+ module PetfinderV2
4
+ module Requests
5
+ class Request
6
+ BASE_URL = 'https://api.petfinder.com/v2'.freeze
7
+
8
+ COLLECTION_OPTS = %i[
9
+ age
10
+ breed
11
+ coat
12
+ color
13
+ gender
14
+ organization
15
+ size
16
+ ].freeze
17
+
18
+ SINGLE_OPTS = %i[
19
+ distance
20
+ limit
21
+ location
22
+ name
23
+ page
24
+ sort
25
+ status
26
+ type
27
+ ].freeze
28
+
29
+ def initialize(access_token)
30
+ @access_token = access_token
31
+ @conn = Faraday.new(BASE_URL)
32
+ end
33
+
34
+ def get(path, opts = {})
35
+ set_connection_headers
36
+ set_connection_get_params(opts)
37
+ @conn.get(path)
38
+ end
39
+
40
+ private
41
+
42
+ def set_connection_headers
43
+ @conn.headers['Content-Type'] = 'application/json'
44
+ @conn.headers['Authorization'] = "Bearer #{@access_token}"
45
+ end
46
+
47
+ def set_connection_get_params(opts)
48
+ SINGLE_OPTS.each do |key|
49
+ @conn.params[key.to_s] = opts[key].to_s if opts[key]
50
+ end
51
+ COLLECTION_OPTS.each do |key|
52
+ @conn.params[key.to_s] = collection_opts(opts[key]) if opts[key]
53
+ end
54
+ end
55
+
56
+ def collection_opts(collection)
57
+ if collection.respond_to?(:each)
58
+ collection.join(',')
59
+ else
60
+ collection.to_s
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,11 @@
1
+ module PetfinderV2
2
+ module Serializers
3
+ class AccessToken
4
+ attr_reader :token, :expires_at
5
+ def initialize(data)
6
+ @token = data['access_token']
7
+ @expires_at = Time.now + data['expires_in'].to_i
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module PetfinderV2
2
+ module Serializers
3
+ class Address
4
+ attr_reader :address1, :address2, :city, :state, :postcode, :country
5
+
6
+ def initialize(data)
7
+ @data = data
8
+ @address1 = data['address1']
9
+ @address2 = data['address2']
10
+ @city = data['city']
11
+ @state = data['state']
12
+ @postcode = data['postcode']
13
+ @country = data['country']
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,92 @@
1
+ require_relative 'pagination'
2
+ require_relative 'contact'
3
+ require_relative 'photos'
4
+
5
+ module PetfinderV2
6
+ module Serializers
7
+ class Breed
8
+ attr_reader :primary, :secondary, :mixed, :unknown
9
+ def initialize(data)
10
+ @primary = data['primary']
11
+ @secondary = data['secondary']
12
+ @mixed = data['mixed']
13
+ @unknown = data['unknown']
14
+ end
15
+ end
16
+
17
+ class Animal
18
+ NESTED_ATTRIBUTE_ATTS = %w[
19
+ declawed
20
+ house_trained
21
+ shots_current
22
+ spayed_neutered
23
+ special_needs
24
+ ].freeze
25
+
26
+ def self.process_collection(data)
27
+ {
28
+ pagination: PetfinderV2::Serializers::Pagination.new(data['pagination']),
29
+ animals: data['animals'].map { |d| new(d) }
30
+ }
31
+ end
32
+
33
+ attr_reader :age,
34
+ :coat,
35
+ :contact,
36
+ :description,
37
+ :gender,
38
+ :id,
39
+ :name,
40
+ :organization_id,
41
+ :species,
42
+ :size,
43
+ :status,
44
+ :type
45
+
46
+ def initialize(data)
47
+ @data = data
48
+ @age = data['age']
49
+ @coat = data['coat']
50
+ @contact = data['contact']
51
+ @description = data['description']
52
+ @gender = data['gender']
53
+ @id = data['id']
54
+ @name = data['name']
55
+ @organization_id = data['organization_id']
56
+ @photos = data['photos']
57
+ @species = data['species']
58
+ @size = data['size']
59
+ @status = data['status']
60
+ @type = data['type']
61
+ end
62
+
63
+ NESTED_ATTRIBUTE_ATTS.each do |att|
64
+ define_method(att.to_sym) { @data['attributes'][att] }
65
+ end
66
+
67
+ def full_response
68
+ @data
69
+ end
70
+
71
+ def breeds
72
+ Breed.new(@data['breeds'])
73
+ end
74
+
75
+ def contact
76
+ Contact.new(@data['contact'])
77
+ end
78
+
79
+ def photos
80
+ Photos.process_collection(@data['photos'])
81
+ end
82
+
83
+ def link
84
+ @data['_links']['self']['href']
85
+ end
86
+
87
+ def organization_link
88
+ @data['_links']['organization']['href']
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,16 @@
1
+ module PetfinderV2
2
+ module Serializers
3
+ class AnimalBreed
4
+ attr_accessor :name, :type_link
5
+
6
+ def self.process_collection(data)
7
+ data['breeds'].map { |b| new(b) }
8
+ end
9
+
10
+ def initialize(data)
11
+ @name = data['name']
12
+ @type_link = data['_links']['type']['href']
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module PetfinderV2
2
+ module Serializers
3
+ class AnimalType
4
+ attr_reader :name, :colors, :genders, :link, :breeds_link
5
+
6
+ def self.process_collection(data)
7
+ data['types'].map { |d| new(d) }
8
+ end
9
+
10
+ def initialize(data)
11
+ @name = data['name']
12
+ @colors = data['colors']
13
+ @genders = data['genders']
14
+ @link = data['_links']['self']['href']
15
+ @breeds_link = data['_links']['breeds']['href']
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'address'
2
+
3
+ module PetfinderV2
4
+ module Serializers
5
+ class Contact
6
+ attr_reader :phone, :email
7
+ def initialize(data)
8
+ @data = data
9
+ @phone = data['phone']
10
+ @email = data['email']
11
+ end
12
+
13
+ def address
14
+ Address.new(@data['address'])
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,83 @@
1
+ require_relative 'address'
2
+ require_relative 'photos'
3
+ require_relative 'pagination'
4
+
5
+ module PetfinderV2
6
+ module Serializers
7
+ class SocialMedia
8
+ attr_reader :facebook, :twitter, :youtube, :instagram, :pinterest
9
+ def initialize(data)
10
+ @facebook = data['facebook']
11
+ @twitter = data['twitter']
12
+ @youtube = data['youtube']
13
+ @instagram = data['instagram']
14
+ @pinterest = data['pinterest']
15
+ end
16
+ end
17
+
18
+ class OrgHours
19
+ attr_reader :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday
20
+ def initialize(data)
21
+ @monday = data['monday']
22
+ @tuesday = data['tuesday']
23
+ @wednesday = data['wednesday']
24
+ @thursday = data['thursday']
25
+ @friday = data['friday']
26
+ @saturday = data['saturday']
27
+ @sunday = data['sunday']
28
+ end
29
+ end
30
+
31
+ class Organization
32
+ def self.process_collection(data)
33
+ {
34
+ pagination: PetfinderV2::Serializers::Pagination.new(data['pagination']),
35
+ organizations: data['organizations'].map { |o| new(o) }
36
+ }
37
+ end
38
+
39
+ attr_reader :id,
40
+ :name,
41
+ :email,
42
+ :phone,
43
+ :url,
44
+ :website,
45
+ :mission_statement,
46
+ :adoption_policy,
47
+ :adoption_url,
48
+ :link,
49
+ :animals_link
50
+
51
+ def initialize(data)
52
+ @data = data
53
+ @id = data['id']
54
+ @name = data['name']
55
+ @email = data['email']
56
+ @phone = data['phone']
57
+ @url = data['url']
58
+ @website = data['website']
59
+ @mission_statement = data['mission_statement']
60
+ @adoption_policy = data['adoption']['policy']
61
+ @adoption_url = data['adoption']['url']
62
+ @link = data['_links']['self']['href']
63
+ @animals_link = data['_links']['animals']['href']
64
+ end
65
+
66
+ def full_response
67
+ @data
68
+ end
69
+
70
+ def address
71
+ Address.new(@data['address'])
72
+ end
73
+
74
+ def hours
75
+ OrgHours.new(@data['hours'])
76
+ end
77
+
78
+ def social_media
79
+ SocialMedia.new(@data['social_media'])
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,19 @@
1
+ module PetfinderV2
2
+ module Serializers
3
+ class Pagination
4
+ attr_reader :count_per_page,
5
+ :total_count,
6
+ :current_page,
7
+ :total_pages,
8
+ :links
9
+
10
+ def initialize(data)
11
+ @count_per_page = data['count_per_page']
12
+ @total_count = data['total_count']
13
+ @current_page = data['current_page']
14
+ @total_pages = data['total_pages']
15
+ @links = data['_links']
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ module PetfinderV2
2
+ module Serializers
3
+ class Photos
4
+ def self.process_collection(collection)
5
+ collection.inject([]) { |mem, photos_hash| mem << new(photos_hash) }
6
+ end
7
+
8
+ attr_reader :small, :medium, :large, :full
9
+ def initialize(data)
10
+ @small = data['small']
11
+ @medium = data['medium']
12
+ @large = data['large']
13
+ @full = data['full']
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,57 @@
1
+ require_relative 'option'
2
+ require_relative 'options_validator'
3
+
4
+ module PetfinderV2
5
+ module Services
6
+ class AnimalOptionsValidator < OptionsValidator
7
+ def my_opts
8
+ {
9
+ limit: Option.new(Integer),
10
+ breed: Option.new(String),
11
+ color: Option.new(String),
12
+ orgnanization: Option.new(String),
13
+
14
+ type: Option.new(String, %w[
15
+ bird
16
+ cat
17
+ dog
18
+ horse
19
+ rabbit
20
+ small_and_furry
21
+ scales_fins_and_other
22
+ ]),
23
+ size: Option.new(String, %w[
24
+ small
25
+ medium
26
+ large
27
+ xlarge
28
+ ]),
29
+ gender: Option.new(String, %w[
30
+ female
31
+ male
32
+ unknown
33
+ ]),
34
+ age: Option.new(String, %w[
35
+ baby
36
+ young
37
+ adult
38
+ senior
39
+ ]),
40
+ coat: Option.new(String, %w[
41
+ short
42
+ medium
43
+ long
44
+ wire
45
+ hairless
46
+ culrly
47
+ ]),
48
+ status: Option.new(String, %w[
49
+ adoptable
50
+ adopted
51
+ found
52
+ ])
53
+ }.freeze
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,33 @@
1
+ module PetfinderV2
2
+ module Services
3
+ class Option
4
+ def initialize(klass, choices = [])
5
+ @klass = klass
6
+ @choices = choices
7
+ end
8
+
9
+ def validate_value(value, key)
10
+ if value.respond_to?(:each)
11
+ validate_collection_val(value, key)
12
+ else
13
+ validate_single_val(value, key)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def validate_single_val(val, key)
20
+ if !val.is_a?(@klass)
21
+ "#{key.capitalize}: #{val} must be a kind of #{@klass}"
22
+ elsif !@choices.empty? && !@choices.include?(val)
23
+ "#{key.capitalize}: #{val} is not an allowed value. " \
24
+ "The allowed values are: #{@choices.join(',')}"
25
+ end
26
+ end
27
+
28
+ def validate_collection_val(collection, key)
29
+ collection.inject([]) { |memo, val| memo << validate_single_val(val, key) }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,36 @@
1
+ module PetfinderV2
2
+ module Services
3
+ class OptionsValidator
4
+ BASE_OPTS_MAP = {
5
+ name: Option.new(String),
6
+ distance: Option.new(Integer),
7
+ location: Option.new(String),
8
+ page: Option.new(Integer),
9
+ sort: Option.new(String)
10
+ }.freeze
11
+
12
+ def initialize(opts)
13
+ @opts = opts
14
+ end
15
+
16
+ def my_opts
17
+ raise 'Abstract Method - Only call in subclasses'
18
+ end
19
+
20
+ def run
21
+ errors = []
22
+ my_opts_map = BASE_OPTS_MAP.merge(my_opts)
23
+ @opts.each do |key, val|
24
+ key = key.to_sym
25
+ if my_opts_map[key].nil?
26
+ errors << "#{key} is not a valid option"
27
+ next
28
+ end
29
+ pf_opt = my_opts_map[key]
30
+ errors << pf_opt.validate_value(val, key)
31
+ end
32
+ errors.flatten.compact
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'option'
2
+ require_relative 'options_validator'
3
+
4
+ module PetfinderV2
5
+ module Services
6
+ class OrganizationOptionsValidator < OptionsValidator
7
+ def my_opts
8
+ {
9
+ state: Option.new(String),
10
+ country: Option.new(String),
11
+ query: Option.new(String)
12
+ }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module PetfinderV2
2
+ VERSION = '0.1.1'.freeze
3
+ end
@@ -0,0 +1,17 @@
1
+ require 'petfinder_V2/version'
2
+ require 'petfinder_V2/client'
3
+ require 'petfinder_V2/config'
4
+ require 'petfinder_V2/services/animal_options_validator'
5
+ require 'petfinder_V2/services/organization_options_validator'
6
+ require 'petfinder_V2/requests/request'
7
+ require 'petfinder_V2/requests/access_token_request'
8
+ require 'petfinder_V2/serializers/access_token'
9
+ require 'petfinder_V2/serializers/animal'
10
+ require 'petfinder_V2/serializers/animal_type'
11
+ require 'petfinder_V2/serializers/animal_breed'
12
+ require 'petfinder_V2/serializers/organization'
13
+
14
+ module PetfinderV2
15
+ class Error < StandardError; end
16
+ class InvalidRequestOptionsError < Error; end
17
+ end
@@ -0,0 +1,34 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'petfinder_V2/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'petfinder_V2'
7
+ spec.version = PetfinderV2::VERSION
8
+ spec.authors = ['Ben Wanicur']
9
+ spec.email = ['bwanicur@gmail.com']
10
+
11
+ spec.homepage = 'https://github.com/bwanicur/petfinder_v2'
12
+ spec.summary = ' Wrapper for the Petfinder V2 API '
13
+ spec.description = ' Wrapper for the Petfinder V2 API '
14
+ spec.license = 'MIT'
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'faraday'
26
+ spec.add_dependency 'json'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 2.0'
29
+ spec.add_development_dependency 'byebug'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'rspec', '~> 3.0'
32
+ spec.add_development_dependency 'vcr'
33
+ spec.add_development_dependency 'webmock'
34
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: petfinder_V2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Wanicur
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-07 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'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: vcr
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: " Wrapper for the Petfinder V2 API "
126
+ email:
127
+ - bwanicur@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - Gemfile
135
+ - Gemfile.lock
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - bin/console
140
+ - bin/setup
141
+ - lib/petfinder_V2.rb
142
+ - lib/petfinder_V2/client.rb
143
+ - lib/petfinder_V2/config.rb
144
+ - lib/petfinder_V2/requests/access_token_request.rb
145
+ - lib/petfinder_V2/requests/request.rb
146
+ - lib/petfinder_V2/serializers/access_token.rb
147
+ - lib/petfinder_V2/serializers/address.rb
148
+ - lib/petfinder_V2/serializers/animal.rb
149
+ - lib/petfinder_V2/serializers/animal_breed.rb
150
+ - lib/petfinder_V2/serializers/animal_type.rb
151
+ - lib/petfinder_V2/serializers/contact.rb
152
+ - lib/petfinder_V2/serializers/organization.rb
153
+ - lib/petfinder_V2/serializers/pagination.rb
154
+ - lib/petfinder_V2/serializers/photos.rb
155
+ - lib/petfinder_V2/services/animal_options_validator.rb
156
+ - lib/petfinder_V2/services/option.rb
157
+ - lib/petfinder_V2/services/options_validator.rb
158
+ - lib/petfinder_V2/services/organization_options_validator.rb
159
+ - lib/petfinder_V2/version.rb
160
+ - petfinder_V2.gemspec
161
+ homepage: https://github.com/bwanicur/petfinder_v2
162
+ licenses:
163
+ - MIT
164
+ metadata: {}
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubygems_version: 3.0.1
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Wrapper for the Petfinder V2 API
184
+ test_files: []