使用 sinatra 存储和输出格式化数组内容

发布于 2025-01-01 14:31:55 字数 1213 浏览 0 评论 0原文

环境: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妳是的陽光 2025-01-08 14:31:55

创建一个 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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文