Okay this seems like a real noob question.
I currently have a simple html and javascript news reader running on my MAMP server. You can see it on Github. Everything runs client side except for fetching and caching the feeds which is done by a really small php file. I have an ajax call which requests the news feed from the php script by passing some parameters to it.
I have of late been learning Ruby and have started to redo this simple php script in Ruby. The problem I have is that I can't just request the ruby script via ajax with parameters like I would do with php.
So what would be the simplest steps to take to be able to do this?
I feel like a full blown framework, even as small as Sinatra, would be overkill. So any help would be much appreciated. Am I completely thinking about this the wrong way round? Thanks.
Update
I went the Ruby CGI way in the end. Here's what I did.
In the folder I had my script I added a .htaccess file with the following to make apache execute the .rb file.
AddHandler cgi-script .rb
Options +ExecCGI
Then I started my ruby file off like this.
#!/usr/bin/ruby
require 'cgi'
cgi = CGI.new('html4')
cgi.out {
cgi.html {
"Hello World" #started content here
}
}
I then made sure the file was executable. chmod 774.
Thanks for all your help.
The simplest solution here would be Ruby CGI.
If you think sinatra is still big for you, go deeper and try to write pure Rack app.
For example see this minimal app - https://github.com/stevenwilkin/ip.stevenwilkin.com
Rack needs only 3 elements array like this
[200 , {"Content-Type" => "text/plain"}, [env["REMOTE_ADDR"]]]
[return status , headers hash , body string ]
Rails, Sinatra and a lot of another ruby web frameworks are using Rack inside.
And if you need request params nicely formated, you can just use http://rack.rubyforge.org/doc/classes/Rack/Request.html for example.
Related
I'm trying to make sense on the best way to do automatize a series of things in a row in order to deploy a web-app and haven't yet came up with a suitable solution. I would like to:
use google's compiler.jar to minify my JS
use yahoo's yui-compressor.jar to minify my CSS
access a file and change a string so that header files like "global.css?v=21" get served the correct version
deploy the app (sftp, mercurial or rsync?) omitting certain directories like "/userfiles"
Can you guys put me on the right track to solve this?
Thank you!
you may want to check out phing http://phing.info/ (they are in the process of moving servers so may be down this weekend), but it can do all of what you are wanting and is written in php.
A quick google search should bring up plenty of tutorials to get you started.
You can run php from the command line to do all sorts of fun things.
$ php script_name.php arg1 arg2
See: command line, argv, argc, exec
Running PHP from the command line is very fast. I've been doing this a lot lately for various automation tasks.
I generally run Python projects so this may or may not be an option for you: but apart from writing your own scripts you could look into the following:
Fabric
Buildout
maven
I'm wondering if anyone has tried to use Julien Hany's Sharrre plugin (http://sharrre.com/) from a Rails app? Specifically, from a view partial? The Google+ portion of the plugin requires a PHP script, which is called from the JavaScript that's in the asset pipeline. How do I execute that script from within the script tags in the view partial? Or if I need to edit Mr Hany's plugin? If so, what might I need to do?
As a side note, I've taken a look at Run a php script in ruby on rails, as well as the Kernel module (http://ruby-doc.org/core-1.9.2/Kernel.html), but I'm not really sure where to start.
Many thanks in advance!
There is no reason to run a php script for this -- you can look at the sharrre.php file in the download and just redo it in ruby, it looks like its using JSON, curl (try the curb gem), and some xpath traversal (try nokogiri or hpricot).
Is there a way to simply send a erb file to a ruby parser get the answer back and send it to client with NGINX? Without all the passenger stuff? It should be easy i guess. I DON'T want to use any rails stuff, don't tell me i should use rails etc.
I created a script to do this two years ago, called ruby-cgi, in response to a similar question. I believe it does exacty what you want. Just set it up the same way you would set up other CGI/FastCGI handlers.
The following gems are available (sorted by feature completeness desc):
rack-server-pages
rhr
Of course you can, try this. But I would highly recommend using Sinatra as it's more like a ruby way of serving web pages like in php
I'm trying to make sense on the best way to do automatize a series of things in a row in order to deploy a web-app and haven't yet came up with a suitable solution. I would like to:
use google's compiler.jar to minify my JS
use yahoo's yui-compressor.jar to minify my CSS
access a file and change a string so that header files like "global.css?v=21" get served the correct version
deploy the app (sftp, mercurial or rsync?) omitting certain directories like "/userfiles"
Can you guys put me on the right track to solve this?
Thank you!
you may want to check out phing http://phing.info/ (they are in the process of moving servers so may be down this weekend), but it can do all of what you are wanting and is written in php.
A quick google search should bring up plenty of tutorials to get you started.
You can run php from the command line to do all sorts of fun things.
$ php script_name.php arg1 arg2
See: command line, argv, argc, exec
Running PHP from the command line is very fast. I've been doing this a lot lately for various automation tasks.
I generally run Python projects so this may or may not be an option for you: but apart from writing your own scripts you could look into the following:
Fabric
Buildout
maven
Is there a framework or something out there so that I can develop webpages in Ruby the same way I can as PHP. Something like
<html><head></head><body>
<?ruby
puts '<p> Hello there!</p>'
?>
</body></html>
The only thing I'm seeing for using Ruby in webpages is huge complex frameworks that is completely different from how PHP works. I mean, sure that's all fine and dandy with the 3 tier model and such but when your just wanting a few simple things done(which are trivial in PHP) in a webpage, to setup such a large framework just doesn't seem right. Especially when you only really want like 1 page made in Ruby and the rest being plain HTML.
The default behavior for PHP is to run as CGI scripts, which means that the web server calls php-cgi <path/to/php-script> or something similar, passing quite a lot of environment variables. To do the same with Ruby, you need to setup a script to handle .rb files. This varies wildly depending on your web server, but if you are using Apache 2.2, put this in your httpd.conf or .htaccess file:
Action ruby-cgi /path/to/ruby-cgi
AddHandler ruby-cgi .rb
# You might want to add this too:
DirectoryIndex index.rb index.html
You could either specify the path to your ruby executable (run which ruby to get the path), or to any other script that accepts a filename as the first parameter. If you use the ruby executable, nothing magical happens, and you can't insert erb into the file without adding some ERB compiling yourself. However, you could use my ruby-cgi script, which does several things:
First, it takes the file and interprets it as ERB, this make the syntax look more like PHP (see below for an example).
Second, it initializes the CGI object into the global variable $CGI. See below for an example on how to use this.
This is a simple example script on how you can use the ruby-cgi "magic":
<% header "Content-Type" => "text/html" %>
<html>
<head>
<title><%= $CGI['title'] %>
</head>
<body>
<h1><%= $CGI['title'] %>
</body>
</html>
Let's say you put this into the webroot with the name example.rb. If you then access this with a URL similar to http://example.com/example.rb?title=Hello%20world it should set the title to "Hello world", and it should display a <h1> with "Hello world" in it.
If you find any bugs with the script, feel free to fork the gist and update it.
Two words: Sinatra and ERB
(For lightweight sites, at least).
Sinatra is a simple HTTP server, ERB is a templating system that acts similar to templating in PHP.
Have you seen Nanoc? It is very simple ruby compiler that outputs static pages through a template engine. May be too simple for your needs but it is handy some times.
You can, with https://github.com/migrs/rack-server-pages
Instruction at that page.
Summarizing:
gem install rack-server-pages
Create config.ru in the root folder of your "application":
require 'rack-server-pages'
run Rack::ServerPages
Create public/index.erb:
<h1>Hello rack!</h1>
<p><%= Time.now %></p>
execute rackup, your Ruby powered pages are ready to be served.
It also works with other application servers, for example, for passenger standalone, you just need to go to that folder and run passenger start.