Class: Classifieds::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/classifieds/item.rb

Overview

describes the thing in a listing that is for sale

Direct Known Subclasses

Vehicle

Constant Summary collapse

COLUMN_SEPARATION =
5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, price, condition, detail_url) ⇒ Item

It is expected to be subclassed based on the type of item for sale (e.g., vehicle, clothing, furniture, etc.) To improve response time, an item’s detail_values are only loaded on demand (from the detail url)



5
6
7
8
9
10
11
# File 'lib/classifieds/item.rb', line 5

def initialize(title, price, condition, detail_url)
  @title = title
  @price = price
  @detail_url = detail_url
  @condition = condition
  @detail_values = {}
end

Class Method Details

.clearObject

Empty list of created objects



14
15
16
# File 'lib/classifieds/item.rb', line 14

def self.clear
  # all.clear
end

Instance Method Details

#detail_phoneObject



50
51
52
# File 'lib/classifieds/item.rb', line 50

def detail_phone
  @detail_values[:Phone]
end

#details_to_string(addon_details) ⇒ Object

Return an item’s detail data formatted for display



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/classifieds/item.rb', line 21

def details_to_string(addon_details)
  Classifieds::Listing.scrape_listing_details(self.class, @detail_url, @condition, @detail_values) if @detail_values.empty?

  detail_values_array = @detail_values.to_a
  addon_details.delete(:Phone) if detail_phone  # do not use seller phone if item details has a phone.
  detail_values_array.concat(addon_details.to_a)
  offset = detail_values_array.size / 2 # prepare for two column output.
  mod2 = detail_values_array.size % 2   # and account for an odd number of details.
  col1_ljust = max_col1_width(detail_values_array, offset+mod2) + COLUMN_SEPARATION
  result = ''

  (0...offset+mod2).each { |index|
    # column 1
    attribute = detail_values_array[index][0].to_s
    value = detail_values_array[index][1]
    result << "  #{Classifieds::Listing.format_detail(attribute, attr_width(1), value).ljust(col1_ljust)}"

    # column 2
    if 'Description' == attribute.to_s  # Have Description be on its own line.
      result << "\n"
    elsif (index + offset) < detail_values_array.size
      attribute = detail_values_array[index+offset][0].to_s
      value = detail_values_array[index+offset][1]
      result << "#{Classifieds::Listing.format_detail(attribute, attr_width(2), value)}\n"
    end
  }
  result
end