Class: Classifieds::Listing
- Inherits:
-
Object
- Object
- Classifieds::Listing
- Defined in:
- lib/classifieds/listing.rb
Overview
describes a classified advertisement
Constant Summary collapse
- MAX_LINE_LENGTH =
100- @@all_listings =
A Listing has one item and one seller, along with listing-based attributes such as the listing start date
[]
Class Method Summary collapse
-
.clear_all ⇒ Object
Empty list of created objects.
-
.fmt_cols(values, formats) ⇒ Object
Formats an array of strings according to an array of formats.
-
.fmt_detail_attr(string, width) ⇒ Object
Format a detail attribute.
-
.fmt_detail_val(string, wrap_indent) ⇒ Object
Format a detail value (with wrap if necessary).
-
.format_detail(attr, attr_width, val) ⇒ Object
Format a detail item.
-
.print_summary(item_class, start_index, end_index) ⇒ Object
Prints the specified summary listings for the specified item subclass.
-
.scrape_listing_details(item_class, detail_url, item_condition, detail_values) ⇒ Object
Returns detail attributes and values in detail_values hash.
-
.scrape_listings(item_class, results_url) ⇒ Object
Creates listings from summary web page.
Instance Method Summary collapse
-
#initialize(id, item, seller, start_date) ⇒ Listing
constructor
A new instance of Listing.
-
#print_detail(item_number) ⇒ Object
Prints a detail listing for a single item.
-
#summary_detail_row(item_number) ⇒ Object
Prints a summary detail row.
Constructor Details
#initialize(id, item, seller, start_date) ⇒ Listing
Returns a new instance of Listing.
6 7 8 9 10 11 12 |
# 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 |
Class Method Details
.clear_all ⇒ Object
Empty list of created objects
15 16 17 18 19 |
# File 'lib/classifieds/listing.rb', line 15 def self.clear_all Classifieds::Item.clear Classifieds::Seller.clear self.clear end |
.fmt_cols(values, formats) ⇒ Object
Formats an array of strings according to an array of formats
54 55 56 57 58 |
# File 'lib/classifieds/listing.rb', line 54 def self.fmt_cols(values, formats) result = '' values.each_with_index { |value, index| result << "#{fmt_col(value, formats[index][0], formats[index][1])} " } result end |
.fmt_detail_attr(string, width) ⇒ Object
Format a detail attribute
63 64 65 |
# File 'lib/classifieds/listing.rb', line 63 def self.fmt_detail_attr(string, width) "#{lfmt(string, width)}" end |
.fmt_detail_val(string, wrap_indent) ⇒ Object
Format a detail value (with wrap if necessary)
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/classifieds/listing.rb', line 73 def self.fmt_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 |
.format_detail(attr, attr_width, val) ⇒ Object
Format a detail item
68 69 70 |
# File 'lib/classifieds/listing.rb', line 68 def self.format_detail(attr, attr_width, val) "#{fmt_detail_attr(attr, attr_width)}: #{fmt_detail_val(val, attr_width)}" end |
.print_summary(item_class, start_index, end_index) ⇒ Object
Prints the specified summary listings for the specified item subclass
30 31 32 33 |
# File 'lib/classifieds/listing.rb', line 30 def self.print_summary(item_class, start_index, end_index) puts " #{item_class.summary_header} #{Classifieds::Seller.summary_header} #{lfmt('List Date', 10)}" all[start_index..end_index].each_with_index { |listing, index| puts listing.summary_detail_row(start_index+index+1) } end |
.scrape_listing_details(item_class, detail_url, item_condition, detail_values) ⇒ Object
Returns detail attributes and values in detail_values hash
43 44 45 46 |
# 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) ⇒ Object
Creates listings from summary web page
36 37 38 39 40 |
# 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 |
Instance Method Details
#print_detail(item_number) ⇒ Object
Prints a detail listing for a single item
22 23 24 25 26 27 |
# File 'lib/classifieds/listing.rb', line 22 def print_detail(item_number) addon_details = {:Phone => @seller.phone, :'Listing #' => @id} puts '', self.summary_detail_row(item_number), @item.details_to_string(addon_details) end |
#summary_detail_row(item_number) ⇒ Object
Prints a summary detail row
49 50 51 |
# 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 |