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, name: nil) ⇒ Document

Returns a new instance of Document.



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

def initialize(source: nil, header: nil, definition: nil, data: nil, name: nil)
  @source = source || data
  @header = header
  @definition = definition
  @name = name
  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



53
54
55
56
57
58
59
# File 'lib/rightmove_blm/document.rb', line 53

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



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

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

#headerObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/rightmove_blm/document.rb', line 42

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

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

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

#inspect(safe: false, error: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/rightmove_blm/document.rb', line 20

def inspect(safe: false, error: nil)
  name = @name.nil? ? nil : %( document="#{@name}")
  error_string = error.nil? ? nil : %( error="#{error}")
  basic = %(<##{self.class.name}#{name}#{error_string}>)
  return basic if safe

  "<##{self.class.name}#{name}#{error_string} version=#{version} " \
    "rows=#{rows.size} valid=#{valid?} errors=#{errors.size}>"
end

#international?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/rightmove_blm/document.rb', line 77

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

#rowsObject



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

def rows
  data
end

#to_blmObject



34
35
36
37
38
39
40
# File 'lib/rightmove_blm/document.rb', line 34

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

#to_sObject



30
31
32
# File 'lib/rightmove_blm/document.rb', line 30

def to_s
  inspect
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  errors.empty?
end

#versionObject



73
74
75
# File 'lib/rightmove_blm/document.rb', line 73

def version
  header[:version]
end