Class: AskStack

Inherits:
Object
  • Object
show all
Defined in:
lib/ask_stack.rb

Constant Summary collapse

LOGIN_URL =
'http://stackoverflow.com/users/login'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ AskStack

Returns a new instance of AskStack.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ask_stack.rb', line 15

def initialize(config)
  @url = LOGIN_URL
  @browser = Selenium::Client::Driver.new \
    :host => "localhost",
    :port => 4444,
    :browser => "*firefox",
    :url => @url,
    :timeout_in_second => 60
  browser.start_new_browser_session
  browser.highlight_located_element=true
  browser.set_browser_log_level 'debug'
  @config = config
end

Instance Attribute Details

#browserObject Also known as: page

Returns the value of attribute browser.



10
11
12
# File 'lib/ask_stack.rb', line 10

def browser
  @browser
end

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/ask_stack.rb', line 10

def config
  @config
end

#selObject

Returns the value of attribute sel.



10
11
12
# File 'lib/ask_stack.rb', line 10

def sel
  @sel
end

#urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/ask_stack.rb', line 10

def url
  @url
end

Class Method Details

.runObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/ask_stack.rb', line 101

def self.run
  config = YAML::load(File.read("ask_stack.yml"))
  raw_question = STDIN.read.strip
  title = raw_question.split(/^\s*$/)[0]
  tags = raw_question.split(/^\s*$/)[-1]
  body = raw_question.split(/^\s*$/)[1..-2].join("\n")
  config[:question] = {title: title, body: body, tags: tags}
  so = AskStack.new config
  so.run
end

Instance Method Details

#complete_tagsObject

stackoverflow.com/filter/tags?callback=jQuery151044475735054025645_1303413170293&q=ruby&limit=6&timestamp=1303413245025&_=1303413245028 sample resp: stackoverflow.com/filter/tags?q=ruby&limit=6&timestamp=1303413245025&_=1303413245028

timestamp divide by 1000

jQuery151044475735054025645_1303413170293(“ruby|22235nruby-on-rails|36488nruby-on-rails-3|7173nrubygems|1569njruby|585nruby-on-rails-plugins|495”);[choi kaja~]$



96
97
98
# File 'lib/ask_stack.rb', line 96

def complete_tags

end

#fill_formObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/ask_stack.rb', line 43

def fill_form
  puts "Filling in question form"
  page.type "name=title", config[:question][:title]
  page.focus 'name=post-text'
  sleep 1
  page.type "name=post-text", config[:question][:body]
  page.type_keys "name=post-text", " "
  sleep 1
  page.type "name=tagnames", config[:question][:tags]
end

#loginObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ask_stack.rb', line 59

def 
  browser.open url
  puts "Signing in"
  browser.run_script "openid.signin('google')"
  puts "Loading Google signin"
  browser.wait_for_page_to_load(7)
  if !at_google_signin?
    puts "Error: Not the Google signin page. Tomfoolery afoot."
    exit
  end
  # TODO separate this Open ID provider signin to pluggable class
  puts "On Google signin page. Signing into Google"
  page.type "Email", config['username']
  page.type "Passwd", config['password']
  puts "Signing in..."
  page.click "signIn", :wait_for => :page
end

#runObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ask_stack.rb', line 29

def run
  browser.window_maximize
  
  page.click "nav-askquestion", :wait_for => :page
  fill_form
  sleep 3
  submit
  sleep 2
  puts "You question is posted at this URL" 
  puts browser.get_location 
  stop
  puts "Done"
end

#similar_questionsObject



85
86
87
# File 'lib/ask_stack.rb', line 85

def similar_questions

end

#stopObject



77
78
79
80
# File 'lib/ask_stack.rb', line 77

def stop
  browser.close
  browser.stop
end

#submitObject



54
55
56
57
# File 'lib/ask_stack.rb', line 54

def submit
  puts "Submitting"
  page.click 'id=submit-button', :wait_for => :page
end