在 Rails 中重新使用带有基本 ruby​​ 测试代码的单个文件?

发布于 2024-07-30 10:39:49 字数 3299 浏览 2 评论 0原文

我在单个文件 snippet.rb 中获取了此代码,它按预期运行。 该脚本来自 dzone 片段,用于获取当前 URL 的缩略图。

现在我想将此功能与 Rails 集成,但我不知道如何开始。 我应该将其放入 lib 目录中的某个 ruby​​ 文件中还是将其设为模块??? 我对 Ruby 不太熟悉,所以有人可以告诉我如何以及从哪里开始吗?

    require 'net/http'
    require 'rubygems'
    require 'xmlsimple'

    class Nailer

        @@api_baseurl = 'http://webthumb.bluga.net/api.php'
        @@api_key = 'YOUR-API-KEY'

        attr_accessor :collection_time, :job_id, :ok

        def initialize(url, width = 1024, height = 768)
            url = url.gsub!(/&/, '&')
            api_request = 
    %Q{<webthumb><apikey>#{@@api_key}</apikey><request><url>#{url}</url><width>#{width}</width><height>#{height}</height></request></webthumb>}

            result = do_request(api_request)

            if result.class == Net::HTTPOK
              result_data = XmlSimple.xml_in(result.body)
              @job_id = result_data['jobs'].first['job'].first['content']
              @collection_time = Time.now.to_i + result_data['jobs'].first['job'].first['estimate'].to_i
              @ok = true
            else
              @ok = false
            end
        end

        def retrieve(size = :small)
            api_request = 
    %Q{<webthumb><apikey>#{@@api_key}</apikey><fetch><job>#{@job_id}</job><size>#{size.to_s}</size></fetch></webthumb>}
            result = do_request(api_request)
            result.body
        end

        def retrieve_to_file(filename, size = :small)
            File.new(filename, 'w+').write(retrieve(size.to_s))
        end

        def ready?
            return unless Time.now.to_i >= @collection_time

            api_request = %Q{<webthumb><apikey>#{@@api_key}</apikey><status><job>#{@job_id}</job></status></webthumb>}
            result = do_request(api_request)

            if result.class == Net::HTTPOK
              @ok = true
              result_data = XmlSimple.xml_in(result.body)
              begin
                @result_url = result_data['jobStatus'].first['status'].first['pickup']
                @completion_time = result_data['jobStatus'].first['status'].first['completionTime']
              rescue
                @collection_time += 60 
                  return false
              end
            else
              @ok = false
            end

            true
        end

        def ok?
            @ok == true
        end

        def wait_until_ready
            sleep 1 until ready?
        end

        private

        def do_request(body)
            api_url = URI.parse(@@api_baseurl)
            request = Net::HTTP::Post.new(api_url.path)
            request.body = body
            Net::HTTP.new(api_url.host, api_url.port).start {|h| h.request(request) }
        end
    end

    url = 'http://www.rubyinside.com/'
    t = Nailer.new(url)

    if t.ok?
        t.wait_until_ready
        t.retrieve_to_file('out1.jpg', :small)
        t.retrieve_to_file('out2.jpg', :medium)
        t.retrieve_to_file('out3.jpg', :medium2)
        t.retrieve_to_file('out4.jpg', :large)
        puts "Thumbnails saved"
    else
        puts "Error"
    end

I got this code in a single file snippet.rb and it runs as expected. This script if from dzone snippet that fetches the thumbnail of the URL at the current time.

Now I wan to integrate this functionality with Rails and here I'm stuck how to begin.
Should I put this in some ruby file inside lib directory or make it modules???
I'm not much fluent in Ruby, so can anyone plz initiate me how and where to start??

    require 'net/http'
    require 'rubygems'
    require 'xmlsimple'

    class Nailer

        @@api_baseurl = 'http://webthumb.bluga.net/api.php'
        @@api_key = 'YOUR-API-KEY'

        attr_accessor :collection_time, :job_id, :ok

        def initialize(url, width = 1024, height = 768)
            url = url.gsub!(/&/, '&')
            api_request = 
    %Q{<webthumb><apikey>#{@@api_key}</apikey><request><url>#{url}</url><width>#{width}</width><height>#{height}</height></request></webthumb>}

            result = do_request(api_request)

            if result.class == Net::HTTPOK
              result_data = XmlSimple.xml_in(result.body)
              @job_id = result_data['jobs'].first['job'].first['content']
              @collection_time = Time.now.to_i + result_data['jobs'].first['job'].first['estimate'].to_i
              @ok = true
            else
              @ok = false
            end
        end

        def retrieve(size = :small)
            api_request = 
    %Q{<webthumb><apikey>#{@@api_key}</apikey><fetch><job>#{@job_id}</job><size>#{size.to_s}</size></fetch></webthumb>}
            result = do_request(api_request)
            result.body
        end

        def retrieve_to_file(filename, size = :small)
            File.new(filename, 'w+').write(retrieve(size.to_s))
        end

        def ready?
            return unless Time.now.to_i >= @collection_time

            api_request = %Q{<webthumb><apikey>#{@@api_key}</apikey><status><job>#{@job_id}</job></status></webthumb>}
            result = do_request(api_request)

            if result.class == Net::HTTPOK
              @ok = true
              result_data = XmlSimple.xml_in(result.body)
              begin
                @result_url = result_data['jobStatus'].first['status'].first['pickup']
                @completion_time = result_data['jobStatus'].first['status'].first['completionTime']
              rescue
                @collection_time += 60 
                  return false
              end
            else
              @ok = false
            end

            true
        end

        def ok?
            @ok == true
        end

        def wait_until_ready
            sleep 1 until ready?
        end

        private

        def do_request(body)
            api_url = URI.parse(@@api_baseurl)
            request = Net::HTTP::Post.new(api_url.path)
            request.body = body
            Net::HTTP.new(api_url.host, api_url.port).start {|h| h.request(request) }
        end
    end

    url = 'http://www.rubyinside.com/'
    t = Nailer.new(url)

    if t.ok?
        t.wait_until_ready
        t.retrieve_to_file('out1.jpg', :small)
        t.retrieve_to_file('out2.jpg', :medium)
        t.retrieve_to_file('out3.jpg', :medium2)
        t.retrieve_to_file('out4.jpg', :large)
        puts "Thumbnails saved"
    else
        puts "Error"
    end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

叫思念不要吵 2024-08-06 10:39:50

将其放入 lib/nailer.rb 中,然后就可以开始了。 Rails 的自动加载允许您使用 Nailer.new(...) 等,无需任何配置或要求。

Put it in lib/nailer.rb, and you're good to go. Rails' autoloading lets you use Nailer.new(...) and such without any configurations or requires.

望喜 2024-08-06 10:39:50

lib 目录是存放此类实用程序代码的好地方。

The lib directory is a good place for utility code like that.

半岛未凉 2024-08-06 10:39:50

在我看来,您可以将其直接放入 lib/ 目录中名为nailer.rb 的文件中,然后就可以开始了。 由于 lib/ 位于 Rails 应用程序的加载路径中,因此任何给定文件顶部的简单 "require 'nailer'" 就足以将类拉入您的命名空间。

您也可以将其放入应用程序/模型中 - 将非 ActiveRecord 模型放入其中是完全可以的。 不过,由于这不完全是您所在域中的数据模型,因此我认为 lib/ 可能是更好的地方。

Looks to me like you can just drop this straight in to your lib/ directory in a file called nailer.rb, and you should be good to go. Since lib/ is in your load path in a Rails app, a simple "require 'nailer'" at the top of any given file should be all you need to pull the class in to your name space.

You could also put it in app/models - it's totally ok to put non-ActiveRecord models in there. Since this isn't exactly a data model in your domain, though, I think lib/ is probably the better place.

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