Class: WindsUpClient
- Inherits:
-
Object
- Object
- WindsUpClient
- Defined in:
- lib/winds-up-client.rb
Instance Method Summary collapse
- #display_favorite_spots ⇒ Object
- #favorites_spots ⇒ Object
-
#initialize(options) ⇒ WindsUpClient
constructor
A new instance of WindsUpClient.
- #join_rows(a, b) ⇒ Object
- #join_rows_ordered(a, b) ⇒ Object
- #lpass(what) ⇒ Object
- #parse_series(spot) ⇒ Object
- #parse_spot(spot) ⇒ Object
- #series_handlers ⇒ Object
- #spot_row(spot) ⇒ Object
Constructor Details
#initialize(options) ⇒ WindsUpClient
Returns a new instance of WindsUpClient.
13 14 15 16 |
# File 'lib/winds-up-client.rb', line 13 def initialize = .merge!({username: lpass(:username), password: lpass(:password)}) if [:lpass] end |
Instance Method Details
#display_favorite_spots ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/winds-up-client.rb', line 76 def display_favorite_spots spots = favorites_spots previous_rows = [] spots.each_with_index do |spot, i| if [:short] puts "#{spot[:title]}: #{spot[:wind]}" else rows = spot_row spot if i % 2 == 1 puts Terminal::Table.new :rows => join_rows(previous_rows, rows) end previous_rows = rows end end puts Terminal::Table.new :rows => previous_rows if spots.size % 2 == 1 and ![:short] end |
#favorites_spots ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/winds-up-client.rb', line 32 def favorites_spots agent = Mechanize.new website = 'https://www.winds-up.com' page = agent.get(website) form = page.form('formu_login') form["login_pseudo"] = [:username] form["login_passwd"] = [:password] agent.submit(form) page = agent.get(website + '/index.php?p=favoris') spots = page.search("div.mt3").reverse.map { |spot| parse_spot spot } end |
#join_rows(a, b) ⇒ Object
72 73 74 |
# File 'lib/winds-up-client.rb', line 72 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
66 67 68 69 70 |
# File 'lib/winds-up-client.rb', line 66 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 |
#lpass(what) ⇒ Object
9 10 11 |
# File 'lib/winds-up-client.rb', line 9 def lpass what `lpass show --#{what} winds-up.com`.chomp end |
#parse_series(spot) ⇒ Object
18 19 20 21 22 |
# File 'lib/winds-up-client.rb', line 18 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
24 25 26 27 28 29 30 |
# File 'lib/winds-up-client.rb', line 24 def parse_spot spot { title: spot.search("a.title").map { |title| title.children.text }, wind: spot.search("div.infosvent2").map { |info| info.children[1].text }[0], series: parse_series(spot) } end |
#series_handlers ⇒ Object
44 45 46 47 48 49 |
# File 'lib/winds-up-client.rb', line 44 def series_handlers { actual: (-> (x) {"#{"=" * x["low"]}#{"-" * (x["high"] - x["low"])} #{(x["high"] + x["low"])/2}"}), expected: (-> (x) {"#{ "*" * x["y"]} #{x["y"]} #{x["o"]}"}) } end |
#spot_row(spot) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/winds-up-client.rb', line 51 def spot_row spot title = [spot[:title], spot[:wind]] i = 0 sampling = 2 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 % sampling == 0 i += 1 end end rows << title end |