Permalink
Browse files

BOOM

  • Loading branch information...
1 parent b3677e0 commit c6c73170ba1abab1c4aa45d00980db4f3a84a50c @trydionel committed Sep 19, 2010
View
@@ -1,4 +1,4 @@
-Copyright (c) 2009 Jeff Tucker
+Copyright (c) 2010 Jeff Tucker
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
@@ -0,0 +1,7 @@
+#!/usr/bin/env ruby -wKU
+
+$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
+
+require 'web_fixtures'
+
+exit WebFixtures::Base.new(ARGV[0]).run!
View
@@ -0,0 +1,3 @@
+require File.join(File.dirname(__FILE__), 'web_fixtures', 'request')
+require File.join(File.dirname(__FILE__), 'web_fixtures', 'dsl')
+require File.join(File.dirname(__FILE__), 'web_fixtures', 'base')
View
@@ -0,0 +1,48 @@
+module WebFixtures
+
+ def self.generate(&block)
+ WebFixtures::Base.new(&block)
+ end
+
+ class Base < Array
+
+ attr_accessor :filename
+
+ def initialize(filename = nil, &block)
+ @filename = filename
+ @block = block
+ end
+
+ def dsl
+ @dsl ||= WebFixtures::DSL.new(self)
+ end
+
+ def run!
+ if @block
+ dsl.instance_eval(&@block)
+ else
+ dsl.instance_eval(File.read(filename), filename)
+ end
+
+ store!
+ return 0
+ rescue Exception
+ return 1
+ end
+
+ def store!
+ credentials = {}
+ self.each do |request|
+ request.username = credentials[:username] if credentials[:username]
+ request.password = credentials[:password] if credentials[:password]
+
+ request.store!
+
+ credentials[:username] = request.username
+ credentials[:password] = request.password
+ end
+ end
+
+ end
+
+end
View
@@ -0,0 +1,53 @@
+module WebFixtures
+ class DSL
+
+ attr_accessor :base
+ attr_accessor :default_options
+
+ @@default_options = {
+ :include_headers => true,
+ :authenticate => false,
+ :root_path => "./fixtures"
+ }
+
+ def initialize(base, options = {})
+ @base = base
+ @default_options = @@default_options.merge(options)
+ end
+
+ def include_headers(choice)
+ default_options[:include_headers] = choice
+ end
+
+ def authenticate(choice)
+ default_options[:authenticate] = choice
+ end
+
+ def storage_path(path)
+ default_options[:root_path] = path
+ end
+
+ def get(url, options = {})
+ add_request(:get, url, options)
+ end
+
+ def post(url, options = {})
+ add_request(:post, url, options)
+ end
+
+ def put(url, options = {})
+ add_request(:put, url, options)
+ end
+
+ def delete(url, options = {})
+ add_request(:delete, url, options)
+ end
+
+ private
+
+ def add_request(method, url, options)
+ base << WebFixtures::Request.new(method, url, default_options.merge(options))
+ end
+
+ end
+end
@@ -0,0 +1,73 @@
+module WebFixtures
+ class Request
+
+ attr_accessor :method
+ attr_accessor :uri
+ attr_accessor :options
+
+ attr_accessor :input
+ attr_accessor :output
+
+ attr_accessor :username, :password
+
+ def initialize(method, uri, options = {}, input = STDIN, output = STDOUT)
+ @method = method
+ @uri = uri
+ @options = options
+
+ @input = input
+ @output = output
+ end
+
+ def store!
+ `mkdir -p \"#{storage_path}\" && #{curl_command}`
+ end
+
+ def curl_command
+ command = "curl -s"
+ command << " -i" if options[:include_headers]
+ command << " -u #{collect_username}:#{collect_password}" if options[:authenticate]
+ command << " -X #{method.to_s.upcase}" if method != :get
+ command << " -o \"#{output_file}\""
+ command << " \"#{uri}\""
+ command
+ end
+
+ def uri_components
+ @uri_components ||= uri.split('/')
+ end
+
+ def storage_path
+ root = options[:root_path] || "."
+ directory = uri_components[2]
+
+ File.join(root, directory)
+ end
+
+ def filename
+ title = uri_components[3..-1].join("_")
+ (title.empty? ? "root" : title) + ".txt"
+ end
+
+ def output_file
+ File.join(storage_path, filename)
+ end
+
+ def collect_username
+ return username if username
+ return nil unless options[:authenticate]
+
+ output.print "Username: "
+ @username = input.gets.chomp
+ end
+
+ def collect_password
+ return password if password
+ return nil unless options[:authenticate]
+
+ output.print "Password: "
+ @password = input.gets.chomp
+ end
+
+ end
+end
View
@@ -0,0 +1 @@
+get "http://www.google.com"
View
@@ -4,6 +4,13 @@
require 'spec'
require 'spec/autorun'
+# Prevent `cmd` from actually executing
+module Kernel
+ def `(cmd)
+ cmd
+ end
+end
+
Spec::Runner.configure do |config|
end
@@ -0,0 +1,101 @@
+require 'spec_helper'
+
+describe WebFixtures::Base do
+
+ it { should be_a(Array) }
+ its(:dsl) { should be_a(WebFixtures::DSL) }
+
+ describe "#run!" do
+
+ before { @file = File.join(File.dirname(__FILE__), '..', 'fixtures','google.txt') }
+ subject { WebFixtures::Base.new(@file) }
+
+ context "when given a file" do
+
+ it "should evaluate the given file" do
+ subject.dsl.should_receive(:instance_eval).
+ with(File.read(@file), @file)
+
+ subject.run!
+ end
+
+ it "should store the resulting requests" do
+ subject.should_receive(:store!)
+
+ subject.run!
+ end
+
+ it "should return 0" do
+ subject.run!.should == 0
+ end
+
+ it "should return 1 on errors" do
+ subject.stub!(:store!).and_raise(Exception)
+
+ subject.run!.should == 1
+ end
+
+ end
+
+ context "when given a block" do
+
+ before { @block = Proc.new { get "http://www.google.com" } }
+ subject { WebFixtures::Base.new(&@block) }
+
+ it "should evaluate the given block" do
+ subject.dsl.should_receive(:instance_eval)
+
+ subject.run!
+ end
+
+ it "should store the resulting requests" do
+ subject.should_receive(:store!)
+
+ subject.run!
+ end
+
+ it "should return 0" do
+ subject.run!.should == 0
+ end
+
+ it "should return 1 on errors" do
+ subject.stub!(:store!).and_raise(Exception)
+
+ subject.run!.should == 1
+ end
+
+ end
+
+ end
+
+ describe "#store!" do
+
+ before(:each) do
+ @stdin = StringIO.new
+ @stdout = StringIO.new
+
+ subject << WebFixtures::Request.new(:get, "http://www.google.com", { :authenticate => true }, @stdin, @stdout)
+ subject << WebFixtures::Request.new(:get, "http://www.google.com", {}, @stdin, @stdout)
+ subject << WebFixtures::Request.new(:get, "http://www.google.com", {}, @stdin, @stdout)
+
+ subject[0].username = "foo"
+ subject[0].password = "bar"
+ end
+
+ it "should call #store! on each of its elements" do
+ subject.each { |request| request.should_receive(:store!) }
+ subject.store!
+ end
+
+ it "should pass credentials along to the following requests" do
+ subject[1...-1].each do |request|
+ request.should_receive(:username=).with("foo")
+ request.should_receive(:password=).with("bar")
+ end
+
+ subject.store!
+ end
+
+ end
+
+end
@@ -0,0 +1,60 @@
+require 'spec_helper'
+
+describe WebFixtures::DSL do
+
+ before(:each) do
+ @base = WebFixtures::Base.new
+ end
+
+ subject do
+ WebFixtures::DSL.new(@base)
+ end
+
+ it "#include_headers should set the :include_headers option" do
+ subject.include_headers true
+ subject.default_options[:include_headers].should be_true
+ end
+
+ it "#authenticate should set the :authenticate option" do
+ subject.authenticate true
+ subject.default_options[:authenticate].should be_true
+ end
+
+ it "#storage_path should set the :root_path option" do
+ subject.storage_path './foo/bar'
+ subject.default_options[:root_path].should == "./foo/bar"
+ end
+
+ [:get, :post, :put, :delete].each do |method|
+
+ describe "##{method}" do
+
+ it "should build a new request object" do
+ WebFixtures::Request.should_receive(:new).
+ with(method,
+ "http://www.google.com",
+ subject.default_options)
+
+ subject.send method, "http://www.google.com"
+ end
+
+ it "should add the request to the base" do
+ expect {
+ subject.send method, "http://www.google.com"
+ }.to change(@base, :size).by(1)
+ end
+
+ it "should allow overrides for default options" do
+ WebFixtures::Request.should_receive(:new).
+ with(method,
+ "http://www.google.com",
+ hash_including(:authenticate => true))
+
+ subject.send method, "http://www.google.com", :authenticate => true
+ end
+
+ end
+
+ end
+
+end
Oops, something went wrong.

0 comments on commit c6c7317

Please sign in to comment.