Displaying url parameter in Ruby - ruby

I am building a VERY simple ruby test application, just to see how it works, but I'm already stuck in overly complex tutorials.
Say that my ruby app is running at heroku at : http://example.herokuapp.com
And that I am calling it like this: http://example.herokuapp.com/test=3 or perhaps http://example.herokuapp.com/page.rb?test=3 ?
How do I get the value from "test" in my ruby output?
My Heroku demo code:
require 'sinatra'
get '/' do
"The value of test is..."
end

In Sinatra the route patterns may consist of named parameters. These parameters can be accessed using the params hash.You need to do something like this:
get '/:test' do
"The value of test is...#{params[:test]}"
end
Url:
http://example.herokuapp.com/3

Related

before filter issue in padrino

I'm trying to create a chain of before filter in padrino that look like this of which look like this
before do
set_current_user
track_order_ip
!current_user and pass
## don't allow the next filter other filter to run if no current user
customer_inactivity!
skip_enforce!
## so the theory is this if a users is is not enforced he should not be allowed to execute enforce! before filter
enforce!
end
Now all filter would execute in chain but if the current_user is not present I wish to drop(i.e pass) the filter chain processing which is taken care by this (!current_user and pass) code
But trying to do something like this in padrino cause the app to redirect the same route multiple time and then break with following error.
ArgumentError at /myaccount/users/authenticate
uncaught throw :pass
at
!current_customer and pass
What I'm find weird and what I'm not able to understand is , why? it not working in Padrino(since I know Padrino internally uses Sinatra) because I did wrote a proof of concept similar application in sinatra (can be found over here) and that just seem to work out of box without any issue
Lastly here the padrino code
Now any one can give me some pointer as too what I'm doing wrong in padrino that is implemented correctly in the proof of concept sinatra app
Thanks

Passing options and parameters to Test/Unit via Rake::TestTask

So I've been trying to figure this out, and the best solution I can came up with his global variables - but that seems so dirty and 1974 - Am I missing a feature of Rake/ Test::Unit?
I have a Rake file in which I'm running tests:
Rake::TestTask.new(:test) do |t|
t.test_files = FileList['test_*.rb']
end
and test_1.rb has something like this:
require "test/unit"
class TestStuff < Test::Unit::TestCase
def setup
#thingy = Thing.New(parameter1, parameter2)
end
def test_this_thing
#thing.do()
end
end
My problem is, Thing.new() requires arguments, and those arguments are specific to my environment. In reality, I'm running Selenium-WebDriver, and I want to pass in a browser type, and a url, etc... sometimes I want ff, othertimes I want chrome... sometimes this URL, sometimes that... depending on the day, etc.
The simplest thing seems to do something like:
#all that rake stuff
$parameter1 = x
$parameter2 = y
and then make Thing.new() look up my global vars:
#thingy = Thing.New($parameter1, $parameter2)
This seems sloppy.. and it just doesn't feel right to me. I'm still trying to get this 'test harness' up and running, and want to do it right the first time. That's why I chose Rake, based on a lot of other feedback.
Keep in mind, I'll probably have 100's of tests, ultimately, and they'll all need to do get this information... I thought Rake was good at making sure all of this was easy, but it doesn't seem to be.
Where did I go wrong?
I have used YAML files to store my configuration (browser config, environments including URLs, etc).
You can also use an environmental variable to define simple configurations. You can access environmental variables via ENV['foobar'] in Ruby.
So, for example, my browser call might look like this inside my setup method:
driver = Selenium::WebDriver.for (ENV['SWD_BROWSER'] || "firefox").to_sym
and in my Rake file (or in the shell console) define the environmental variable to use.

Understanding Ruby (Sinatra) - very very basic

I was looking at Sinatra and trying to understand the syntax:
require 'sinatra'
get '/' do
"Hello, World!"
end
I understand it does this:
This is a ‘Route’. Here, we’re telling Sinatra that if the home, or root, URL '/' is requested, using the normal GET HTTP method, to display “Hello, World!”
But what is happening the Ruby language?
What does this syntax mean: get '/'? Is get a method and '/' a parameter to it? If it is method, then in Ruby, I can call a method as methodname (parameter) {}. What is { } there for?
I usually understand do and end as { }, which are kinds of enclosures to function bodies.
Between do and end we have "Hello, World!" so is it a statement? What I mean is, it is getting printed, but we did not call it as print "Hello, World!", so what is going on?
It seems get is a method defined in Sinatra, but if I add a gem, where there is a get method already defined, then how do I know which 'get' method it would call? Or, does it refer to the HTTP get method?
I am sorry if this question sounds very basic, but I want to get through it before I move forward.
May I suggest going through a tutorial on ruby before tackling a larger problem like sinatra which is a fairly specialized library.
A good place to start is with the Ruby Koans
As for your questions.
get is a method. '/' is its argument. and do ... end denotes a block in ruby just like {} would.
Yeah that's what do ... end are
Blocks in Ruby return the last value calculated by default so in the is case having a string is the same as having return "String".
If you are getting a namespace collision, Ruby will complain. In this case get is the sinatra defined method get. Abstractly it stands for an HTTP GET request against the server.
For the "perfect" response, I suggest you have a look at the book "Sinatra Up and Running" by
Alan Harris and Konstantin Haase.
http://shop.oreilly.com/product/0636920019664.do
Pages 6 and 7 explain how the line "get '/' do" is in fact a method call.
And you can view this 2 pages with Google Preview.

How to access sinatra class variable in coffeescript template

How can I access ruby instance variable from within coffeescript template?
In sinatra documentation it's said that templates are evaluated within same scope as rout that invoke that template.
So, I have following sinatra app:
server.rb:
require "sinatra"
require "coffee-script"
get '/app.js' do
#str = "Hello"
coffee :app
end
and in views/app.coffe file I would like to use #strvariable. Is it possible? If so, how can I access #str variable?
It could be possible only if you'll process coffee source file with something like erb. So if you'd use rails assets pipeline you can just append .erb to file extension and the file will be processed with erb before sending it to coffee I think in sinatra you'll have to wrap up something similar yourself.
The idea will be close to this one - http://www.sinatrarb.com/intro#Textile%20Templates
P.S: accessing variables from different layers of application is bad idea anyway.
EDIT
You have amultistage template compilation process in RAILS driven by a gem called sprockets. You start with a file for example called /app/views/foo/show.js.coffee.erb
class <%= #magic %>
doSomthing: ->
console.log "hello"
In your controller you add the instance variable
#magic = "Crazy"
Rails first processes the erb file and generates
class Crazy
doSomething: ->
console.log "hello"
Secondly it processes the coffeescript file to generate
var Crazy;
Crazy = (function() {
function Crazy() {}
Crazy.prototype.doSomething = function() {
return console.log("hello");
};
return Crazy;
})();
That is why it is called the asset pipeline. More conventionally you could call it
a compilation pipeline. If you know what you are doing you might be able to get sprockets running with Sinatra. However your life would be easier if you just used Rails 3.1 from
the start.
I wrote this for Rails: https://github.com/ludicast/ice
but it can be easily adapted for Sinatra.
It lets you use Eco and CoffeeKup templates inside a Rails application, with the ruby models exposed to Coffeescript.
Nate

Rails -- use RSpec "get" and "response" methods in Rake task

I know RSpec has useful methods "get" and "response.should" to run integration tests - I want to know how I can use these (or other methods to achieve the same result) in a Rake task:
desc "Check all content items with type 0 and do something"
task :my_task => :environment do
ContentItem.where("content_type = ?", 0).each do |obj|
get "/my_path/"+obj.value
if (response has a certain html tag)
perform some action on obj
end
end
end
I know that I can't just run rspec methods like that, but this is effectively what I need to do, and I need to be able to process information returned when /my_path/obj.value is opened. Does anyone have any suggestions?
why do you need to go through the url to do this action with your ContentItem ? Why not just use the local obj and do stuff?
Basically it looks like you're mixing up view-code with model code here... the model's object should not depend on values in the html... if there is some information being figured out in the view... put it into a method on the ContentItem model and call that code either from the view... or from this rake task.
Edit:
Ok.... well if you really need to GET a URL - look into Ruby's Net::Http gem - that will literally fetch URLs. Rails doesn't do that as standard... even for local URLs.
You might then be able to use a parser such as hpricot or nokogiri to parse the results to find the tag you need.

Resources