Class: MajesticSeo::Api::DataTable

Inherits:
Object
  • Object
show all
Defined in:
lib/majestic_seo/api/data_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ DataTable

Returns a new instance of DataTable.



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/majestic_seo/api/data_table.rb', line 38

def initialize(node)
  @node       =   node

  if (@node)
    @name       =   @node["Name"]       ||  nil
    @row_count  =   @node["RowsCount"]  ||  0
    @headers    =   @node["Headers"]    ||  []

    @rows       =   []
    parse
  end
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



36
37
38
# File 'lib/majestic_seo/api/data_table.rb', line 36

def headers
  @headers
end

#nameObject

Returns the value of attribute name.



36
37
38
# File 'lib/majestic_seo/api/data_table.rb', line 36

def name
  @name
end

#nodeObject

Returns the value of attribute node.



36
37
38
# File 'lib/majestic_seo/api/data_table.rb', line 36

def node
  @node
end

#row_countObject

Returns the value of attribute row_count.



36
37
38
# File 'lib/majestic_seo/api/data_table.rb', line 36

def row_count
  @row_count
end

#rowsObject

Returns the value of attribute rows.



36
37
38
# File 'lib/majestic_seo/api/data_table.rb', line 36

def rows
  @rows
end

Instance Method Details

#parseObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/majestic_seo/api/data_table.rb', line 51

def parse
  rows = @node.xpath("Row")

  if (@headers && rows && rows.any?)
    @headers = split(@headers)

    rows.each do |row|
      parse_row(row)
    end
  end
end

#parse_row(row) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/majestic_seo/api/data_table.rb', line 63

def parse_row(row)
  if (row && row.content)
    row           =   row.content
    row_hash      =   {}
    splitted_row  =   split(row)

    @headers.each_with_index do |header, index|
      value               =   splitted_row[index].strip
      value               =   (value && value != "") ? value : nil
      row_hash[header]    =   value
    end

    @rows << row_hash if (row_hash && !row_hash.empty?)
  end
end

#split(text) ⇒ Object



79
80
81
# File 'lib/majestic_seo/api/data_table.rb', line 79

def split(text)
  splitted = text.split(/\|(?!\|)/)
end