PetSearch 0.0.7.9 → 0.0.8.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NmQ0ODUyOGNiY2I5OGEwMzdkZTg2OTVlMGUwNmY3ZjBmNjU1MTdmMw==
4
+ OGUwODQ5YTVhOTNmZjViOGEwYzYzZTZkNDI5ZWVhM2JkZDNjNWZiYQ==
5
5
  data.tar.gz: !binary |-
6
- ODY1MzU5YzQ2OGI0N2E0OTg0NTU5ZTczMjFlZGMwYWZmMmRkMWVhMQ==
6
+ NWFiOWUzYjVmNzFiYzU1ZDVjYTllNzc1NTlmZTcyZjRjZjlhNjViOA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZTcxM2QwZDRkYzc0YTRjMzhmMDY3NWJmYTdmNzc3YmViMDNjMWZkODE3ODE2
10
- ZTIzODliNzU2ZGYxOWU4NzJlNDE0ZTQyNzk4MGU1NGQ3ODY2OGJjZWRhMGVm
11
- YTkzZWE2MzEzZmNlNjI1OGU1Nzc0YTE4YjNlMWMyODQxMjJhMTQ=
9
+ ODk1MTg5Yjg3OTZmYmU4MjE2NDAwMjgyMTc3NzUxYWVhMjljMmQzYzJiN2Yz
10
+ YjY4OWFjZDNiMTMyNzIxZDRkYzQ0NjExZGE0NzEyZTljNzFjMDQ1MTg4NTA2
11
+ OTliMWU0OTA1MmJiYmU4OTEyM2IxYjY0YzcxMTUwNDJmZTY5NTQ=
12
12
  data.tar.gz: !binary |-
13
- NmQ5ZDdlMGY5ZjUwOTE5ZTA0NjBjZmY2ZjVhYjQ4NWY2NGQ1MDcwYmQxM2Mz
14
- NDMxNjQxNjZmMTFkZjY3Nzg1YWYwZWFjNzdkOWI1NGNhOTAyYzA4NGY1YmRh
15
- MjVjMmQwMGNmMDlmYTU1MzgyNzBkM2Y0ZjNmY2YxMWM4NTBjMDY=
13
+ MmY4YjBlMWFkYmQyODYwMWE2ZjhmMGZlY2UxYjc5ZmM5NjUzMGZkOTRmNDI4
14
+ OTNhMTUyYTRjNjhkODkyMDIyZWQ5ZGJkZjNjNmYyZjc3MGM5YTU2NDM5NzEw
15
+ ZWRjNmU4ZjgwOThiYjlkZjA1YWYxYTAxNDE3YjVmYmRkZDllN2I=
@@ -8,7 +8,7 @@ require 'petsearch/pet'
8
8
 
9
9
 
10
10
  class PetSearch
11
- attr_accessor :dog, :cat, :bird, :reptile, :horse, :pig, :barnyard, :smallfurry
11
+ attr_accessor :dog, :cat, :bird, :reptile, :horse, :pig, :barnyard, :smallfurry, :api_key, :api_secret
12
12
  def initialize
13
13
  @dog = {:name => 'dog', :breeds => []}
14
14
  @cat = {:name => 'cat', :breeds => []}
@@ -20,27 +20,34 @@ class PetSearch
20
20
  @smallfurry = {:name => 'smallfurry', :breeds => []}
21
21
  end
22
22
 
23
-
24
23
  def self.init_options(*args)
25
24
  @@options = Hash.new
26
25
  begin
27
26
  @@options[:breeds] = args.grep(/[Bb]reeds/).first.downcase.match(/(?<=breeds=).*/)[0].split(',') if args.grep(/[Bb]reeds/).first
28
27
  @@options[:animal] = args.grep(/[Dd]og|[Cc]at|[Bb]ird|[Rr]eptile|[Hh]orse|[Pp]ig|[Bb]arnyard|[Ss]mallfurry/).first
29
28
  @@options[:age] = args.grep(/[Bb]aby|[Yy]oung|[Aa]dult|[Ss]enior/).first
30
- @@options[:location] = args.grep(/[1..9]/).first
29
+ @@options[:location] = args.grep(/\d{5}/).first.strip
31
30
  @@options[:sex] = args.grep(/[Ss]ex/).first.downcase.match(/(?<=sex=).*/)[0].upcase if args.grep(/[Ss]ex/).first
32
- @@options[:size] = args.grep(/[Ss]m|[Mm]dm|[Ll]rg|[Xx]l/).first.downcase if args.grep(/[Ss]m|[Mm]dm|[Ll]rg|[Xx]l/).first
33
- case (@@options[:size])
34
- when 'sm'
35
- @@options[:size] = 'S'
36
- when 'mdm'
37
- @@options[:size] = 'M'
38
- when 'lrg'
39
- @@options[:size] = 'L'
40
- when 'xl'
41
- @@options[:size] = 'XL'
42
- end
43
- @@options[:random] = args.grep(/[Tt]rue|[Ff]alse/).first
31
+
32
+ if (args.grep(/[Ss]m|[Mm]dm|[Ll]rg|[Xx]l/).length == 2)
33
+ @@options[:size] = args.grep(/[Ss]m|[Mm]dm|[Ll]rg|[Xx]l/)[1].downcase
34
+ elsif (args.grep(/[Ss]m|[Mm]dm|[Ll]rg|[Xx]l/).length == 1)
35
+ @@options[:size] = args.grep(/[Ss]m|[Mm]dm|[Ll]rg|[Xx]l/).first.downcase
36
+ else
37
+ @@options[:size]=nil
38
+ end
39
+
40
+ case (@@options[:size])
41
+ when 'sm'
42
+ @@options[:size] = 'S'
43
+ when 'mdm'
44
+ @@options[:size] = 'M'
45
+ when 'lrg'
46
+ @@options[:size] = 'L'
47
+ when 'xl'
48
+ @@options[:size] = 'XL'
49
+ end
50
+ @@options[:random] = args.grep(/[Tt]rue|[Ff]alse/).first
44
51
  @@options[:offset] = args.grep(/[Oo]ff/).first.downcase.match(/(?<=off).*/)[0] if args.grep(/[Oo]ff/).first
45
52
  @@options[:count] = args.grep(/[Cc]t/).first.downcase.match(/(?<=ct).*/)[0] if args.grep(/[Cc]t/).first
46
53
  rescue NoMethodError
@@ -57,10 +64,6 @@ class PetSearch
57
64
 
58
65
  end
59
66
 
60
- def self.get_token
61
- PetClient.get_token
62
- end
63
-
64
67
 
65
68
  end
66
69
 
@@ -28,7 +28,7 @@ class Pet
28
28
  def self.pets
29
29
  @@pets
30
30
  end
31
-
31
+ #(?<=\&)(\d{5})(?=\&)
32
32
  ##### RAILS ONLY - FOR SEARCHING WITH URLS
33
33
  def self.make_search(options)
34
34
  animal = options.match(/animal=(.*?)&/).nil? ? "ERROR" : options.match(/animal=(.*?)&/)[1]
@@ -37,8 +37,15 @@ class Pet
37
37
  offset = options.match(/&off(.*?)&/).nil? ? "" : options.match(/&off(.*?)&/)[1]
38
38
  count = options.match(/&ct(.*?)&/).nil? ? "" : options.match(/&ct(.*?)&/)[1]
39
39
  sex = options.match(/&sex=(.*?)&/).nil? ? "" : options.match(/&sex=(.*?)&/)[1]
40
- location = options.match(/((?:\d\d+)+)/).nil? ? "" : options.match(/((?:\d\d+)+)/)[1]
41
- PetSearch.init_options("#{animal}", "#{location}", "ct#{count}", "#{sex}", "off#{offset}")
40
+ location = options.match(/(?<=\&)(\d{5})(?=\&)/).nil? ? "" : options.match(/(?<=\&)(\d{5})(?=\&)/)[1]
41
+ if options.match(/(?=)([Ss]m|[Mm]dm|[Ll]rg|[Xx]l)/).nil?
42
+ size = ""
43
+ elsif options.match(/(?=)([Ss]m|[Mm]dm|[Ll]rg|[Xx]l)/).length > 3
44
+ size = options.match(/(?=)([Ss]m|[Mm]dm|[Ll]rg|[Xx]l)/)[2]
45
+ else
46
+ size = options.match(/(?=)([Ss]m|[Mm]dm|[Ll]rg|[Xx]l)/)[1]
47
+ end
48
+ PetSearch.init_options("#{animal}", "#{location}", "ct#{count}", "#{size}", "#{sex}", "off#{offset}")
42
49
  PetSearch.options[:breeds] = breeds.nil? ? [] : breeds
43
50
  result = PetSearch.search
44
51
  make_pet(result)
@@ -46,21 +53,36 @@ class Pet
46
53
 
47
54
  def self.make_pet(result)
48
55
  @@pets = []
49
- result[0]['petfinder']['pets']['pet'].each do |pet|
56
+ return [{'code' => '999', 'message' => 'Cannot Make Pet. Bad parameters or no results'}] if ((!result[0]['code'].nil? || result[0]['petfinder']['header']['status']['code']['$t'].to_i != 100) == true)
57
+ if result[0]['petfinder']['lastOffset']['$t'].to_i > 1
58
+ petresult = result[0]['petfinder']['pets']['pet']
59
+ elsif result[0]['petfinder']['lastOffset']['$t'].to_i == 1
60
+ petresult = [[]]
61
+ petresult[0] << result[0]['petfinder']['pets']['pet']
62
+ petresult = petresult[0]
63
+ else
64
+ end
65
+ resultsize = result[0]['petfinder']['lastOffset']['$t'].to_i
66
+
67
+ petresult.each do |pet|
50
68
  p = Pet.new
51
- if pet['options']['option'].class == Hash
69
+
70
+ if (pet['options']['option'].class == Hash)
52
71
  p.options << pet['options']['option']['$t']
53
- elsif pet['options']['option'].class == Array
72
+ elsif (pet['options']['option'].class == Array)
54
73
  p.options = pet['options']['option'].map { |t|
55
74
  t['$t'] }
56
75
  else
57
76
  p.options = []
58
77
  end
59
- if pet['breeds']['breed'].class == Hash
60
- p.options << pet['breeds']['breed']['$t']
61
- else
78
+
79
+ if (pet['breeds']['breed'].class == Hash)
80
+ p.breeds << pet['breeds']['breed']['$t']
81
+ elsif (pet['breeds']['breed'].class == Array)
62
82
  p.breeds = pet['breeds']['breed'].map { |t|
63
83
  t['$t']}
84
+ else
85
+ p.breeds = []
64
86
  end
65
87
  p.shelter_pet_id = pet['shelterPetId'] ? pet['shelterPetId']['$t'] : ""
66
88
  p.status = pet['status']['$t']
@@ -86,10 +108,11 @@ class Pet
86
108
  p.images = []
87
109
  end
88
110
  p.petfinder_id = pet['id']['$t']
89
- @@pets << p
90
111
 
112
+ @@pets << p
91
113
  end
92
114
  return @@pets
115
+
93
116
  end
94
117
 
95
118
  end
@@ -4,8 +4,14 @@ require 'digest/md5'
4
4
  require 'nokogiri'
5
5
  require 'date'
6
6
 
7
- class PetClient
8
7
 
8
+ class NoRecordError < Exception
9
+ end
10
+
11
+ class BadKeyError < Exception
12
+ end
13
+
14
+ class PetClient
9
15
  @@token_hash = Hash.new
10
16
 
11
17
  def initialize
@@ -15,50 +21,77 @@ class PetClient
15
21
  @@token_hash
16
22
  end
17
23
 
18
- def self.get_token
19
- key_secret="#{ENV['PET_SECRET']}key=#{ENV['PET_KEY']}"
24
+ def self.get_token(api_key=nil, api_secret=nil)
25
+ begin
26
+ key_status = 'bad' if ((api_key==nil || api_secret==nil) && (ENV['PET_SECRET']==nil || ENV['PET_KEY']==nil))
27
+ raise BadKeyError, "API KEY ERROR" if key_status=='bad'
28
+ key_secret = (api_key.nil? || api_secret.nil?) ? ("#{ENV['PET_SECRET']}key=#{ENV['PET_KEY']}") : (api_secret+"key="+api_key)
20
29
  md5 = Digest::MD5.hexdigest(key_secret)
21
30
  @@token_hash = Hash.new
22
31
  @@token_hash[:md5] = md5
23
32
  @@token_hash[:time] = Time.now
24
- expires = Date.new
25
- url = "http://api.petfinder.com/auth.getToken?key=#{ENV['PET_KEY']}&sig=#{@@token_hash[:md5]}"
33
+ url = (api_key.nil? || api_secret.nil?) ? "http://api.petfinder.com/auth.getToken?key=#{ENV['PET_KEY']}&sig=#{@@token_hash[:md5]}" : "http://api.petfinder.com/auth.getToken?key=#{api_key}&sig=#{@@token_hash[:md5]}"
26
34
  updated_url = URI.encode(url)
27
35
  result = Nokogiri::XML(open(updated_url.strip).read)
28
36
  @@token_hash[:token] = result.xpath("//auth//token/text()").text.strip
29
37
  parsed_date = Time.at(result.xpath("//auth/expires/text()").text.strip.to_i)
30
38
  @@token_hash[:expires] = Time.parse(parsed_date.strftime('%a %d %b %Y %I:%M:%S %p'))
39
+ puts ("Your token expires at #{@@token_hash[:expires]}")
40
+ raise BadKeyError, "API KEY ERROR" if @@token_hash[:expires] < Time.now
41
+ rescue
42
+ "Re-check your keys"
43
+ end
31
44
  end
32
45
 
33
-
34
46
  def self.breed_list(animal)
35
- search_string = "#{ENV['PET_SECRET']}key=#{ENV['PET_KEY']}&animal=#{animal}&format=json&token=#{@@token_hash[:token]}"
36
- sig = Digest::MD5.hexdigest(search_string)
37
- url = "http://api.petfinder.com/breed.list?key=#{ENV['PET_KEY']}&animal=#{animal}&format=json&token=#{@@token_hash[:token]}&sig=#{sig}"
38
- updated_url = URI.encode(url)
39
- result = JSON.parse(open(updated_url.strip).read)
47
+ begin
48
+ raise ArgumentError if (@@token_hash[:expires].nil? || @@token_hash[:expires] < Time.now )
49
+ search_string = "#{ENV['PET_SECRET']}key=#{ENV['PET_KEY']}&animal=#{animal}&format=json&token=#{@@token_hash[:token]}"
50
+ sig = Digest::MD5.hexdigest(search_string)
51
+ url = "http://api.petfinder.com/breed.list?key=#{ENV['PET_KEY']}&animal=#{animal}&format=json&token=#{@@token_hash[:token]}&sig=#{sig}"
52
+ updated_url = URI.encode(url)
53
+ result = JSON.parse(open(updated_url.strip).read)
54
+ rescue ArgumentError
55
+ "Key Error. Did you do PetClient.get_token(key, secret) or use PetClient.get_token with ENV variables?"
56
+ return false
57
+ end
40
58
  end
41
59
 
42
60
  ##load all the breeds
43
61
  def self.load_breeds(pet)
44
- pet[:breeds] = breed_list(pet[:name])['petfinder']['breeds']['breed'].map { |b|
45
- b["$t"]
46
- }
62
+ if breed_list(pet[:name]) == false
63
+ puts("You have a key error")
64
+ else
65
+ pet[:breeds] = breed_list(pet[:name])['petfinder']['breeds']['breed'].map { |b|
66
+ b["$t"]
67
+ }
68
+ end
47
69
  end
48
70
 
49
71
  ## DRY-ish method for API lookup
50
72
  def self.build_url(search_string, random=nil, id=nil)
51
- sig = Digest::MD5.hexdigest(search_string)
52
- partial_string = search_string.scan(/key.*/)[0]
53
- if random && id.nil? == true
54
- url = "http://api.petfinder.com/pet.getRandom?"+ partial_string+"&sig=#{sig}"
55
- elsif !random && id.nil? == true
56
- url = "http://api.petfinder.com/pet.find?"+ partial_string+"&sig=#{sig}"
57
- else
58
- url = "http://api.petfinder.com/pet.get?"+ partial_string+"&sig=#{sig}"
59
- end
60
- updated_url = URI.encode(url)
61
- JSON.parse(open(updated_url.strip).read)
73
+ begin
74
+ sig = Digest::MD5.hexdigest(search_string)
75
+ partial_string = search_string.scan(/key.*/)[0]
76
+ if random && id.nil? == true
77
+ url = "http://api.petfinder.com/pet.getRandom?"+ partial_string+"&sig=#{sig}"
78
+ elsif !random && id.nil? == true
79
+ url = "http://api.petfinder.com/pet.find?"+ partial_string+"&sig=#{sig}"
80
+ else
81
+ url = "http://api.petfinder.com/pet.get?"+ partial_string+"&sig=#{sig}"
82
+ end
83
+ updated_url = URI.encode(url)
84
+ result = JSON.parse(open(updated_url.strip).read)
85
+ raise NoMethodError if result['petfinder']['header']['status']['code']['$t'].to_i == 200
86
+ raise NoRecordError if result['petfinder']['header']['status']['code']['$t'].to_i == 201
87
+ result
88
+ rescue NoMethodError
89
+ return {'code' => '999', 'message' => "You typed in your options incorrectly"}
90
+ rescue NoRecordError
91
+ return {'code' => '201', 'message' => 'No results found'}
92
+ rescue
93
+ return {'code' => '999', 'message' => "Invalid token. Did you do PetClient.get_token(key, secret) or use PetClient.get_token with ENV variables?" }
94
+ end
62
95
  end
63
96
 
64
97
  def self.get_pet(id)
@@ -88,19 +121,23 @@ class PetClient
88
121
  ## must have separate random search because do not want to pass in parameters that will break api call, even if empty strings
89
122
  if random
90
123
  search_string = "#{ENV['PET_SECRET']}key=#{ENV['PET_KEY']}&animal=#{animal}&size=#{size}&breed=#{breed}&sex=#{sex}&location=#{location}&output=full&format=json&token=#{@@token_hash[:token]}"
91
- result_set << build_url(search_string, random)
124
+ result = build_url(search_string, random)
125
+ result_set << result
92
126
  else
93
127
  search_string = "#{ENV['PET_SECRET']}key=#{ENV['PET_KEY']}&animal=#{animal}&size=#{size}&breed=#{breed}&sex=#{sex}&location=#{location}&age=#{age}&offset=#{offset}&count=#{count}&output=full&format=json&token=#{@@token_hash[:token]}"
94
- result_set << build_url(search_string)
128
+ result = build_url(search_string)
129
+ result_set << result
95
130
  end
96
131
  }
97
132
  else
98
133
  if random
99
134
  search_string = "#{ENV['PET_SECRET']}key=#{ENV['PET_KEY']}&animal=#{animal}&size=#{size}&sex=#{sex}&location=#{location}&output=full&format=json&token=#{@@token_hash[:token]}"
100
- result_set << build_url(search_string, random)
135
+ result = build_url(search_string, random)
136
+ result_set << result
101
137
  else
102
138
  search_string = "#{ENV['PET_SECRET']}key=#{ENV['PET_KEY']}&animal=#{animal}&size=#{size}&sex=#{sex}&location=#{location}&age=#{age}&offset=#{offset}&count=#{count}&output=full&format=json&token=#{@@token_hash[:token]}"
103
- result_set << build_url(search_string)
139
+ result = build_url(search_string)
140
+ result_set << result
104
141
  end
105
142
  end
106
143
  return result_set
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: PetSearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7.9
4
+ version: 0.0.8.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lior Bendat, Sharif Hadidi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-31 00:00:00.000000000 Z
11
+ date: 2013-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri