class Classifieds::Listing

Constants

MAX_LINE_LENGTH

Public Class Methods

clear_all() click to toggle source

Empty list of created objects

# File lib/classifieds/listing.rb, line 15
def self.clear_all
  Classifieds::Item.clear
  Classifieds::Seller.clear
  self.clear
end
format_cols(values, formats) click to toggle source

Formats an array of strings according to an array of formats

# File lib/classifieds/listing.rb, line 54
def self.format_cols(values, formats)
  result = ''
  values.each_with_index { |value, index| result << "#{format_col(value, formats[index][0], formats[index][1])} " }
  result
end
format_detail(attr, attr_width, val) click to toggle source

Format a detail item

# File lib/classifieds/listing.rb, line 68
def self.format_detail(attr, attr_width, val)
  "#{format_detail_attr(attr, attr_width)}: #{format_detail_val(val, attr_width)}"
end
format_detail_attr(string, width) click to toggle source

Format a detail attribute

# File lib/classifieds/listing.rb, line 63
def self.format_detail_attr(string, width)
  "#{lfmt(string, width)}"
end
format_detail_val(string, wrap_indent) click to toggle source

Format a detail value (with wrap if necessary)

# File lib/classifieds/listing.rb, line 73
def self.format_detail_val(string, wrap_indent)
  new_string = string.strip
  if new_string.size > MAX_LINE_LENGTH
    new_string = ''
    new_line = "\n  #{' '*wrap_indent}  "  # Indent size of Attribute display width.
    line_len = 0
    string.split(' ').each { |word|
      new_string << "#{word} "
      if (line_len += word.size + 1) > MAX_LINE_LENGTH
        new_string << new_line
        line_len = 0
      end
    }
  end
  new_string
end
new(id, item, seller, start_date) click to toggle source
# File lib/classifieds/listing.rb, line 6
def initialize(id, item, seller, start_date)
  @id = id
  @item = item
  @seller = seller
  @start_date = start_date
  Classifieds::Listing.all << self
end
print_summary(item_class, start_index, end_index) click to toggle source

Prints the specified summary listings for the specified item subclass

scrape_listing_details(item_class, detail_url, item_condition, detail_values) click to toggle source

Returns detail attributes and values in detail_values hash

# File lib/classifieds/listing.rb, line 43
def self.scrape_listing_details(item_class, detail_url, item_condition, detail_values)
  detail_doc = Nokogiri::HTML(open(detail_url, :read_timeout=>10))
  item_class.scrape_results_detail_page(detail_doc, item_condition, detail_values)
end
scrape_listings(item_class, results_url) click to toggle source

Creates listings from summary web page

# File lib/classifieds/listing.rb, line 36
def self.scrape_listings(item_class, results_url)
  results_url_file = open(results_url, :read_timeout=>10)
  results_doc = Nokogiri::HTML(results_url_file)
  item_class.scrape_results_page(results_url, results_url_file, results_doc)
end

Private Class Methods

all() click to toggle source

Returns array of all listings

# File lib/classifieds/listing.rb, line 94
def self.all
  @@all_listings
end
clear() click to toggle source

Empty list of created objects

# File lib/classifieds/listing.rb, line 99
def self.clear
  all.clear
end
format_col(str, width, justify) click to toggle source

Format a column

# File lib/classifieds/listing.rb, line 104
def self.format_col(str, width, justify)
  case justify
  when 'l'
    "#{lfmt(str, width)}"
  when 'r'
    "#{rfmt(str, width)}"
  else
    "error"
  end
end
lfmt(string, size) click to toggle source

Left justify string and pad or trim to size

# File lib/classifieds/listing.rb, line 116
def self.lfmt(string, size)
  size == 0 ? string : string.slice(0,size).ljust(size)
end
rfmt(string, size) click to toggle source

Right justify string and pad or trim to size

# File lib/classifieds/listing.rb, line 121
def self.rfmt(string, size)
  string.slice(0,size).rjust(size)
end

Public Instance Methods

print_detail(item_number) click to toggle source

Prints a detail listing for a single item

summary_detail_row(item_number) click to toggle source

Prints a summary detail row

# File lib/classifieds/listing.rb, line 49
def summary_detail_row(item_number)
  "#{(item_number).to_s.rjust(2)}. #{@item.summary_detail}  #{@seller.summary_detail}  #{Classifieds::Listing.lfmt(@start_date, 10)}"
end