Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8098739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:10:10+00:00 2026-06-05T22:10:10+00:00

My application scrapes information from various sites using Mechanize. Naturally, each site requires custom

  • 0

My application scrapes information from various sites using Mechanize. Naturally, each site requires custom Mechanize code. Each site is stored in my database, including the url to scrape and the string name of an .rb file containing that site’s Mechanize code. For this case, let’s assume the scripts are available in the assets folder.

I would like to call http://example.com/site/:id, then have the show action dynamically choose which Mechanize script to run (say, @site.name + ".rb" ). The script will massage the data into a common model, so all sites can use the same show template.

I can’t find a way to dynamically load a .rb script within an action and obtain the result. It may be easier to have the scripts return a JSON string, which I can parse before passing on to the template, but I can’t see a solution for that either. Ideally, the script will run in the action’s scope. The ugly solution is an enormous if-else chain (testing the site name to determine which code block to run), but there must be a better way.

Any suggestions would be greatly appreciated, as would any general solutions to running different code dependent upon the properties of database objects.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:10:12+00:00Added an answer on June 5, 2026 at 10:10 pm

    If you have all the code in your app already why are you eval’ing Ruby code?

    Create classes like:

    class GoogleSpider < Spider; end
    class NewYorkTimesSpider < Spider; end
    class SomeOtherSpider < Spider; end
    

    And the site class will hold the class name that will be used, so you would be able to easily do something like this in your controller action:

    def show
      @site = Site.find(params[:id])
      # name contains SomeOtherSpider
      @process_output = @site.name.constantize.new.process
      # do something with the output here
    end
    

    And then you don’t need to mess around evaluating Ruby code, just call the class needed. You can even make them all singletons or keep them all in a hash for faster access.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which scrapes soccer results from different sources on the web.
I'm writing an aggregation application which scrapes data from a couple of web sources
I developed a simple .net application that scrapes some data from web and saves
Using the following code I am trying to scrape a call log from our
I'm writing an application that scrapes some web pages using QWebPage. I'm having some
I want to scrape text data from a windows application to do additional processing
Our application takes text from a web form and sends it via email to
I'm deveoping web scraping scoftware that relies on XPath to extract information from web
I have a console application which screen scrapes some data, and now I need
I have a client application that scrapes data into a raw table. This client

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.