Sha256: f65f554170aceeab291b54d0df213cab6861dc35df20a88adb68413d99239467

Contents?: true

Size: 1.88 KB

Versions: 4

Compression:

Stored size: 1.88 KB

Contents

require 'open-uri'
require 'json'

module Helpers
  module Course
    COURSE_URL = URI('http://spidr.rubyforge.org/course/start.html')

    SPECS_URL = 'http://spidr.rubyforge.org/course/specs.json'

    def self.included(base)
      specs = JSON.parse(open(SPECS_URL).read)

      if specs.kind_of?(Array)
        specs.each do |spec|
          message = spec['message'].to_s.dump
          url = spec['url'].to_s.dump

          if spec['behavior'] == 'follow'
            base.module_eval %{
              it #{message} do
                should_visit_link(#{url})
              end
            }
          elsif spec['behavior'] == 'nofollow'
            base.module_eval %{
              it #{message} do
                should_visit_once(#{url})
              end
            }
          else
            link = spec['link'].to_s.dump

            base.module_eval %{
              it #{message} do
                should_ignore_link(#{link})
                should_ignore_link(#{url})
              end
            }
          end
        end
      end
    end

    def run_course
      Agent.start_at(COURSE_URL,:hosts => [COURSE_URL.host])
    end

    def visited_once?(link)
      url = COURSE_URL.merge(URI.encode(link))

      return @agent.visited_urls.select { |visited_url|
        visited_url == url
      }.length == 1
    end

    #
    # Returns +true+ if the agent has visited the specified _link_, returns
    # +false+ otherwise.
    #
    def visited_link?(link)
      url = COURSE_URL.merge(URI.encode(link))

      @agent.visited_urls.each do |visited_url|
        return true if visited_url == url
      end

      return false
    end

    def should_visit_link(link)
      visited_link?(link).should == true
    end

    def should_ignore_link(link)
      visited_link?(link).should == false
    end

    def should_visit_once(link)
      visited_once?(link).should == true
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spidr-0.1.3 spec/helpers/course.rb
spidr-0.1.5 spec/helpers/course.rb
spidr-0.1.2 spec/helpers/course.rb
spidr-0.1.4 spec/helpers/course.rb