-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPetFinderWrapper.js
More file actions
112 lines (93 loc) · 3.44 KB
/
PetFinderWrapper.js
File metadata and controls
112 lines (93 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var rp = require('request-promise')
var node_env = process.env.NODE_ENV || "development"
if (node_env === "development") {
var dotenv = require('dotenv');
dotenv.load();
}
module.exports = {
getpets: function (zip, lastoffset) {
var options = {
uri: 'https://api.petfinder.com/pet.find?key=' + process.env.API_KEY + '&animal=cat&location=' + zip + '&offset=' + lastoffset + '&count=100' +'&format=json',
headers: {
'User-Agent': 'Request-Promise'
},
json: true,
transform2xxOnly: false,
transform: function (response) {
var allpets = response.petfinder.pets.pet
var wrapper = {}
var petsarray = []
for (var i = 0, len = allpets.length; i < len; i++) {
var obj = {}
obj["id"] = allpets[i].id.$t
obj["name"] = allpets[i].name.$t
obj["age"] = allpets[i].age.$t
if (allpets[i].sex.$t === "F") {
obj["sex"] = "Female"
}
if (allpets[i].sex.$t === "M") {
obj["sex"] = "Male"
}
obj["breed"] = allpets[i].breeds.breed.$t
obj["shelterid"] = allpets[i].shelterId.$t
obj["description"] = allpets[i].description.$t
if (Object.getOwnPropertyNames(allpets[i].media).length === 0) {
continue
} else {
obj["image"] = allpets[i].media.photos.photo[2].$t
}
obj["address"] = allpets[i].contact.address1.$t
obj["city"] = allpets[i].contact.city.$t
obj["state"] = allpets[i].contact.state.$t
obj["zip"] = allpets[i].contact.zip.$t
obj["phone"] = allpets[i].contact.phone.$t
obj["email"] = allpets[i].contact.email.$t
petsarray.push(obj)
}
wrapper["offset"] = response.petfinder.lastOffset.$t
wrapper["pets"] = petsarray
return wrapper
}
}
return rp(options).promise()
},
getpet: function (petid) {
var options = {
uri: 'https://api.petfinder.com/pet.get?key=' + process.env.API_KEY + '&id=' + petid + '&format=json',
headers: {
'User-Agent': 'Request-Promise'
},
json: true,
transform2xxOnly: false,
transform: function (response) {
if (response.petfinder.header.status.code.$t === '100') {
var pet = {}
pet["id"] = response.petfinder.pet.id.$t
pet["name"] = response.petfinder.pet.name.$t
pet["age"] = response.petfinder.pet.age.$t
if (response.petfinder.pet.sex.$t === "F") {
pet["sex"] = "Female"
}
if (response.petfinder.pet.sex.$t === "M") {
pet["sex"] = "Male"
}
pet["breed"] = response.petfinder.pet.breeds.breed.$t
pet["shelterid"] = response.petfinder.pet.shelterId.$t
pet["description"] = response.petfinder.pet.description.$t
pet["image"] = response.petfinder.pet.media.photos.photo[2].$t
pet["address"] = response.petfinder.pet.contact.address1.$t
pet["city"] = response.petfinder.pet.contact.city.$t
pet["state"] = response.petfinder.pet.contact.state.$t
pet["zip"] = response.petfinder.pet.contact.zip.$t
pet["phone"] = response.petfinder.pet.contact.phone.$t
pet["email"] = response.petfinder.pet.contact.email.$t
pet["code"] = response.petfinder.header.status.code.$t
return pet
} else {
return undefined
}
}
}
return rp(options).promise()
}
}