PetSearch 0.0.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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NmJmZDI3MWJmMTJkODA1OTRiYmEzYjBkZjY1ZmMwZjUzMTFjZDBmZQ==
5
+ data.tar.gz: !binary |-
6
+ NWRhOGZhMjY5MTA5Zjk1M2UyOWNkZTliYmIxNzZiNjljNjVhYTExNQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MDA5NTE2NWRlZGYwMzNjNTM3NzI5YTc3MmUzNTg3YTFkODVkMzlmZTNjYzE4
10
+ ZGUzM2NlOWNlOTQ0NGFlN2YwYTE1NGU2YTJiNTc5YmM5ZTE5NDMyYTdlM2Qy
11
+ ZWJhM2NkMTY5NjU1ZDVjZmI2NWExMjU5YTEyMmE2NzI1YzUzYmY=
12
+ data.tar.gz: !binary |-
13
+ YzBiMDk2ZmRlOGJjMTcwODcwMWM3OTc1NzM4Y2YwNGI2YTk0NDViZGRmYzQy
14
+ YzU0NjA1ZTliYjBjNjMzMzc3NTgyOTg2MDk4YzFhN2VmZmUzNDFlZmEzZDQ4
15
+ Y2ZiYTU3MTE1NzUwMmY5ZjU2YTQ3NTkwODNjOWFhYmI3YzcxN2E=
@@ -0,0 +1,27 @@
1
+ require 'json'
2
+ require 'open-uri'
3
+ require 'digest/md5'
4
+ require 'pry'
5
+ require 'nokogiri'
6
+ require 'date'
7
+ require 'petsearch/PetClient'
8
+
9
+ class PetSearch
10
+ attr_accessor :dog, :cat, :bird, :reptile, :horse, :pig, :barnyard, :smallfurry
11
+
12
+ def initialize
13
+ @dog = {:name => 'dog', :breeds => []}
14
+ @cat = {:name => 'cat', :breeds => []}
15
+ @bird = {:name => 'bird', :breeds => []}
16
+ @reptile = {:name => 'reptile', :breeds => []}
17
+ @horse = {:name => 'horse', :breeds => []}
18
+ @pig = {:name => 'horse', :breeds => []}
19
+ @barnyard = {:name => 'horse', :breeds => []}
20
+ @smallfurry = {:name => 'smallfurry', :breeds => []}
21
+ end
22
+
23
+
24
+ end
25
+
26
+
27
+
@@ -0,0 +1,106 @@
1
+ require 'json'
2
+ require 'open-uri'
3
+ require 'digest/md5'
4
+ require 'pry'
5
+ require 'nokogiri'
6
+ require 'date'
7
+
8
+
9
+ class PetClient
10
+
11
+ @@token_hash = Hash.new
12
+
13
+ def initialize
14
+ end
15
+
16
+ def self.get_token
17
+ key_secret="#{ENV['PET_SECRET']}key=#{ENV['PET_KEY']}"
18
+ md5 = Digest::MD5.hexdigest(key_secret)
19
+ @@token_hash = Hash.new
20
+ @@token_hash[:md5] = md5
21
+ @@token_hash[:time] = Time.now
22
+ expires = Date.new
23
+ url = "http://api.petfinder.com/auth.getToken?key=#{ENV['PET_KEY']}&sig=#{@@token_hash[:md5]}"
24
+ updated_url = URI.encode(url)
25
+ result = Nokogiri::XML(open(updated_url.strip).read)
26
+ @@token_hash[:token] = result.xpath("//auth//token/text()").text.strip
27
+ parsed_date = Time.at(result.xpath("//auth/expires/text()").text.strip.to_i)
28
+ @@token_hash[:expires] = Time.parse(parsed_date.strftime('%a %d %b %Y %I:%M:%S %p'))
29
+ end
30
+
31
+
32
+ def self.breed_list(animal)
33
+ search_string = "#{ENV['PET_SECRET']}key=#{ENV['PET_KEY']}&animal=#{animal}&format=json&token=#{@@token_hash[:token]}"
34
+ sig = Digest::MD5.hexdigest(search_string)
35
+ url = "http://api.petfinder.com/breed.list?key=#{ENV['PET_KEY']}&animal=#{animal}&format=json&token=#{@@token_hash[:token]}&sig=#{sig}"
36
+ updated_url = URI.encode(url)
37
+ result = JSON.parse(open(updated_url.strip).read)
38
+ end
39
+
40
+ ##load all the breeds
41
+ def self.load_breeds(pet)
42
+ pet[:breeds] = breed_list(pet[:name])['petfinder']['breeds']['breed'].map { |b|
43
+ b["$t"]
44
+ }
45
+ end
46
+
47
+ ## DRY-ish method for API lookup
48
+ def self.build_url(search_string, random=nil, id=nil)
49
+ sig = Digest::MD5.hexdigest(search_string)
50
+ partial_string = search_string.scan(/key.*/)[0]
51
+ if random && id.nil? == true
52
+ url = "http://api.petfinder.com/pet.getRandom?"+ partial_string+"&sig=#{sig}"
53
+ elsif !random && id.nil? == true
54
+ url = "http://api.petfinder.com/pet.find?"+ partial_string+"&sig=#{sig}"
55
+ else
56
+ url = "http://api.petfinder.com/pet.get?"+ partial_string+"&sig=#{sig}"
57
+ end
58
+ updated_url = URI.encode(url)
59
+ JSON.parse(open(updated_url.strip).read)
60
+ end
61
+
62
+ def self.get_pet(id)
63
+ search_string = "#{ENV['PET_SECRET']}key=#{ENV['PET_KEY']}&id=#{id}&format=json&token=#{@@token_hash[:token]}"
64
+ result = build_url(search_string, nil, id)
65
+ end
66
+
67
+ def self.search_listings(pet, options={})
68
+ animal = (pet[:name].nil?) ? false : pet[:name]
69
+ breeds = (options[:breeds].nil? || options[:breeds].empty?) ? "" : options[:breeds]
70
+ size = options[:size].nil? ? "" : options[:size]
71
+ sex = options[:sex].nil? ? "" : options[:sex]
72
+ location = options[:location].nil? ? "" : options[:location]
73
+ age = options[:age].nil? ? "" : options[:age]
74
+ offset = options[:offset].nil? ? "" : options[:offset]
75
+ count = options[:count].nil? ? "" : options[:count]
76
+ random = (options[:random].nil? || options[:random]==false) ? false : true
77
+ result_set = []
78
+
79
+ # this is a custom option not supported by API
80
+ distance = options[:distance].nil? ? "" : options[:distance]
81
+ pure = options[:pure].nil? ? "" : options[:pure]
82
+
83
+ ### must do loops if breed param
84
+ if breeds && breeds!=""
85
+ breeds.each { |breed|
86
+ ## must have separate random search because do not want to pass in parameters that will break api call, even if empty strings
87
+ if random
88
+ 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]}"
89
+ result_set << build_url(search_string, random)
90
+ else
91
+ 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]}"
92
+ result_set << build_url(search_string)
93
+ end
94
+ }
95
+ else
96
+ if random
97
+ 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]}"
98
+ result_set << build_url(search_string, random)
99
+ else
100
+ 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]}"
101
+ result_set << build_url(search_string)
102
+ end
103
+ end
104
+ end
105
+
106
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: PetSearch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lior Bendat, Sharif Hadidi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This gem is a simple interface for the PetFinder API, which is notoriously
14
+ difficult to deal with. I've attempted to make people's lives easier.
15
+ email: liorbendat@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/petsearch.rb
21
+ - lib/petsearch/petclient.rb
22
+ homepage: https://github.com/lbendat/PetfinderAPIGem
23
+ licenses:
24
+ - Open-Source
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.0.5
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Interface for PetFinder API
46
+ test_files: []