Class: Taza::Site

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

Overview

An abstraction of a website, but more really a container for a sites pages.

You can generate a site by performing the following command:

$ ./script/generate site google

This will generate a site file for google, a flows folder, and a pages folder in lib

Example:

require 'taza'

class Google < Taza::Site

end

Constant Summary collapse

@@before_browser_closes =
Proc.new {}
@@donot_close_browser =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, &block) ⇒ Site

A site can be called a few different ways

The following example creates a new browser object and closes it:

Google.new do
  google.search.set "taza"
  google.submit.click
end

This example will create a browser object but not close it:

Google.new.search.set "taza"

Sites can take a couple of parameters in the constructor:

:browser => a browser object to act on instead of creating one automatically
:url => the url of where to start the site


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/taza/site.rb', line 51

def initialize(params={},&block)
  @module_name = self.class.parent.to_s
  @class_name  = self.class.to_s.split("::").last
  define_site_pages
  define_flows
  config = Settings.config(@class_name)
  if params[:browser]
    @browser = params[:browser]
  else
    @browser = Browser.create(config)
    @i_created_browser = true
  end
  @browser.goto(params[:url] || config[:url]) unless params[:url] == false
  execute_block_and_close_browser(browser,&block) if block_given?
end

Instance Attribute Details

#browserObject

Returns the value of attribute browser.



35
36
37
# File 'lib/taza/site.rb', line 35

def browser
  @browser
end

Class Method Details

.before_browser_closes(&block) ⇒ Object

Use this to do something with the browser before it closes, but note that it is a class method which means that this will get called for any instance of a site.

Here’s an example of how you might use it to print the DOM output of a browser before it closes:

Taza::Site.before_browser_closes do |browser|
  puts browser.html
end


28
29
30
# File 'lib/taza/site.rb', line 28

def self.before_browser_closes(&block)
  @@before_browser_closes = block
end

.donot_close_browserObject



32
33
34
# File 'lib/taza/site.rb', line 32

def self.donot_close_browser
  @@donot_close_browser = true
end

.settingsObject

:nodoc:



81
82
83
# File 'lib/taza/site.rb', line 81

def self.settings # :nodoc:
  Taza::Settings.site_file(self.name.to_s.split("::").last)
end

Instance Method Details

#base_pathObject

:nodoc:



144
145
146
# File 'lib/taza/site.rb', line 144

def base_path # :nodoc:
  '.'
end

#close_browser_and_raise_if(original_error) ⇒ Object

:nodoc:



85
86
87
88
89
90
91
# File 'lib/taza/site.rb', line 85

def close_browser_and_raise_if original_error # :nodoc:
  begin
    @browser.close if (@i_created_browser && !@@donot_close_browser)
  ensure
    raise original_error if original_error
  end
end

#define_flowsObject

:nodoc:



109
110
111
112
113
# File 'lib/taza/site.rb', line 109

def define_flows # :nodoc:
  Dir.glob(flows_path) do |file|
    require file
  end
end

#define_site_pagesObject

:nodoc:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/taza/site.rb', line 93

def define_site_pages # :nodoc:
  Dir.glob(pages_path) do |file|
    require file
    page_name = File.basename(file,'.rb')
    page_class = "#{@module_name}::#{page_name.camelize}"
    self.class.class_eval <<-EOS
    def #{page_name}(page_module = nil)
      page = '#{page_class}'.constantize.new(page_module)
      page.browser = @browser
      yield page if block_given?
      page
    end
    EOS
  end
end

#execute_block_and_close_browser(browser) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/taza/site.rb', line 67

def execute_block_and_close_browser(browser)
  begin
    yield self
  rescue => site_block_exception
  ensure
    begin
      @@before_browser_closes.call(browser)
    rescue => before_browser_closes_block_exception
      "" # so basically rcov has a bug where it would insist this block is uncovered when empty
    end
    close_browser_and_raise_if site_block_exception || before_browser_closes_block_exception
  end
end

#flows_pathObject

:nodoc:



136
137
138
# File 'lib/taza/site.rb', line 136

def flows_path # :nodoc:
  File.join(path,'flows','**','*.rb')
end

#pages_pathObject

This is used to call a flow belonging to the site

Example:

Google.new do |google|
  google.flow(:perform_search, :query => "taza")
end

Where the flow would be defined under lib/sites/google/flows/perform_search.rb and look like:

class PerformSearch < Taza::Flow
  alias :google :site

  def run(params={})
    google.search.set params[:query]
    google.submit.click
  end
end


132
133
134
# File 'lib/taza/site.rb', line 132

def pages_path # :nodoc:
  File.join(path,'pages','**','*.rb')
end

#pathObject

:nodoc:



140
141
142
# File 'lib/taza/site.rb', line 140

def path # :nodoc:
  File.join(base_path,'lib','sites',@class_name.underscore)
end