Sha256: acfa2b69ea1481de5c1aa70d411f2fce611e668f1dd6923f3a3b26f739e9f990

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

module Petfinder

  class Pet
    extend JsonMapper

    json_attributes "name", "breed", "age", "size", "id", "shelter_id",
                    "description", "shelter_id"

    attr_reader :attributes, :contact_info
    def initialize pet_hash
      @attributes = pet_hash
      @contact_info = contact
    end

    def photos
      parse_photos if @photos.nil?
      @photos
    end

    private

    def contact
      class << self
        json_attributes "phone", "state", "email", "city",
                        "zip", "fax", "address1"
      end
    end

    def parse_photos
      @photos = []
      @attributes["media"]["photos"]["photo"].each do |photo|
        add_photo photo, photo["$t"]
      end
    end

    def add_photo photo, url
      size = photo["@size"]
      photo = set_photo photo
      case size
      when "x"
        photo.large = url
      when "pn"
        photo.medium = url
      when "fpm"
        photo.small = url
      when "pnt"
        photo.tiny = url
      end
      @photos << photo unless @photos.find_index photo
    end

    def set_photo photo
      @photos.each do |existing_photo|
        if photo["@id"] == existing_photo.id
          return existing_photo
        end
      end
      Photo.new photo
    end

    class Photo

      attr_accessor :large, :medium, :small, :thumbnail, :tiny
      attr_reader :id, :attributes

      def initialize attributes
        @attributes = attributes
        @id = attributes["@id"]
      end

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
petfinder-wrap-1.0.3 lib/petfinder/pet.rb
petfinder-wrap-1.0.2.1 lib/petfinder/pet.rb
petfinder-wrap-1.0.2 lib/petfinder/pet.rb