使用 sinatra 存储和输出格式化数组内容
环境:Ruby 1.9.2
我是 Ruby/Sinatra 的新手,正在创建一个概念验证 Web 应用程序。目的相当简单:用户输入域列表,脚本首先检查其 mx 记录,如果满足条件,则提取域联系信息。我相当确定我不会适当地存储数据,而是正在寻找一种更优雅的解决方案,该解决方案将使我能够对结果进行样式设置,以便将域、电子邮件和名称分组在一起。
#!/usr/bin/env ruby
require "sinatra/base"
require 'rubygems'
require 'haml'
require 'sinatra'
require 'whois'
get '/' do
haml :index
end
post '/' do
@host = params[:host]
@host.split('\n')
@email = Array.new
@name = Array.new
@domain = Array.new
@host.each_line {|i|
if %x[dig -t mx #{i.chomp.gsub('www.', '')} | grep -i mx | grep -i google].empty?
puts "empty"
else
@domain << i.chomp.gsub('www.','')
@email << (Whois.whois(i.chomp.gsub('www.',''))).technical_contact.email
@name << (Whois.whois(i.chomp.gsub('www.',''))).technical_contact.name
end
}
haml :index
end
__END__
@@ layout
%html
%head
%title Gcrawl
%body
#header
%h1 Gcrawl
#content
=yield
%footer
@@ index
%p
Welcome to Gcrawl
%form(action='/' method='POST')
%textarea{:rows => '12', :cols => '40', :name => 'host'}
%input(type='submit')
- if defined?(@email)
%h3= @domain
%h3= @email
%h3= @name
Environment: Ruby 1.9.2
I am new to Ruby/Sinatra and am creating a proof of concept web application. The purpose is fairly simple: A user inputs a list of domains and the script first checks their mx records, and if they meet the condition, pulls out the domain contact information. I am fairly sure that I am not going about storing the data appropriately and am looking for a more elegant solution that will enable me to style the results such that domain, email, and name are grouped together.
#!/usr/bin/env ruby
require "sinatra/base"
require 'rubygems'
require 'haml'
require 'sinatra'
require 'whois'
get '/' do
haml :index
end
post '/' do
@host = params[:host]
@host.split('\n')
@email = Array.new
@name = Array.new
@domain = Array.new
@host.each_line {|i|
if %x[dig -t mx #{i.chomp.gsub('www.', '')} | grep -i mx | grep -i google].empty?
puts "empty"
else
@domain << i.chomp.gsub('www.','')
@email << (Whois.whois(i.chomp.gsub('www.',''))).technical_contact.email
@name << (Whois.whois(i.chomp.gsub('www.',''))).technical_contact.name
end
}
haml :index
end
__END__
@@ layout
%html
%head
%title Gcrawl
%body
#header
%h1 Gcrawl
#content
=yield
%footer
@@ index
%p
Welcome to Gcrawl
%form(action='/' method='POST')
%textarea{:rows => '12', :cols => '40', :name => 'host'}
%input(type='submit')
- if defined?(@email)
%h3= @domain
%h3= @email
%h3= @name
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个
Record
类,其中包含特定条目的 @name、@domain 和 @email。因此,
Record
的每个实例都将拥有自己的名称、域名和电子邮件。将数组实现替换为类。如果您需要将记录存储在数据库中,请使用
ActiveRecord
。很高兴您从 Sinatra 开始,但如果您赶时间,您可以在一小时内让您的应用程序在 Rails 上运行。
编辑
Rails 入门教程/指南:
Create a class
Record
which will contain @name, @domain and @email for a particular entry.So, each instance of
Record
will have it's own name, domain and email.Replace the array implementation with a Class. If you need to store the records in a database, use
ActiveRecord
.It's good that you started with Sinatra, but if you are in a hurry, you can get your app running on Rails in an hour.
EDIT
Tutorials/Guides for getting started with Rails: