0

I'm trying to get an example of the following code from github that looks to be a dead topic for my Linux/Ubuntu install. I have been trying to scrape data from my company intranet using "mechanize" see stack question for details. Since I'm not smart enough to figure a way around my login issue I thought I would try and feed data from an excel sheet as a work around until I can figure out the mechanize route. Once again I'm not smart enough to get the provided code to work on Linux because I'm getting the following error:

`kqueue=': kqueue is not supported on this platform (EventMachine::Unsupported)

If I'm understanding correctly from the information provided in the original source, the problem is that kqueue isn't supported in Linux. The OP states that inotify is an alternative but I've had no luck finding a similar example using it to display Excel in a widget.

Here is the code that is shown on GitHub and would like help converting it to work on Linux:

require 'roo'

EM.kqueue = EM.kqueue?
file_path = "#{Dir.pwd}/spreadsheet.xls"

def fetch_spreadsheet_data(path)
  s = Roo::Excel.new(path)
  send_event('valuation',   { current: s.cell(1, 2) })
end

module Handler
  def file_modified
    fetch_spreadsheet_data(path)
  end
end

fetch_spreadsheet_data(file_path)

EM.next_tick do
  EM.watch_file(file_path, Handler)
end
Community
  • 1
  • 1
rleigh
  • 61
  • 1
  • 8

2 Answers2

0

Okay, so I was able to get this working and to display my data on a Dashing Dashboard widget by doing the following:

First: I uploaded my spreadsheet.xls to the root directory of my dashboard.

Second: I replaced the /jobs/sample.rb code with:

#!/usr/bin/env ruby
require 'roo'


SCHEDULER.every '2s' do
file_path = "#{Dir.pwd}/spreadsheet.xls"

def fetch_spreadsheet_data(path)
  s = Roo::Excel.new(path)
  send_event('valuation',   { current: s.cell('B',49) })

end

module Handler
  def file_modified
    fetch_spreadsheet_data(path)
  end
end

fetch_spreadsheet_data(file_path)

end

Third: Make sure the /widgets/number is in your dashboard "this is part of the sample install".

Fourth: Add the following code to your /dashboards/sample.erb file "this is part of the sample install as well".

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
      <div data-id="valuation" data-view="Number" data-title="Current Valuation" data-prefix="$"></div>
    </li>

I used this source to help me better understand how Roo works. I tested my widget by changing my values and re-uploading the spreadsheet.xls to server and seen instant changes on my dashboard.

Hope this helps someone and I'm still looking for help to automate this process by scraping the data. Reference this if you can help.

Community
  • 1
  • 1
rleigh
  • 61
  • 1
  • 8
0

Thanks for sharing this code sample. I did not manage to make it work in my environment (Raspberry/Raspbian) but after some efforts I managed to come up something that works -- at least for me ;)

I had never worked with Ruby before this week, so this code may be a bit crappy. Please accept apologizes.

-- Christophe

require 'roo'
require 'rubygems'
require 'rb-inotify'

# Implement INotify::Notifier.watch as described here:
# https://www.go4expert.com/articles/track-file-changes-ruby-inotify-t30264/

file_path = "#{Dir.pwd}/datasheet.csv"

def fetch_spreadsheet_data(path)
  s = Roo::CSV.new(path)
  send_event('csvdata',   { value: s.cell(1, 1) })
end

SCHEDULER.every '5s' do

notifier = INotify::Notifier.new

notifier.watch(file_path, :modify) do |event|
    event.flags.each do |flag|
        ## convert to string
        flag = flag.to_s

        puts case flag
            when 'modify' then fetch_spreadsheet_data(file_path)
        end
    end
end

## loop, wait for events from inotify
notifier.process

end