Class: RightmoveBLM::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/rightmove_blm/document.rb

Overview

A BLM document including its header, definition, and data content.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source: nil, header: nil, definition: nil, data: nil) ⇒ Document

Returns a new instance of Document.



12
13
14
15
16
17
# File 'lib/rightmove_blm/document.rb', line 12

def initialize(source: nil, header: nil, definition: nil, data: nil)
  @source = source
  @header = header
  @definition = definition
  initialize_with_data(data) unless data.nil?
end

Class Method Details

.from_array_of_hashes(array) ⇒ Object



6
7
8
9
10
# File 'lib/rightmove_blm/document.rb', line 6

def self.from_array_of_hashes(array)
  date = Time.now.utc.strftime('%d-%b-%Y %H:%M').upcase
  header = { version: '3', eof: '^', eor: '~', 'property count': array.size.to_s, 'generated date': date }
  new(header: header, definition: array.first.keys.map(&:to_sym), data: array)
end

Instance Method Details

#definitionObject



46
47
48
49
50
51
52
# File 'lib/rightmove_blm/document.rb', line 46

def definition
  @definition ||= contents(:definition).split(header[:eor]).first.split(header[:eof]).map do |field|
    next nil if field.empty?

    field.downcase.strip
  end.compact
end

#errorsObject



58
59
60
# File 'lib/rightmove_blm/document.rb', line 58

def errors
  @errors ||= data.reject(&:valid?).flat_map(&:errors)
end

#headerObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/rightmove_blm/document.rb', line 35

def header
  @header ||= contents(:header).each_line.map do |line|
    next nil if line.empty?

    key, _, value = line.partition(':')
    next nil if value.nil?

    [key.strip.downcase.to_sym, value.tr("'", '').strip]
  end.compact.to_h
end

#inspectObject



19
20
21
# File 'lib/rightmove_blm/document.rb', line 19

def inspect
  %(<##{self.class.name} version=#{version} rows=#{rows.size} valid=#{valid?} errors=#{errors.size}>)
end

#international?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rightmove_blm/document.rb', line 70

def international?
  %w[H1 3I].include?(version)
end

#rowsObject



54
55
56
# File 'lib/rightmove_blm/document.rb', line 54

def rows
  data
end

#to_blmObject



27
28
29
30
31
32
33
# File 'lib/rightmove_blm/document.rb', line 27

def to_blm
  [
    header_string,
    definition_string,
    data_string
  ].join("\n")
end

#to_sObject



23
24
25
# File 'lib/rightmove_blm/document.rb', line 23

def to_s
  inspect
end

#valid?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/rightmove_blm/document.rb', line 62

def valid?
  errors.empty?
end

#versionObject



66
67
68
# File 'lib/rightmove_blm/document.rb', line 66

def version
  header[:version]
end