loop through file with hashes in ruby - ruby

I have a text file located in my project directory that contains the link of sites as well as the location of the logos. For example:
img1 = {name => "logo1", link => "http://example.com", imgpath => "img/logo1.png"}
img2 = {name => "logo2", link => "http://foobar.com", imgpath => "img/foobar.png"}
img3 = {name => "logo3", link => "http://foobar.com", imgpath => "img/foobar2.png"}
What I would like to do is loop through that text file to display each image with its link. I've searched all over and I can't seem to find a good example on how to do it. Here's the for loop I've got so far in my page:
- File.open("img/logos.txt").readlines.each do |name,link,imgpath|
.col-sm-6.col-md-4.col-lg-3
.item-work
.hover
%img{:alt => "Image", :src => "img/#{imgpath}"}/
%a.lightbox-image{:href => "#{link}", :title => "Image"}/
.overlay
.info
%a{:href => "#{name}"} #{name}
I'm sort of close, the page loads all the logos except the images aren't there and the links are associated with them. I know it's because I didn't call them correctly in my %img link and %a tags but I can't seem to figure it out. How would you suggest going about doing this?

#readlines is going to give you each line of the text file as a string. Based on your comment, this would word fine:
logos.txt:
logo1,http://example.com,img/logo1.png
logo2,http://foobar.com,img/foobar.png
logo3,http://foobar.com,img/foobar2.png
Haml:
- File.open("img/logos.txt").readlines.each do |line|
- img = line.split(',').map(&:chomp)
.col-sm-6.col-md-4.col-lg-3
.item-work
.hover
%img{:alt => "Image", :src => "img/#{img[2]}"}/
%a.lightbox-image{:href => "#{img[1]}", :title => "Image"}/
.overlay
.info
%a{:href => "#{img[0]}"} #{img[0]}
The second line is creating an array of values, like so:
[1] pry(main)> line
=> "logo1,http://example.com,img/logo1.png\n"
[2] pry(main)> line.split(',').map(&:chomp)
=> ["logo1", "http://example.com", "img/logo1.png"]

Related

Change deep_merge to Utils.deep_merge_hashes

I am using Octopress to generate static html pages. I tried to change the language of the dates using this instruction (it is in German but we need only the code). When I copy date.rb from this German website to my octopress/plugins, I have the following error: Liquid Exception: undefined method `deep_merge' for # in blog/path/to/post/index.html.
I can generate site if I comment out this part in date.rb:
def to_liquid
date_format = self.site.config['date_format']
self.data.deep_merge({
"title" => self.data['title'] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '),
"url" => self.url,
"date" => self.date,
# Monkey patch
"date_formatted" => format_date(self.date, date_format),
"updated_formatted" => self.data.has_key?('updated') ? format_date(self.data['updated'], date_format) : nil,
"id" => self.id,
"categories" => self.categories,
"next" => self.next,
"previous" => self.previous,
"tags" => self.tags,
"content" => self.content })
end
Then the language is changed for the dates in blog/archives, but not for the dates in posts. I found a similar problem which has been solved by changing deep_merge → Utils.deep_merge_hashes. So I understand that I need to do exactly the same in the piece of the code I presented above. I think it should be quite easy, but since I don't know Ruby, I didn't succeed yet. Could you please tell me how should I use Utils.deep_merge_hashes instead of deep_merge in this case?
This works (ruby 2.1.1 - Jekyll 2.5.3)
def to_liquid(attrs = nil)
date_format = self.site.config['date_format']
new_datas = {
"title" => self.data['title'] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '),
"url" => self.url,
"date" => self.date,
# Monkey patch
"date_formatted" => format_date(self.date, date_format),
"updated_formatted" => self.data.has_key?('updated') ? format_date(self.data['updated'], date_format) : nil,
"id" => self.id,
"categories" => self.categories,
"next" => self.next,
"previous" => self.previous,
"tags" => self.tags,
"content" => self.content }
Utils.deep_merge_hashes(self.data, new_datas)
end

How to I add a hyperlink to a cell in axlsx?

With the spreadsheet gem, you can run Spreadsheet::Link.new('http://hyperlinkhere.com', 'Some words') to make a spreadsheet with a cell containing the string "Some words" with a hyperlink leading to "http://hyperlinkhere.com."
What's the axlsx equivalent?
EDIT: What if I want to write a row with more than one cell?
With spreadsheet, you can do this:
newSheetRow[13] = Spreadsheet::Link.new('url.com','text')
newSheetRow[14] = 'some text'
How do I do that with axlsx's .add_row method?
You can add both links within workbook and URLs.
p = Axlsx::Package.new
book = p.workbook
book.add_worksheet(:name => 'hyperlinks') do |sheet|
# external references
sheet.add_row ['axlsx']
sheet.add_hyperlink :location => 'https://github.com/randym/axlsx', :ref => sheet.rows.first.cells.first
# internal references
sheet.add_hyperlink :location => "'Next Sheet'!A1", :ref => 'A2', :target => :sheet
sheet.add_row ['next sheet']
end

Selenium::WebDriver::Error::ElementNotVisibleError while looping to click button using Watir

I am trying to click a button on loop basis.
Let us say I have following code
if ind == 2
export_id = #browser.div(:id => pop_id).div(:class => /actionDropDownItem groupChild nonSelectable/,:title => "Export").id
#browser.div(:id => export_id).click
else
#browser.div(:id => pop_id).div(:class => /actionDropDownItem groupChild nonSelectable/,:title => "Export").click
end
But it shows error at index value "2" and the Error is
Selenium::WebDriver::Error::ElementNotVisibleError:
Element is not currently visible and so may not be interacted with
Can anyone help me in this case?
It would be great if you provide proper html .
Try this once , this would work for you
if ind == 2
export_id = #browser.div(:id => export_id)
export_id.click
else
#browser.div(:id => pop_id).div(:class => /actionDropDownItem groupChild nonSelectable/,:title => "Export").click
end

How to have different headers/footers for a big table in Ruby/Prawn?

I wanna generate a pdf with ruby and the prawn(0.8.4) gem. the first page of the pdf should have a different header/footer than the following pages. The data will be shown in a table, but the table is shown on multiple pages.
Example:
first page should have an header height of 60.mm
the table starts at the first page, below the header
on the second page there should be a header with a height of 30.mm
the table continues on the second page, below the smaller header
do you see my problem?
solved.
require "rubygems"
require "prawn"
require "prawn/core"
require "prawn/layout"
require "prawn/measurement_extensions"
Prawn::Document.generate("test.pdf", :page_size => "A3", :page_layout => :landscape, :margin => 0) do
padded_box 30.mm do
move_down(40.mm)
items = 100.times.map {|i| [i, i]}
table items, :border_style => :underline_header, :headers => ["Column#1", "Column#2"]
end
page_count.times do |i|
page_num = i+1
go_to_page(page_num)
if page_num == 1
# header of first page
text_box "header#1", :at => [30.mm, 290.mm], :size => 18
image "logo.png", :at => [12.mm,(297-15.78).mm]
else
# header 2..n
text_box "header#2..n", :at => [30.mm, 290.mm], :size => 12
end
end
end

Setting cell/column widths on a Prawn table

I'm making a little script with ruby which produces a week schedule PDF file, using Prawn as a PDF library and I'm struggling with styling the table. I'd like to set a static width for all the columns in the table so that the widths wouldn't depend on the contents of the cells.
I've read the documentation (lot of room for improvement there) from the Prawn project site and googled for a few hours, but I'm lost at how to set width for columns or cells in a table, or how to style the columns/cells in any way whatsoever. I do get a PDF file which has a grid layout though, the cells just vary in size a lot, which doesn't look that neat.
This didn't work:
Prawn::Document.generate(#filename, :page_size => 'A4', :page_layout => :landscape) do
table(course_matrix, :headers => HEADERS, :border_style => :grid, :row_colors => ['dddddd', 'eeeeee'], :column_widths => 50)
end
Here's the current version of my method to generate PDF, but it doesn't stylize the cells either:
def produce_pdf
course_matrix = DataParser.new.parse_for_pdf
Prawn::Document.generate(#filename, :page_size => 'A4', :page_layout => :landscape) do
table(course_matrix, :headers => HEADERS, :border_style => :grid, :row_colors => ['dddddd', 'eeeeee']) do |table|
table.cells.style { |cell| cell.width = 50 }
end
end
end
I do something like this:
pdf = Prawn::Document.new(
:page_size => 'A4',
:page_layout => :landscape,
:margin => [5.mm])
....
....
pdf.table(tbl_data) do
row(0).style(:background_color => 'dddddd', :size => 9, :align => :center, :font_style => :bold)
column(0).style(:background_color => 'dddddd', :size => 9, :padding_top => 20.mm, :font_style => :bold)
row(1).column(1..7).style(:size => 8, :padding => 3)
cells[0,0].background_color = 'ffffff'
row(0).height = 8.mm
row(1..3).height = 45.mm
column(0).width = 28.mm
column(1..7).width = 35.mm
row(1..3).column(6..7).borders = [:left, :right]
row(3).column(6..7).borders = [:left, :right, :bottom]
....
pdf.render()
More info here.
To set a static width for all the columns I do something like this:
REPORT_FIELDS = %w[DESCRIPTION PRICE DATE NOTE].freeze
A4_SIZE = 200.freeze
data = []
data << REPORT_FIELDS
... things happen ...
table(data, column_widths: (A4_SIZE/REPORT_FIELDS.size).mm))
In this case I wanted to set the table to fit the full page and with the cells with the same width.

Resources