Class: Superbot::Web

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

Constant Summary collapse

DEFAULT_OPTIONS =
{ webdriver_type: 'cloud' }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(teleport_options = {}) ⇒ Web

Returns a new instance of Web.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/superbot/web.rb', line 11

def initialize(teleport_options = {})
  @sinatra = Sinatra.new
  @sinatra.set :bind, ENV.fetch('SUPERBOT_BIND_ADDRESS', '127.0.0.1')
  @sinatra.set :silent_sinatra, true
  @sinatra.set :silent_webrick, true
  @sinatra.set :silent_access_log, false
  @sinatra.server_settings[:Silent] = true

  @sinatra.set :teleport_options, DEFAULT_OPTIONS.merge(teleport_options)
  @sinatra.set :webdriver_url, Superbot.webdriver_endpoint(@sinatra.settings.teleport_options[:webdriver_type])

  @sinatra.before do
    headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS'
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Headers'] = 'accept, authorization, origin'
  end

  @sinatra.options '*' do
    response.headers['Allow'] = 'HEAD,GET,PUT,DELETE,OPTIONS,POST'
    response.headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Cache-Control, Accept'
  end

  @sinatra.get "/__superbot/v1/ping" do
    "PONG"
  end

  @sinatra.register Superbot::Teleport::Web if defined?(Superbot::Teleport::Web)
  @sinatra.register Superbot::Cloud::Web if defined?(Superbot::Cloud::Web)
  @sinatra.register Superbot::Convert::Web if defined?(Superbot::Convert::Web)
end

Class Method Details

.run!(teleport_options = {}) ⇒ Object



42
43
44
# File 'lib/superbot/web.rb', line 42

def self.run!(teleport_options = {})
  new(teleport_options).tap(&:run!)
end

Instance Method Details

#quit!Object



66
67
68
# File 'lib/superbot/web.rb', line 66

def quit!
  @sinatra&.quit!
end

#run!Object



50
51
52
# File 'lib/superbot/web.rb', line 50

def run!
  @sinatra.run!
end

#run_async!Object



46
47
48
# File 'lib/superbot/web.rb', line 46

def run_async!
  @sinatra.run_async!
end

#run_async_after_running!Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/superbot/web.rb', line 54

def run_async_after_running!
  Thread.new do
    @sinatra.run!
  end

  loop do
    break if @sinatra.running?

    sleep 0.001
  end
end