Module: Stratagem::Crawler::Session

Includes:
ActionController::Integration::Runner, Authentication, HtmlUtils, RouteInvoker, TraceUtils
Included in:
SiteCrawler
Defined in:
lib/stratagem/crawler/session.rb

Overview

require ‘rack/lint’ module Rack

# Rack::Lint validates your application and the requests and
# responses according to the Rack spec.

class Lint
  alias_method :old_call, :call

  def call(env)
    data = env['rack.input']
    data.set_encoding(Encoding::ASCII_8BIT) if data.respond_to?(:set_encoding)

    status, headers, body = old_call(env)
    body.close # fix for Rack
    [status,headers,body]
  end
end

end

Constant Summary

Constants included from RouteInvoker

RouteInvoker::IGNORE_PARAMETERS

Constants included from HtmlUtils

HtmlUtils::INPUT_BUTTON, HtmlUtils::INPUT_RADIO, HtmlUtils::INPUT_TEXT, HtmlUtils::INPUT_TOGGLE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RouteInvoker

#call_route, #call_route!, #do_get, #do_post, #do_put, #visit

Methods included from ParameterResolver

#resolve_id_with_convention, #resolve_parameter_types, #resolve_with_convention, #resolve_with_instrumentation

Methods included from Authentication

#authenticate, #authentication, #find_login_form, #guess_login_model, #login, #logout, #populate_login_form, #reset_authentication, #user_model, #users

Methods included from TraceUtils

#model_invocations_for_request

Methods included from HtmlUtils

#find_login_form, #parse_forms

Instance Attribute Details

#aquifer(initial_capacity = 6) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/stratagem/crawler/session.rb', line 71

def aquifer(initial_capacity=6)
  @aquifer ||= begin
    Stratagem.logger.phase "mocking_models"

    if Stratagem.configuration.use_automatic_mocking
      Stratagem::AutoMock::Aquifer.init(application_model).fill_by_automock(initial_capacity)
    else
      # TODO - refactor out into another method
      Stratagem::AutoMock::Aquifer.init(application_model).fill_by_configuration(Stratagem.configuration.credentials)
    end
  end
end

Instance Method Details

#appObject

def self.app

# DEPRECATE Rails application fallback
# This should be set by the initializer
(defined?(Rails.application) && Rails.application) || nil

end



42
43
44
# File 'lib/stratagem/crawler/session.rb', line 42

def app
  (defined?(Rails.application) && Rails.application) || nil
end

#application_modelObject



67
68
69
# File 'lib/stratagem/crawler/session.rb', line 67

def application_model
  @application_model ||= Stratagem::ModelBuilder.new.run
end

#crawl(verbs = [:any,:get]) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/stratagem/crawler/session.rb', line 127

def crawl(verbs=[:any,:get])
  puts "crawling site for verbs #{verbs.inspect}"
  verbs = [verbs] unless verbs.kind_of?(Array)
  
  # grab all pages independently

  authentication_controller = session_controller()
  application_model.routes.each {|route_container|
    if authentication_controller && route_container.controller && (route_container.controller.klass == authentication_controller.klass)
      log "Skipping authentication routes #{route_container.route.to_s}"
    else
      if (application_model.routes.invalid.include?(route_container))
        log "Skipping invalid route #{route_container.route.to_s}"
      elsif (verbs.include?(route_container.verb))
        visit(route_container)
      else
        log "Skipping route with verb #{route_container.verb} - #{route_container.route.to_s}"
      end
    end
  }

  # establish edges on site graph
  site_model.pages.each do |page|
    site_model.add_edge(page, page.redirected_to, :redirect) if page.redirected?
  end

  site_model
end

#crawler_session(application_model = nil) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/stratagem/crawler/session.rb', line 58

def crawler_session(application_model=nil)
  aquifer # force fill
  @application_model = application_model if application_model
  open_session do |session|
    @session = session
    yield
  end
end

#log(msg) ⇒ Object



46
47
48
# File 'lib/stratagem/crawler/session.rb', line 46

def log(msg)
  Stratagem.logger.debug msg
end

#page_set(name) {|site_model| ... } ⇒ Object

Yields:



93
94
95
96
97
98
99
100
101
102
# File 'lib/stratagem/crawler/session.rb', line 93

def page_set(name, &block)
  Stratagem.logger.phase "traversing_site"
  
  log "---------------------------------------"
  log "Crawling page set #{name}"
  log "---------------------------------------"
  reset!
  site_models << Stratagem::Crawler::SiteModel::PageSet.new(name)
  yield site_model
end

#parameter_typesObject



50
51
52
# File 'lib/stratagem/crawler/session.rb', line 50

def parameter_types
  @parameter_types ||= {}
end


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/stratagem/crawler/session.rb', line 104

def print
  # print out pages and inbound / outbound links
  # debugger
  site_model.pages.each do |page|
    title = page.title
    title ||= page.redirected_to.url if page.redirected_to
    title ||= page.response.code
    log "Page: #{page.url} - #{title} - #{page.response.code}"
    begin
      page.outbound_edges.each do |edge|
       log "\tout: #{edge.to.url} - #{edge.to.title} - #{edge.to.route}"
      end
      page.inbound_edges.each do |edge|
       log "\tin: #{edge.from.url} - #{edge.from.title}"
      end
    rescue Exception
      puts $!.message
      puts $!.backtrace
    end
  end
  puts "end of site model"
end

#redirect_procObject



54
55
56
# File 'lib/stratagem/crawler/session.rb', line 54

def redirect_proc
  @redirect_proc ||= Proc.new {|redirect_url| handle_redirect(redirect_url) }
end

#session_controllerObject



156
157
158
159
160
161
162
163
164
# File 'lib/stratagem/crawler/session.rb', line 156

def session_controller
  authentication_controller = nil
  if (site_model.authentication)
    route = application_model.routes.recognize(authentication.)
    authentication_controller = route.controller if route
  end

  authentication_controller
end

#site_modelObject



88
89
90
91
# File 'lib/stratagem/crawler/session.rb', line 88

def site_model
  raise Stratagem::Crawler::CrawlError.new("Not within page set") unless site_models.last
  site_models.last
end

#site_modelsObject



84
85
86
# File 'lib/stratagem/crawler/session.rb', line 84

def site_models
  @site_models ||= []
end