diff --git a/Gemfile b/Gemfile index 6b85167..5c2e990 100644 --- a/Gemfile +++ b/Gemfile @@ -5,5 +5,5 @@ ruby '2.0.0' gem 'twitter' gem 'dotenv' gem 'nokogiri' - -gem 'minitest' \ No newline at end of file +gem 'petfinder' +gem 'minitest' diff --git a/Gemfile.lock b/Gemfile.lock index b9b8c8d..c489f95 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,34 +1,38 @@ GEM remote: https://rubygems.org/ specs: - addressable (2.3.5) - atomic (1.1.15) + addressable (2.3.6) buftok (0.2.0) - dotenv (0.10.0) + dotenv (0.11.1) + dotenv-deployment (~> 0.0.2) + dotenv-deployment (0.0.2) equalizer (0.0.9) + excon (0.39.2) faraday (0.9.0) multipart-post (>= 1.2, < 3) - http (0.5.0) - http_parser.rb + http (0.6.1) + http_parser.rb (~> 0.6.0) http_parser.rb (0.6.0) json (1.8.1) - memoizable (0.4.1) - thread_safe (~> 0.2.0) - mini_portile (0.5.3) - minitest (4.3.2) + memoizable (0.4.2) + thread_safe (~> 0.3, >= 0.3.1) + mini_portile (0.6.0) + minitest (5.4.0) multipart-post (2.0.0) naught (1.0.0) - nokogiri (1.6.1) - mini_portile (~> 0.5.0) + nokogiri (1.6.3.1) + mini_portile (= 0.6.0) + petfinder (1.0.1) + excon (~> 0.26) + nokogiri (~> 1.6) simple_oauth (0.2.0) - thread_safe (0.2.0) - atomic (>= 1.1.7, < 2) - twitter (5.7.1) + thread_safe (0.3.4) + twitter (5.11.0) addressable (~> 2.3) buftok (~> 0.2.0) equalizer (~> 0.0.9) faraday (~> 0.9.0) - http (~> 0.5.0) + http (~> 0.6.0) http_parser.rb (~> 0.6.0) json (~> 1.8) memoizable (~> 0.4.0) @@ -42,4 +46,5 @@ DEPENDENCIES dotenv minitest nokogiri + petfinder twitter diff --git a/lib/cuties/adopt_a_pet.rb b/lib/cuties/adopt_a_pet.rb index 56146ca..c9979d7 100644 --- a/lib/cuties/adopt_a_pet.rb +++ b/lib/cuties/adopt_a_pet.rb @@ -2,26 +2,17 @@ require 'json' require 'open-uri' require 'pp' - +require 'petfinder' require 'dotenv' -Dotenv.load -module AdoptAPet - URL = 'http://api.petfinder.com/pet.getRandom' - - def self.get_shelter - shelter = ENV['shelter_id'].split(",") - shelter.sample - end +Dotenv.load - PARAMS = { - format: 'json', - key: ENV['petfinder_key'], - shelterid: get_shelter, - output: 'full' - } +Petfinder.configure do |config| + config.api_key = ENV['petfinder_key'] + config.api_secret = ENV['petfinder_secret'] +end - SEXES = { m: 'male', f: 'female' } +module AdoptAPet def self.random pet = fetch_pet while pet.nil? || pet.error? @@ -30,53 +21,49 @@ def self.random private - def self.get_breeds(pet) - breed_list = [pet['breeds']['breed']].flatten # Coerces into an Array - breed_list = breed_list.map{ |b| b["$t"] }.join(' / ') # Joins into string - breed_list << ' mix' if breed_list.include?('/') - breed_list + unless pet.breeds.nil? + pet.breeds[0] + end end - def self.get_photo(pet) - # Assume that if there isn't a 3rd photo, there is a first one - # There's some more refactoring to be done here. - - unless pet['media']['photos']['photo'].nil? - photo = pet['media']['photos']['photo'][2] || pet['media']['photos']['photo'][0] - photo['$t'] + unless pet.photos.nil? + pet.photos[0].medium end end - def self.get_sex(pet) - sex = pet['sex']['$t'].downcase.to_sym - SEXES.fetch(sex) { 'gender-unspecified '} # Fetch a sex, or list as gender-unspecified + sexes = { m: 'male', f: 'female' } + sex = pet.sex.downcase.to_sym + # Fetch a sex, or list as gender-unspecified + sexes[sex] || 'gender-unspecified' end - def self.get_city(pet) - city = pet['contact']['city']['$t'] + def self.get_shelter + shelter = ENV['shelter_id'].split(",") + shelter.sample end def self.fetch_pet - uri = URI(URL) - uri.query = URI.encode_www_form(PARAMS) - json = JSON.parse(Net::HTTP.get_response(uri).body) + petfinder = Petfinder::Client.new - PP.pp(json) # Pretty-prints the response in the Terminal + options = { shelterid: get_shelter } + pet = petfinder.random_pet(options) - pet_json = json['petfinder']['pet'] + # Prints the Pet xml in the Terminal + xml = pet.instance_variable_get(:@xml) + puts xml Pet.new({ - breed: get_breeds(pet_json), - pic: get_photo(pet_json), - link: "https://www.petfinder.com/petdetail/" + pet_json['id']['$t'], - name: pet_json['name']['$t'].my_titleize, - id: pet_json['id']['$t'], - sex: get_sex(pet_json), - type: pet_json['animal']['$t'], - city: get_city(pet_json) + breed: get_breeds(pet), + pic: get_photo(pet), + link: "https://www.petfinder.com/petdetail/" + pet.id, + name: pet.name.my_titleize, + id: pet.id, + sex: get_sex(pet), + type: pet.animal, + city: pet.city[0] }) end end diff --git a/lib/cuties/twit.rb b/lib/cuties/twit.rb index bf678e4..6a3f109 100644 --- a/lib/cuties/twit.rb +++ b/lib/cuties/twit.rb @@ -21,7 +21,6 @@ def message "#{greeting} #{pet.message}" end - def client Twitter::REST::Client.new do |config| begin @@ -37,7 +36,6 @@ def client end end - def tweet puts message unless pet.pic.nil?