Class: WindsUpClient
- Inherits:
-
Object
- Object
- WindsUpClient
- Defined in:
- lib/winds-up-client.rb
Instance Method Summary collapse
- #bar(size, alternate, bg) ⇒ Object
- #display_favorite_spots ⇒ Object
- #expected(size) ⇒ Object
- #favorites_spots ⇒ Object
- #favorites_spots_text ⇒ Object
- #favorites_spots_text_with_cache ⇒ Object
- #high(size) ⇒ Object
-
#initialize(options) ⇒ WindsUpClient
constructor
A new instance of WindsUpClient.
- #join_rows(a, b) ⇒ Object
- #join_rows_ordered(a, b) ⇒ Object
- #low(size) ⇒ Object
- #lpass(what) ⇒ Object
- #parse_series(spot) ⇒ Object
- #parse_spot(spot) ⇒ Object
- #series_handlers ⇒ Object
- #spot_row(spot) ⇒ Object
- #to_arrows(cardinals) ⇒ Object
Constructor Details
#initialize(options) ⇒ WindsUpClient
Returns a new instance of WindsUpClient.
29 30 31 32 |
# File 'lib/winds-up-client.rb', line 29 def initialize @options = @options.merge!({username: lpass(:username), password: lpass(:password)}) if @options[:lpass] end |
Instance Method Details
#bar(size, alternate, bg) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/winds-up-client.rb', line 60 def size, alternate, bg if !@options[:nocolor] Paint[(" " * size), nil, bg] else alternate * size end end |
#display_favorite_spots ⇒ Object
162 163 164 165 166 167 168 |
# File 'lib/winds-up-client.rb', line 162 def display_favorite_spots if @options[:cache] puts favorites_spots_text_with_cache else puts favorites_spots_text end end |
#expected(size) ⇒ Object
76 77 78 |
# File 'lib/winds-up-client.rb', line 76 def expected size size, "*", :yellow end |
#favorites_spots ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# 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 ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/winds-up-client.rb', line 130 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 ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/winds-up-client.rb', line 154 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) ⇒ Object
72 73 74 |
# File 'lib/winds-up-client.rb', line 72 def high size size, "-", :green end |
#join_rows(a, b) ⇒ Object
126 127 128 |
# File 'lib/winds-up-client.rb', line 126 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) ⇒ Object
120 121 122 123 124 |
# File 'lib/winds-up-client.rb', line 120 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) ⇒ Object
68 69 70 |
# File 'lib/winds-up-client.rb', line 68 def low size size, "=", :blue end |
#lpass(what) ⇒ Object
25 26 27 |
# File 'lib/winds-up-client.rb', line 25 def lpass what `lpass show --#{what} winds-up.com`.chomp end |
#parse_series(spot) ⇒ Object
34 35 36 37 38 |
# 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) ⇒ Object
40 41 42 43 44 45 46 |
# 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 ⇒ Object
99 100 101 102 103 104 |
# File 'lib/winds-up-client.rb', line 99 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) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/winds-up-client.rb', line 106 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) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/winds-up-client.rb', line 80 def to_arrows(cardinals) { 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 |