class WindsUpClient
Public Class Methods
new(options)
click to toggle source
# File lib/winds-up-client.rb, line 29 def initialize options @options = options @options.merge!({username: lpass(:username), password: lpass(:password)}) if @options[:lpass] end
Public Instance Methods
bar(size, alternate, bg)
click to toggle source
# File lib/winds-up-client.rb, line 60 def bar size, alternate, bg if !@options[:nocolor] Paint[(" " * size), nil, bg] else alternate * size end end
display_favorite_spots()
click to toggle source
# File lib/winds-up-client.rb, line 163 def display_favorite_spots if @options[:cache] puts favorites_spots_text_with_cache else puts favorites_spots_text end end
expected(size)
click to toggle source
# File lib/winds-up-client.rb, line 76 def expected size bar size, "*", :yellow end
favorites_spots()
click to toggle source
# File lib/winds-up-client.rb, line 48 def favorites_spots agent = Mechanize.new website = 'https://www.winds-up.com' page = agent.get(website) form = page.form('formu_login') form["login_pseudo"] = @options[:username] form["login_passwd"] = @options[:password] agent.submit(form) page = agent.get(website + '/index.php?p=favoris') spots = page.search("div.mt3").reverse.map { |spot| parse_spot spot } end
favorites_spots_text()
click to toggle source
# File lib/winds-up-client.rb, line 131 def favorites_spots_text result = "" spots = favorites_spots previous_rows = [] spots.each_with_index do |spot, i| if @options[:spot].nil? or spot[:title].downcase.include?(@options[:spot]) if @options[:short] result += "#{spot[:title]}: #{to_arrows(spot[:wind])}\n" elsif @options[:ultrashort] result += "#{spot[:title][0]}#{to_arrows(spot[:wind]).sub(" nds", "").sub(" ", "")}" else rows = spot_row spot if i % 2 == 1 result += TerminalTable.new(:rows => join_rows(previous_rows, rows)).to_s result += "\n" end previous_rows = rows end end end result += TerminalTable.new(:rows => previous_rows).to_s if spots.size % 2 == 1 and !@options[:short] result + "\n" end
favorites_spots_text_with_cache()
click to toggle source
# File lib/winds-up-client.rb, line 155 def favorites_spots_text_with_cache path = "#{ENV['HOME']}/.local/share/winds-up-client.cache" if not File.exists?(path) or Time.now - File.mtime(path) > 60 File.write(path, favorites_spots_text) end File.read(path) end
high(size)
click to toggle source
# File lib/winds-up-client.rb, line 72 def high size bar size, "-", :green end
join_rows(a, b)
click to toggle source
# File lib/winds-up-client.rb, line 127 def join_rows a, b a.size > b.size ? join_rows_ordered(a, b) : join_rows_ordered(b, a) end
join_rows_ordered(a, b)
click to toggle source
# File lib/winds-up-client.rb, line 121 def join_rows_ordered a, b a.each_with_index.map do |a_row, i| i < b.size ? a_row + b[i] : a_row end end
low(size)
click to toggle source
# File lib/winds-up-client.rb, line 68 def low size bar size, "=", :blue end
lpass(what)
click to toggle source
# File lib/winds-up-client.rb, line 25 def lpass what `lpass show --#{what} winds-up.com`.chomp end
parse_series(spot)
click to toggle source
# File lib/winds-up-client.rb, line 34 def parse_series spot Hash[[:actual, :expected].zip(spot.search("script").map do |script| JSON.parse(script.children[0].text.match(/data: \[.*\]/)[0].gsub("data:", "").gsub(",}", "}").gsub(/(\w+):/, '"\1":')) end)] end
parse_spot(spot)
click to toggle source
# File lib/winds-up-client.rb, line 40 def parse_spot spot { title: spot.search("a.title").map { |title| title.children.text }[0], wind: spot.search("div.infosvent2").map { |info| info.children[1].text }[0], series: parse_series(spot) } end
series_handlers()
click to toggle source
# File lib/winds-up-client.rb, line 100 def series_handlers { actual: (-> (x) {"#{low(x["low"])}#{high(x["high"] - x["low"])} #{(x["high"] + x["low"])/2}"}), expected: (-> (x) {"#{ expected(x["y"])} #{x["y"]} #{to_arrows(x["o"])}"}) } end
spot_row(spot)
click to toggle source
# File lib/winds-up-client.rb, line 107 def spot_row spot title = [spot[:title], to_arrows(spot[:wind])] i = 0 rows = [] rows << title series_handlers.each do |kind, handler| spot[:series][kind].each do |value| rows << [Time.at(value["x"] / 1000).to_datetime.to_s.gsub(/\+.*/, ""), handler.call(value)] if i % @options[:sampling] == 0 i += 1 end end rows << title end
to_arrows(cardinals)
click to toggle source
# File lib/winds-up-client.rb, line 80 def to_arrows(cardinals) return "?" if cardinals.nil? { se: "↖", so: "↗", no: "↘", ne: "↙", }.map { |name, value| cardinals.sub!(name.to_s.upcase, value) } { e: "←", s: "↑", o: "→", n: "↓", }.map { |name, value| cardinals.sub!(name.to_s.upcase, value) } cardinals end