require 'watir-webdriver'

class Strava
	def initialize(user, pass)
		@user = user
		@pass = pass
	end	

	def login
		$browser = Watir::Browser.new
		$browser.goto  'https://www.strava.com/login'

  	login_form = $browser.fieldset
		login_form.text_field(id: 'email').set @user
		login_form.text_field(id: 'password').set @pass
		$browser.send_keys :return
		raise LoginNotSuccess, "Problem when login in!" if $browser.title.match("Dashboard").nil?
	end


	def training
		$browser.goto 'http://www.strava.com/athlete/training'

		table = $browser.table(css: '.activities.sortable.striped')
		table_header = {}
		table_header = table.rows[0].cells.map { | h | h.text unless h.text.empty? }
		table_header.compact!
		table_header.map! &:downcase
		table_header.map! &:to_sym

		ta = []
		i=0
		for i in 1..(table.rows.size-1) do
			ta << Hash[table_header.zip(
					table.rows[i].cells.map { | h | 
						h.text unless h.text.empty?
					}
				)]
		end

		p ta
	end


	class LoginNotSuccess < StandardError; end

end

# $browser.table(css: '.activities.sortable.striped').rows[1].cells.each{|a| p a.text}



strava = Strava.new('tiago.l.nobre+test@gmail.com', '123123')
strava.login
strava.training
