Class: RightmoveBLM::Document
- Inherits:
-
Object
- Object
- RightmoveBLM::Document
- Defined in:
- lib/rightmove_blm/document.rb
Overview
A BLM document including its header, definition, and data content.
Constant Summary collapse
- BLM_FILE_SECTIONS =
rubocop:disable Metrics/ClassLength
%w[HEADER DEFINITION DATA END].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #definition ⇒ Object
- #errors ⇒ Object
- #header ⇒ Object
-
#initialize(source: nil, header: nil, definition: nil, data: nil, name: nil) ⇒ Document
constructor
A new instance of Document.
- #inspect(safe: false, error: nil) ⇒ Object
- #international? ⇒ Boolean
- #rows ⇒ Object
- #to_blm ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #version ⇒ Object
Constructor Details
#initialize(source: nil, header: nil, definition: nil, data: nil, name: nil) ⇒ Document
Returns a new instance of Document.
15 16 17 18 19 20 21 22 |
# File 'lib/rightmove_blm/document.rb', line 15 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? verify_source_file_structure(source) unless source.nil? end |
Class Method Details
.from_array_of_hashes(array, international: false) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/rightmove_blm/document.rb', line 8 def self.from_array_of_hashes(array, international: false) date = Time.now.utc.strftime('%d-%b-%Y %H:%M').upcase version = international ? '3i' : '3' header = { version: version, 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
#definition ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/rightmove_blm/document.rb', line 57 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 |
#errors ⇒ Object
69 70 71 |
# File 'lib/rightmove_blm/document.rb', line 69 def errors @errors ||= data.reject(&:valid?).flat_map(&:errors) end |
#header ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rightmove_blm/document.rb', line 46 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
24 25 26 27 28 29 30 31 32 |
# File 'lib/rightmove_blm/document.rb', line 24 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
81 82 83 |
# File 'lib/rightmove_blm/document.rb', line 81 def international? %w[H1 3I 3i].include?(version) end |
#rows ⇒ Object
65 66 67 |
# File 'lib/rightmove_blm/document.rb', line 65 def rows data end |
#to_blm ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/rightmove_blm/document.rb', line 38 def to_blm [ header_string, definition_string, data_string ].join("\n") end |
#to_s ⇒ Object
34 35 36 |
# File 'lib/rightmove_blm/document.rb', line 34 def to_s inspect end |
#valid? ⇒ Boolean
73 74 75 |
# File 'lib/rightmove_blm/document.rb', line 73 def valid? errors.empty? end |
#version ⇒ Object
77 78 79 |
# File 'lib/rightmove_blm/document.rb', line 77 def version header[:version] end |