Class: SycLink::Website

Inherits:
Object
  • Object
show all
Includes:
Exporter, Formatter
Defined in:
lib/syclink/website.rb

Overview

A Website is organizing a link list. The links can be added, updated and removed. It is also possible to search for links. And finally an html representation of the Website can be created.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Formatter

#cut, #extract_columns, #formatter_string, #max_column_widths, #print_header, #print_horizontal_line, #print_table, #scale_widths, #table, #table_of_array

Methods included from Exporter

#to_csv, #to_html

Constructor Details

#initialize(title = "Link List") ⇒ Website

Create a new Website



22
23
24
25
# File 'lib/syclink/website.rb', line 22

def initialize(title = "Link List")
  @links = []
  @title = title
end

Instance Attribute Details

The links of the website



17
18
19
# File 'lib/syclink/website.rb', line 17

def links
  @links
end

#titleObject (readonly)

The title of the website



19
20
21
# File 'lib/syclink/website.rb', line 19

def title
  @title
end

Instance Method Details

Add a link to the website



28
29
30
# File 'lib/syclink/website.rb', line 28

def add_link(link)
  links << link
end

Finds all links that contain the search string



53
54
55
# File 'lib/syclink/website.rb', line 53

def find_links(search)
  links.select { |link| link.contains? search }
end

List all attributes of the links



106
107
108
# File 'lib/syclink/website.rb', line 106

def link_attribute_list(attribute, separator = nil)
  links.map {|link| link.send(attribute).split(separator)}.flatten.uniq.sort
end

Create multiple Links based on the attribute provided. The specified spearator will splitt the attribute value in distinct values and for each different value a Link will be created



92
93
94
95
96
97
98
# File 'lib/syclink/website.rb', line 92

def links_duplicate_on(attribute, separator)
  links.map do  |link|
    link.send(attribute).split(separator).collect do |value|
      link.dup.update(Hash[attribute, value])
    end
  end.flatten
end

Groups the links on the provided attribute. If no links array is provided the links from self are used



76
77
78
79
80
# File 'lib/syclink/website.rb', line 76

def links_group_by(attribute, linkz = links)
  linkz.map      { |link| { key: link.send(attribute), link: link } }
       .group_by { |entry| entry[:key] }
       .each     { |key, link| link.map! { |l| l[:link] }}
end

Groups the links on the provided attribute. If the attribute’s value contains the provided separator, the value is split up and each of the values is used as group key



85
86
87
# File 'lib/syclink/website.rb', line 85

def links_group_by_separated(attribute, separator)
  links_group_by(attribute, links_duplicate_on(attribute, separator))
end

List links that match the attributes



38
39
40
41
42
43
44
# File 'lib/syclink/website.rb', line 38

def list_links(args = {})
  if args.empty?
    links
  else
    links.select { |link| link.match? args }
  end
end

Merge links based on the provided attribue to one link by combining the values. The first link will be updated and the obsolete links are deleted and will be returned



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/syclink/website.rb', line 60

def merge_links_on(attribute, concat_string = ',')
  links_group_by(attribute)
       .select { |key, link_list| links.size > 1 }
       .map do |key, link_list| 
          merge_attributes = Link::ATTRS - [attribute]
          link_list.first
               .update(Hash[extract_columns(link_list, merge_attributes)
                            .map { |c| c.uniq.join(concat_string) }
                            .collect { |v| [merge_attributes.shift, v] }])
          link_list.shift
          link_list.each { |link| links.delete(link) }
        end
end

Remove a link from the website



33
34
35
# File 'lib/syclink/website.rb', line 33

def remove_link(link)
  links.delete(link)
end

Check availability of links. Returns the links’ url with code ‘200’ if reachable other wise with code ‘Error’.



48
49
50
# File 'lib/syclink/website.rb', line 48

def report_links_availability
  links.map { |link| [link.url, link.response] }
end

#rowsObject

Return an array of all link values as rows



101
102
103
# File 'lib/syclink/website.rb', line 101

def rows
  links.map { |link| link.row }
end