Skip to content

Commit

Permalink
Merge pull request codeforamerica#32 from lynnaloo/petfinder_gem_refa…
Browse files Browse the repository at this point in the history
…ctor

Petfinder gem refactor
  • Loading branch information
lynnaloo committed Aug 22, 2014
2 parents f206e9e + 8991114 commit 0880a64
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 65 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ ruby '2.0.0'
gem 'twitter'
gem 'dotenv'
gem 'nokogiri'

gem 'minitest'
gem 'petfinder'
gem 'minitest'
35 changes: 20 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -42,4 +46,5 @@ DEPENDENCIES
dotenv
minitest
nokogiri
petfinder
twitter
79 changes: 33 additions & 46 deletions lib/cuties/adopt_a_pet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
2 changes: 0 additions & 2 deletions lib/cuties/twit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def message
"#{greeting} #{pet.message}"
end


def client
Twitter::REST::Client.new do |config|
begin
Expand All @@ -37,7 +36,6 @@ def client
end
end


def tweet
puts message
unless pet.pic.nil?
Expand Down

0 comments on commit 0880a64

Please sign in to comment.