class PCGS

Public Class Methods

convert_to_integer(text) click to toggle source
# File lib/pcgs.rb, line 296
def self.convert_to_integer(text)
        if text.gsub(",","").to_i.to_s == text.gsub(",","")
                text.gsub(",","").to_i
        else
                text
        end
end
get_rows(table) click to toggle source
# File lib/pcgs.rb, line 304
def self.get_rows(table)
        trs = get_trs(table)
        rows = []
        trs.each do |tr|
                tds = get_tds(tr)
                tds = get_ths(tr) if tds.empty?
                row = tds.map do |td|
                        if td.search("//span").empty?
                                r = td.inner_html
                        else
                                r = td.search("//span").first.inner_html
                                r = strip_span(r)
                        end
                        r = strip_a(r)
                        r = strip_br(r)
                        r = strip_nbsp(r)
                        r = r.strip
                        r = pick_element(r)
                        r = convert_to_integer(r)
                end
                rows << row
        end
        rows
end
get_tabs(tab_data) click to toggle source
# File lib/pcgs.rb, line 329
def self.get_tabs(tab_data)
        tab_data.search("//a").map{|a|a.inner_html}
end
get_tds(tr) click to toggle source
# File lib/pcgs.rb, line 247
def self.get_tds(tr)
        tr.search("//td")
end
get_ths(tr) click to toggle source
# File lib/pcgs.rb, line 251
def self.get_ths(tr)
        tr.search("//th")
end
get_trs(table) click to toggle source
# File lib/pcgs.rb, line 243
def self.get_trs(table)
        table.search("//tr")
end
objectify_table_info(t) click to toggle source
# File lib/pcgs.rb, line 333
def self.objectify_table_info(t)
        table = PCGS::Table.new(t[:coin_type], t[:tab_title], t[:coin_grade_type], t[:table])
        rows = PCGS.get_rows(t[:table]) rescue []
        table.add_rows(rows)
        table
end
pick_element(text) click to toggle source
# File lib/pcgs.rb, line 285
def self.pick_element(text)
        if text[-1] == "-"
                text = text[0..-2]
        elsif text[-2..-1] == " +"
                text = text[0..-3]
        elsif (text.scan(/[\d,]+/).size == 2) && (text.scan(/[^\d^,^ ]/) == [] )
                text = text.scan(/[\d,]+/)[0]
        end
        text
end
scrape(url) click to toggle source
# File lib/pcgs.rb, line 212
def self.scrape(url)
        open(URI(url)){|f|Hpricot(f)}
end
strip_a(text) click to toggle source
# File lib/pcgs.rb, line 266
def self.strip_a(text)
        if text.include?("<a")
                text.scan(/<a[^>]*>/).each do |replace|
                        text = text.gsub(replace, " ") rescue text
                end
                text = text.gsub("</a>", "")
        else
                text
        end
end
strip_br(text) click to toggle source
# File lib/pcgs.rb, line 277
def self.strip_br(text)
        text.gsub("<br />", "")
end
strip_nbsp(text) click to toggle source
# File lib/pcgs.rb, line 281
def self.strip_nbsp(text)
        text.gsub("&nbsp;", " ")
end
strip_span(text) click to toggle source
# File lib/pcgs.rb, line 255
def self.strip_span(text)
        if text.include?("<span")
                text.scan(/<span[^>]*>/).each do |replace|
                        text = text.gsub(replace, " ") rescue text
                end
                text = text.gsub("</span>", "")
        else
                text
        end
end
table_from_doc(doc, n) click to toggle source
# File lib/pcgs.rb, line 221
def self.table_from_doc(doc, n)
        doc.search("//div[@id='blue-table']").search("//table")[n]
end
table_from_url(url, n) click to toggle source
# File lib/pcgs.rb, line 216
def self.table_from_url(url, n)
        doc = PCGS.scrape(url)
        table_from_doc(doc, n)
end
table_info(doc) click to toggle source
# File lib/pcgs.rb, line 225
def self.table_info(doc)
        coin_type = doc.to_s.scan(/<a href=\"\/prices\">Home<\/a>[^<]+</).first.gsub("<a href=\"/prices\">Home</a>&nbsp;&gt;&nbsp;","").split("\r")[0]
        tabs = doc.search("//div[@id='blue-table']").search("//ul")
        tab_titles = tabs.map{|t|get_tabs(t)}
        tab_links = tabs.map{|tab|tab.search("//a").map{|a|a["href"]}}
        t = []
        (0..(tab_links.size-1)).each do |i|
                tab_links[i].each_index do |j|
                        tab_title = tab_titles[i][j]
                        link = tab_links[i][j]
                        table = table_from_url(link, i)
                        coin_grade_type = ["MS","PR"][i]#doc.to_s.scan(/Copper Type Coins, \S\S/).map{|t|t.gsub("Copper Type Coins, ","")}[i]
                        t << {:coin_type => coin_type, :coin_grade_type => coin_grade_type, :tab_title => tab_title, :link => link, :table => table}
                end
        end
        t
end