class PCGS::Wallet

Attributes

coins[RW]
tables[RW]

Public Class Methods

new(type=nil) click to toggle source
# File lib/pcgs.rb, line 10
def initialize(type=nil)
        doc = PCGS.scrape("http://www.pcgs.com/prices/")
        cols = doc.search("//div[@class='twocolumn']").first.search("//div[@class='col']")
        boxes = cols.map{|col|col.search("//div").find_all{|div|div["class"].include?("box")}}.flatten
        as = boxes.map{|box|box.search("//ul").first.search("//a")}.flatten
        urls = as.map{|a|"http://www.pcgs.com"+a["href"]}
        if not type.nil?
                urls = as.find_all{|a|a.inner_html==type}.map{|a|"http://www.pcgs.com"+a["href"]}
        end

        @tables = []

        urls.each do |url|
                # p "Getting prices for #{url.split('title=')[1].gsub('+',' ')}..."
                doc = PCGS.scrape(url)
                ti = PCGS.table_info(doc)
                ti.each do |t|
                        # p "    Making table for #{t[:tab_title]}, #{t[:coin_grade_type]}."
                        @tables << PCGS.objectify_table_info(t)
                end
        end

        @coins = []

        self.coinify_tables

        self
end

Public Instance Methods

add_coin(pcgs_no, description, design, grade, price, grade_type, subtype, type, year, mint_mark) click to toggle source
# File lib/pcgs.rb, line 82
def add_coin(pcgs_no, description, design, grade, price, grade_type, subtype, type, year, mint_mark)
        coin = PCGS::Coin.new(pcgs_no, description, design, grade, price, grade_type, subtype, type, year, mint_mark)
        self.coins << coin
        coin
end
coinify_table(table) click to toggle source
# File lib/pcgs.rb, line 45
def coinify_table(table)
        # p "Adding #{table.coin_type} coins to wallet..."
        table.rows.each do |row|
                if row.elements.first.is_a?(Integer)
                        if row.elements[1] != "Type"
                                pcgs_no = row.elements[0].to_i
                                description = row.elements[1]
                                design = row.elements[2]
                                grade_type = table.coin_grade_type
                                subtype = row.coin_subtype
                                type = table.coin_type
                                y = description.to_s.scan(/\d\d\d\d/)
                                if y.empty?
                                        year = ""
                                elsif y.size == 1
                                        year = y[0]
                                elsif y.size == 2
                                        if description.gsub(" ","").include?(y[0]+"-"+y[1])
                                                year = y[0]+"-"+y[1]
                                        else
                                                year = y[0]
                                        end
                                end
                                mint_mark = description.to_s.scan(/\d\d\d\d\-[A-Z]/)[0][-1] rescue ""
                                row.elements[3..-1].each do |e|
                                        i = row.elements.index(e)
                                        grade = row.header_row.elements[i]
                                        price = row.elements[i]
                                        coin = self.add_coin(pcgs_no, description, design, grade, price, grade_type, subtype, type, year, mint_mark)
                                        coin = coin.set_name
                                        # p "Adding a #{coin.name} to the wallet."
                                end
                        end
                end
        end
end
coinify_tables() click to toggle source
# File lib/pcgs.rb, line 39
def coinify_tables
        self.tables.each do |table|
                self.coinify_table(table)
        end
end
coins_like(str) click to toggle source
# File lib/pcgs.rb, line 88
def coins_like(str)
        wallet = self
        types = wallet.coins.find_all{|c|c.type_like?(str)}
        names = wallet.coins.find_all{|c|c.name_like?(str)}
        (types+names).flatten.uniq
end