Class: Neverland::ActionDispatchScriptInjector

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

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ ActionDispatchScriptInjector

Returns a new instance of ActionDispatchScriptInjector.



22
23
24
25
26
# File 'lib/neverland/script_injector.rb', line 22

def initialize(response)
  @response = response
  @document = Nokogiri::HTML(@response.body)
  @head = @document.at('head')
end

Instance Method Details

#inject(params) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/neverland/script_injector.rb', line 28

def inject(params)
  if @head
    inject_script(:src => '/javascripts/neverland.js')

    if params.error_code
      inject_script(:body => <<SCRIPT)
Neverland.setError(#{params.error_code})
SCRIPT
    elsif params.latitude && params.longitude
      inject_script(:body => <<SCRIPT)
Neverland.setLatitude(#{params.latitude})
Neverland.setLongitude(#{params.longitude})
SCRIPT
    end

    @response.body = @document.to_s
  end

  @response
end