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

#adjust_for_invalid_title(rows) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/majestic_seo/api/data_table.rb', line 81

def adjust_for_invalid_title(rows)
  if (rows.size > @headers.size)
    difference          =   rows.size - @headers.size
    index               =   @headers.index("Title")
    
    title               =   rows[index]
    remaining_title     =   ""
    
    1.upto(difference) do |diff_index|
      remaining_title  +=   rows.delete_at(index+1)
    end
    
    title              +=   remaining_title
    rows[index]         =   title
  end
  
  return rows
end

#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
78
79
# 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, true)
    splitted_row    =   adjust_for_invalid_title(splitted_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, remove_excess_separators = false) ⇒ Object



100
101
102
103
# File 'lib/majestic_seo/api/data_table.rb', line 100

def split(text, remove_excess_separators = false)
  text        =   text.gsub(/\|{2,}/i, "|") if remove_excess_separators
  splitted    =   text.split(/\|(?!\|)/)
end