Class: Wagon::Household

Inherits:
Object
  • Object
show all
Defined in:
lib/wagon/household.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Household

Returns a new instance of Household.



13
14
15
16
# File 'lib/wagon/household.rb', line 13

def initialize(connection)
  @members = []
  @connection = connection
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



11
12
13
# File 'lib/wagon/household.rb', line 11

def address
  @address
end

#connectionObject (readonly)

Returns the value of attribute connection.



9
10
11
# File 'lib/wagon/household.rb', line 9

def connection
  @connection
end

#image_pathObject

Returns the value of attribute image_path.



11
12
13
# File 'lib/wagon/household.rb', line 11

def image_path
  @image_path
end

#membersObject

Returns the value of attribute members.



11
12
13
# File 'lib/wagon/household.rb', line 11

def members
  @members
end

#nameObject



18
19
20
# File 'lib/wagon/household.rb', line 18

def name
  self.individual? ? "#{members.first.name} #{@name}" : "#{@name} Household"
end

#phone_numberObject

Returns the value of attribute phone_number.



11
12
13
# File 'lib/wagon/household.rb', line 11

def phone_number
  @phone_number
end

Class Method Details

.create_from_td(connection, td) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wagon/household.rb', line 46

def self.create_from_td(connection, td)
  Household.new(connection).instance_eval do
    name_element, phone_element, *member_elements = *td.search('table > tr > td.eventsource[width="45%"] > table > tr > td.eventsource')
    @address = Address.extract_from_string(td.search('table > tr > td.eventsource[width="25%"]').inner_text)
    @image_path = td.at('table > tr > td.eventsource[width="30%"] > img')['src'] rescue nil
    @name = name_element.inner_text
    @phone_number = PhoneNumber.extract_from_string(phone_element.inner_text)
    
    member_elements.each_slice(2) do |name_and_email|
      name, email = *name_and_email.collect { |element| element.inner_text.gsub(/\302\240/, '').strip() }
      @members << Member.new(self, name, email)
    end

    self
  end
end

Instance Method Details

#<=>(other) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/wagon/household.rb', line 38

def <=>(other)
  if has_image? == other.has_image?
    reversed_name <=> other.reversed_name
  else
    has_image? ? -1 : 1
  end
end

#has_image?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/wagon/household.rb', line 30

def has_image?
  !image_path.to_s.empty?
end

#image_dataObject



34
35
36
# File 'lib/wagon/household.rb', line 34

def image_data
  @image_data ||= image_path.to_s.empty? ? "" : connection.get(image_path)
end

#individual?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/wagon/household.rb', line 26

def individual?
  members.count == 1
end

#reversed_nameObject



22
23
24
# File 'lib/wagon/household.rb', line 22

def reversed_name
  self.individual? ? "#{@name}, #{members.first.name}" : name
end