Class: SycLink::Designer

Inherits:
Object
  • Object
show all
Includes:
Infrastructure
Defined in:
lib/syclink/designer.rb

Overview

Creates a designer that acts as a proxy between the user and the website. The designer will create a website with the provided title.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Infrastructure

#copy_file_if_missing, #create_directory_if_missing, #html_file, #load_config, #yaml_file

Instance Attribute Details

#websiteObject

The website the designer is working on



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

def website
  @website
end

Instance Method Details

Adds a link based on the provided arguments to the website



27
28
29
# File 'lib/syclink/designer.rb', line 27

def add_link(url, args = {})
  website.add_link(Link.new(url, args))
end

Reads arguments from a CSV file and creates links accordingly. The links are added to the websie



33
34
35
36
37
38
39
40
41
# File 'lib/syclink/designer.rb', line 33

def add_links_from_file(file)
  File.foreach(file) do |line|
    next if line.chomp.empty?
    url, name, description, tag = line.chomp.split(';')
    website.add_link(Link.new(url, { name: name,
                                     description: description,
                                     tag: tag }))
  end
end

#create_website(directory, template_filename) ⇒ Object

Creates the html representation of the website. The website is saved to the directory with websites title and needs an erb-template where the links are integrated to. An example template can be found at templates/syclink.html.erb



152
153
154
155
156
157
# File 'lib/syclink/designer.rb', line 152

def create_website(directory, template_filename)
  template = File.read(template_filename)
  File.open(html_file(directory, website.title), 'w') do |f|
    f.puts website.to_html(template)
  end 
end

#delete_website(directory) ⇒ Object

Deletes the website if it already exists



142
143
144
145
146
# File 'lib/syclink/designer.rb', line 142

def delete_website(directory)
  if File.exists? yaml_file(directory, website.title)
    FileUtils.rm(yaml_file(directory, website.title)) 
  end
end

#export(format) ⇒ Object

Export links to specified format



51
52
53
54
55
56
57
58
# File 'lib/syclink/designer.rb', line 51

def export(format)
  message = "to_#{format.downcase}"
  if website.respond_to? message
    website.send(message)
  else
    raise "cannot export to #{format}"
  end
end

Finds links based on a search string



66
67
68
# File 'lib/syclink/designer.rb', line 66

def find_links(search)
  website.find_links(search)
end

Accepts and SycLink::Importer to import Links and add them to the website



44
45
46
47
48
# File 'lib/syclink/designer.rb', line 44

def import_links(importer)
  importer.links.each do |link|
    website.add_link(link)
  end
end

List links contained in the website and optionally filter on attributes



61
62
63
# File 'lib/syclink/designer.rb', line 61

def list_links(args = {})
  website.list_links(args)
end

#load_website(website) ⇒ Object

Loads a website based on the provided YAML-file and asigns it to the designer to operate on



137
138
139
# File 'lib/syclink/designer.rb', line 137

def load_website(website)
  @website = YAML.load_file(website)
end

Merge links with same URL



108
109
110
# File 'lib/syclink/designer.rb', line 108

def merge_links
  website.merge_links_on(:url)
end

#new_website(title = "SYC LINK") ⇒ Object

Creates a new website where designer can operate on



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

def new_website(title = "SYC LINK")
  @website = Website.new(title)
end

Deletes one or more links from the website. Returns the deleted links. Expects the links provided in an array



114
115
116
117
118
# File 'lib/syclink/designer.rb', line 114

def remove_links(urls)
  urls.map { |url| list_links({ url: url }) }.flatten.compact.each do |link|
    website.remove_link(link)
  end
end

Deletes links based on URLs that are provided in a file. Each URL has to be in one line



122
123
124
125
# File 'lib/syclink/designer.rb', line 122

def remove_links_from_file(file)
  urls = File.foreach(file).map { |url| url.chomp unless url.empty? }
  remove_links(urls)
end

Check links availability. Takes a filter which values to return and whether to return available, unavailable or available and unavailable links. In the following example only unavailable links’ url and response would be returned

report_links_availability(available: false, 
                          unavailable: false,
                          columns: "url,response")


77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/syclink/designer.rb', line 77

def report_links_availability(opts)
  cols = opts[:columns].gsub(/ /, "").split(',')
  website.report_links_availability.map do |url, response|
    result = if response == "200" and opts[:available]
               { "url" => url, "response" => response }
             elsif response != "200" and opts[:unavailable]
               { "url" => url, "response" => response }
             end
    next if result.nil?
    cols.inject([]) { |res, c| res << result[c.downcase] }
  end.compact
end

#save_website(directory) ⇒ Object

Saves the website to the specified directory with the downcased name of the website and the extension ‘website’. The website is save as YAML



129
130
131
132
133
# File 'lib/syclink/designer.rb', line 129

def save_website(directory)
  File.open(yaml_file(directory, website.title), 'w') do |f|
    YAML.dump(website, f)
  end
end

Updates a link. The link is identified by the URL. If there is more than one link with the same URL, only the first link is updated



92
93
94
# File 'lib/syclink/designer.rb', line 92

def update_link(url, args)
  website.find_links(url).first.update(args)
end

Updates links read from a file



97
98
99
100
101
102
103
104
105
# File 'lib/syclink/designer.rb', line 97

def update_links_from_file(file)
  File.foreach(file) do |line|
    next if line.chomp.empty?
    url, name, description, tag = line.chomp.split(';')
    website.find_links(url).first.update({ name:        name,
                                           description: description,
                                           tag:         tag })
  end
end