gorp 0.16.1 → 0.16.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +0 -9
- data/gorp.gemspec +3 -3
- data/lib/gorp/net.rb +163 -0
- data/lib/gorp/rails.rb +126 -0
- data/lib/version.rb +1 -1
- metadata +5 -1
data/Manifest
CHANGED
data/gorp.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{gorp}
|
5
|
-
s.version = "0.16.
|
5
|
+
s.version = "0.16.3"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Sam Ruby"]
|
@@ -16,8 +16,8 @@ Gem::Specification.new do |s|
|
|
16
16
|
assertions based on selections (typically CSS) against the generated HTML.
|
17
17
|
}
|
18
18
|
s.email = %q{rubys@intertwingly.net}
|
19
|
-
s.extra_rdoc_files = ["README", "lib/gorp.rb", "lib/gorp/edit.rb", "lib/gorp/env.rb", "lib/gorp/test.rb", "lib/version.rb"]
|
20
|
-
s.files = ["Manifest", "README", "Rakefile", "gorp.gemspec", "lib/gorp.rb", "lib/gorp/edit.rb", "lib/gorp/env.rb", "lib/gorp/test.rb", "lib/version.rb"]
|
19
|
+
s.extra_rdoc_files = ["README", "lib/gorp.rb", "lib/gorp/edit.rb", "lib/gorp/env.rb", "lib/gorp/net.rb", "lib/gorp/rails.rb", "lib/gorp/test.rb", "lib/version.rb"]
|
20
|
+
s.files = ["Manifest", "README", "Rakefile", "gorp.gemspec", "lib/gorp.rb", "lib/gorp/edit.rb", "lib/gorp/env.rb", "lib/gorp/net.rb", "lib/gorp/rails.rb", "lib/gorp/test.rb", "lib/version.rb"]
|
21
21
|
s.homepage = %q{http://github.com/rubys/gorp}
|
22
22
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Gorp", "--main", "README"]
|
23
23
|
s.require_paths = ["lib"]
|
data/lib/gorp/net.rb
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'cgi'
|
3
|
+
|
4
|
+
def snap response, form=nil
|
5
|
+
if response.content_type == 'text/plain' or response.content_type =~ /xml/
|
6
|
+
$x.div :class => 'body' do
|
7
|
+
response.body.split("\n").each do |line|
|
8
|
+
$x.pre line.chomp, :class=>'stdout'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
return
|
12
|
+
end
|
13
|
+
|
14
|
+
if response.body =~ /<body/
|
15
|
+
body = response.body
|
16
|
+
else
|
17
|
+
body = "<body>#{response.body}</body>"
|
18
|
+
end
|
19
|
+
|
20
|
+
begin
|
21
|
+
doc = xhtmlparse(body)
|
22
|
+
rescue
|
23
|
+
body.split("\n").each {|line| $x.pre line.chomp, :class=>'hilight'}
|
24
|
+
raise
|
25
|
+
end
|
26
|
+
|
27
|
+
title = doc.at('html/head/title').text rescue ''
|
28
|
+
body = doc.at('//body')
|
29
|
+
doc.search('//link[@rel="stylesheet"]').each do |sheet|
|
30
|
+
body.children.first.add_previous_sibling(sheet)
|
31
|
+
end
|
32
|
+
|
33
|
+
# ensure that textareas don't use the self-closing syntax
|
34
|
+
body.search('//textarea').each do |element|
|
35
|
+
element.content=''
|
36
|
+
end
|
37
|
+
|
38
|
+
if form
|
39
|
+
body.search('//input[@name]').each do |input|
|
40
|
+
input['value'] ||= form[input['name']].to_s
|
41
|
+
end
|
42
|
+
body.search('//textarea[@name]').each do |textarea|
|
43
|
+
textarea.content = form[textarea['name']].to_s if textarea.text.empty?
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
%w{ a[@href] form[@action] }.each do |xpath|
|
48
|
+
name = xpath[/@(\w+)/,1]
|
49
|
+
body.search("//#{xpath}").each do |element|
|
50
|
+
next if element[name] =~ /^http:\/\//
|
51
|
+
element[name] = URI.join("http://localhost:#{$PORT}/", element[name]).to_s
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
%w{ img[@src] }.each do |xpath|
|
56
|
+
name = xpath[/@(\w+)/,1]
|
57
|
+
body.search("//#{xpath}").each do |element|
|
58
|
+
if element[name][0] == ?/
|
59
|
+
element[name] = 'data' + element[name]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
attrs = {:class => 'body', :title => title}
|
65
|
+
attrs[:class] = 'traceback' if response.code == '500'
|
66
|
+
attrs[:id] = body['id'] if body['id']
|
67
|
+
$x.div(attrs) do
|
68
|
+
body.children.each do |child|
|
69
|
+
$x << child.to_xml unless child.instance_of?(Comment)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
$x.div '', :style => "clear: both"
|
73
|
+
end
|
74
|
+
|
75
|
+
def get path
|
76
|
+
post path, nil
|
77
|
+
end
|
78
|
+
|
79
|
+
def post path, form, options={}
|
80
|
+
log :get, path unless form
|
81
|
+
$x.pre "get #{path}", :class=>'stdin' unless options[:snapget] == false
|
82
|
+
|
83
|
+
if path.include? ':'
|
84
|
+
host, port, path = URI.parse(path).select(:host, :port, :path)
|
85
|
+
else
|
86
|
+
host, port = '127.0.0.1', $PORT
|
87
|
+
end
|
88
|
+
|
89
|
+
Net::HTTP.start(host, port) do |http|
|
90
|
+
get = Net::HTTP::Get.new(path)
|
91
|
+
get['Cookie'] = $COOKIE if $COOKIE
|
92
|
+
response = http.request(get)
|
93
|
+
snap response, form unless options[:snapget] == false
|
94
|
+
$COOKIE = response.response['set-cookie'] if response.response['set-cookie']
|
95
|
+
|
96
|
+
if form
|
97
|
+
body = xhtmlparse(response.body).at('//body')
|
98
|
+
body = xhtmlparse(response.body).root unless body
|
99
|
+
xforms = body.search('//form')
|
100
|
+
|
101
|
+
# find matching button by action
|
102
|
+
xform = xforms.find do |element|
|
103
|
+
next unless element.attribute('action').to_s.include?('?')
|
104
|
+
query = CGI.parse(URI.parse(element.attribute('action').to_s).query)
|
105
|
+
query.all? {|key,values| values.include?(form[key].to_s)}
|
106
|
+
end
|
107
|
+
|
108
|
+
# find matching button by input names
|
109
|
+
xform ||= xforms.find do |element|
|
110
|
+
form.all? do |name, value|
|
111
|
+
element.search('.//input | .//textarea | ..//select').any? do |input|
|
112
|
+
input.attribute('name').to_s==name.to_s
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
# match based on action itself
|
118
|
+
xform ||= xforms.find do |element|
|
119
|
+
form.all? do |name, value|
|
120
|
+
element.attribute('action').to_s.include?(path)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# look for a commit button
|
125
|
+
xform ||= xforms.find {|element| element.at('.//input[@name="commit"]')}
|
126
|
+
|
127
|
+
return unless xform
|
128
|
+
|
129
|
+
path = xform.attribute('action').to_s unless
|
130
|
+
xform.attribute('action').to_s.empty?
|
131
|
+
$x.pre "post #{path}", :class=>'stdin'
|
132
|
+
|
133
|
+
$x.ul do
|
134
|
+
form.each do |name, value|
|
135
|
+
$x.li "#{name} => #{value}"
|
136
|
+
end
|
137
|
+
|
138
|
+
xform.search('.//input[@type="hidden"]').each do |element|
|
139
|
+
# $x.li "#{element['name']} => #{element['value']}"
|
140
|
+
form[element['name']] ||= element['value']
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
log :post, path
|
145
|
+
post = Net::HTTP::Post.new(path)
|
146
|
+
post.form_data = form
|
147
|
+
post['Cookie'] = $COOKIE
|
148
|
+
response=http.request(post)
|
149
|
+
snap response
|
150
|
+
end
|
151
|
+
|
152
|
+
if response.code == '302'
|
153
|
+
$COOKIE=response.response['set-cookie'] if response.response['set-cookie']
|
154
|
+
path = response['Location']
|
155
|
+
$x.pre "get #{path}", :class=>'stdin'
|
156
|
+
get = Net::HTTP::Get.new(path)
|
157
|
+
get['Cookie'] = $COOKIE if $COOKIE
|
158
|
+
response = http.request(get)
|
159
|
+
snap response
|
160
|
+
$COOKIE=response.response['set-cookie'] if response.response['set-cookie']
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
data/lib/gorp/rails.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'open3'
|
3
|
+
require 'builder'
|
4
|
+
require 'stringio'
|
5
|
+
require 'time'
|
6
|
+
|
7
|
+
# verify that port is available for testing
|
8
|
+
if (Net::HTTP.get_response('localhost','/',$PORT).code == '200' rescue false)
|
9
|
+
STDERR.puts "local server already running on port #{$PORT}"
|
10
|
+
exit
|
11
|
+
end
|
12
|
+
|
13
|
+
# select a version of Rails
|
14
|
+
if ARGV.first =~ /^_\d[.\d]*_$/
|
15
|
+
$rails = "rails #{ARGV.first}"
|
16
|
+
elsif File.directory?(ARGV.first.to_s)
|
17
|
+
$rails = ARGV.first
|
18
|
+
$rails = File.join($rails,'rails') if
|
19
|
+
File.directory?(File.join($rails,'rails'))
|
20
|
+
$rails = File.expand_path($rails)
|
21
|
+
else
|
22
|
+
$rails = 'rails'
|
23
|
+
end
|
24
|
+
|
25
|
+
# determine which version of rails is running
|
26
|
+
def which_rails rails
|
27
|
+
railties = File.join(rails, 'railties', 'bin', 'rails')
|
28
|
+
rails = railties if File.exists?(railties)
|
29
|
+
if File.exists?(rails)
|
30
|
+
firstline = open(rails) {|file| file.readlines.first}
|
31
|
+
rails = 'ruby ' + rails unless firstline =~ /^#!/
|
32
|
+
end
|
33
|
+
rails
|
34
|
+
end
|
35
|
+
|
36
|
+
# run rails as a command
|
37
|
+
def rails name, app=nil
|
38
|
+
Dir.chdir($WORK)
|
39
|
+
FileUtils.rm_rf name
|
40
|
+
log :rails, name
|
41
|
+
|
42
|
+
# determine how to invoke rails
|
43
|
+
rails = which_rails $rails
|
44
|
+
|
45
|
+
$x.pre "#{rails} #{name}", :class=>'stdin'
|
46
|
+
popen3 "#{rails} #{name}"
|
47
|
+
|
48
|
+
# canonicalize the reference to Ruby
|
49
|
+
Dir["#{name}/script/**/*"].each do |script|
|
50
|
+
next if File.directory? script
|
51
|
+
code = open(script) {|file| file.read}
|
52
|
+
code.sub! /^#!.*/, '#!/usr/bin/env ruby'
|
53
|
+
open(script,'w') {|file| file.write code}
|
54
|
+
end
|
55
|
+
|
56
|
+
cmd "mkdir #{name}" unless File.exist?(name)
|
57
|
+
Dir.chdir(name)
|
58
|
+
FileUtils.rm_rf 'public/.htaccess'
|
59
|
+
|
60
|
+
cmd 'rake rails:freeze:edge' if ARGV.include? 'edge'
|
61
|
+
|
62
|
+
if $rails != 'rails' and File.directory?($rails)
|
63
|
+
cmd "mkdir vendor" unless File.exist?('vendor')
|
64
|
+
cmd "ln -s #{$rails} vendor/rails"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# start/restart a rails server in a separate process
|
69
|
+
def restart_server
|
70
|
+
log :server, 'restart'
|
71
|
+
if $server
|
72
|
+
$x.h3 'Restart the server.'
|
73
|
+
Process.kill "INT", $server
|
74
|
+
Process.wait($server)
|
75
|
+
else
|
76
|
+
$x.h3 'Start the server.'
|
77
|
+
end
|
78
|
+
|
79
|
+
$server = fork
|
80
|
+
if $server
|
81
|
+
# wait for server to start
|
82
|
+
60.times do
|
83
|
+
sleep 0.5
|
84
|
+
begin
|
85
|
+
status = Net::HTTP.get_response('localhost','/',$PORT).code
|
86
|
+
break if %(200 404).include? status
|
87
|
+
rescue Errno::ECONNREFUSED
|
88
|
+
end
|
89
|
+
end
|
90
|
+
else
|
91
|
+
#
|
92
|
+
# For unknown reason, the below produces:
|
93
|
+
# undefined method `chomp' for nil:NilClass (NoMethodError)
|
94
|
+
# from rails/actionpack/lib/action_dispatch/middleware/static.rb:13
|
95
|
+
# path = env['PATH_INFO'].chomp('/')
|
96
|
+
#
|
97
|
+
# STDOUT.reopen '/dev/null', 'a'
|
98
|
+
# exec "#{$ruby} script/server --port #{$PORT}"
|
99
|
+
|
100
|
+
# alternatives to the above, with backtrace
|
101
|
+
begin
|
102
|
+
if File.exist?('config.ru')
|
103
|
+
require 'rack'
|
104
|
+
server = Rack::Builder.new {eval(open('config.ru') {|file| file.read})}
|
105
|
+
Rack::Handler::WEBrick.run(server, :Port => $PORT)
|
106
|
+
else
|
107
|
+
ARGV.clear.unshift('--port', $PORT.to_s)
|
108
|
+
|
109
|
+
# start server, redirecting stdout to a string
|
110
|
+
$stdout = StringIO.open('','w')
|
111
|
+
require './config/boot'
|
112
|
+
if Rails::VERSION::MAJOR == 2
|
113
|
+
require 'commands/server'
|
114
|
+
else
|
115
|
+
require 'rails/commands/server'
|
116
|
+
Rails::Server.start
|
117
|
+
end
|
118
|
+
end
|
119
|
+
rescue
|
120
|
+
STDERR.puts $!
|
121
|
+
$!.backtrace.each {|method| STDERR.puts "\tfrom " + method}
|
122
|
+
ensure
|
123
|
+
Process.exit!
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gorp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.16.
|
4
|
+
version: 0.16.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Ruby
|
@@ -123,6 +123,8 @@ extra_rdoc_files:
|
|
123
123
|
- lib/gorp.rb
|
124
124
|
- lib/gorp/edit.rb
|
125
125
|
- lib/gorp/env.rb
|
126
|
+
- lib/gorp/net.rb
|
127
|
+
- lib/gorp/rails.rb
|
126
128
|
- lib/gorp/test.rb
|
127
129
|
- lib/version.rb
|
128
130
|
files:
|
@@ -133,6 +135,8 @@ files:
|
|
133
135
|
- lib/gorp.rb
|
134
136
|
- lib/gorp/edit.rb
|
135
137
|
- lib/gorp/env.rb
|
138
|
+
- lib/gorp/net.rb
|
139
|
+
- lib/gorp/rails.rb
|
136
140
|
- lib/gorp/test.rb
|
137
141
|
- lib/version.rb
|
138
142
|
has_rdoc: true
|