NameError: undefined local variable or method `request' for main:Object - sinatra

I'm building a Sinatra app which will take a visitor's IP address and return the weather of that location. Following the Geocoder doc, it would seem that getting the IP is as simple as
city = request.location.city
Entering that into irb, however, just gives the NameError you see in the post title. Going what seems to me the logical route and attempting to create a new class upon which to use the request method results in another NameError - this time for the env hash taken by Rack::Request.new(env).
I can figure out how to build the thing on my own, but I'm stumped on this particular point, and assume that I am overlooking something not mentioned in the docs. What am I doing wrong that is causing request and env to remain undefined, and how do I go about correctly defining them?

It is as simple as that:
require "sinatra"
require "geocoder"
get "/" do
p request.location.city
"works"
end
Your problem is that you trying to run it in irb where you haven't the context of a request which creates and populates your request object. To play with it I would recommend just to run sinatra. Or use something like racksh, tux which gives you a something like irb but with all the context needed.

Related

Why doesn't elapsed_time() work from log_message() even if called from post_system hook?

If I try to log benchmark->elapsed_time() in post_system hook, it just logs {elapsed_time} as if I called it from a controller.
CodeIgniter documentation says:
"post_system Called after the final rendered page is sent to the browser, at the end of system execution after the finalized data is sent to the browser."
It also says you are supposed to echo the elapsed_time() in a view to show it to the user, but... how is it possible that elapsed_time() is still being calculated after sending the finalized data to the browser?
I feel being lied to.
People keep saying I should use my own marks and get the difference, but that's not the same as using this...
Turns out the documentation also says:
"If the first parameter is empty this function instead returns the {elapsed_time} pseudo-variable. This permits the full system execution time to be shown in a template. The output class will swap the real value for this variable."
I went to the Output class and found the 2 marks it is using: total_execution_time_start and total_execution_time_end and I can use those in the post_system hook.

Get Status of Driver Inside Visual Basic

I'm currently trying to detect which drivers appear as "not working" in visual basic.
This unknown device is a good example of what I'm trying to grab (notice how it has the flag DN_HAS_PROBLEM).
I've tried using searches such as:
Dim searcher As New ManagementObjectSearcher( "root\CIMV2", "SELECT * FROM Win32_SystemDriver")
And running a loop in the searcher.Get() through this documentation
However, none of these seem to return what I am looking for.
Would anyone happen to know how I can get the DN_ statuses within Visual Basic?
Thanks!
The Win32_SystemDriver class documentation lists these Status properties:
OK
Error
Degraded
Unknown
Pred Fail
Starting
Stopping
Service
Stressed
NonRecover
No Contact
Lost Comm
...whereas DN_HAS_PROBLEM comes from the CM_Get_DevNode_Status function, or perhaps also from other system calls.
There may not be a way to get that specific code from the API you're using, but perhaps the existing Status properties will suffice for your needs if you don't need to know more specific failure reasons.
If you do need to know that specific status, you'll have to call other APIs, like the one I called out.

Issue with a topjson object in a Meteor app built with coffeescript

Apologies for the lack of precision in the question, but I'm not completely sure which of possibly many things I'm doing wrong here.
I'm relatively new to Coffeescript and also geo applications in general, but here goes:
I've got a working (simple) Meteor (.7.0.1) application utilizing coffeescript in both client and server. The issue I'm having occurs when attempting to utilize TopoJSON encoded files to create a layer of US congressional districts. (the purpose of the app is to help highlight voter suppression in the US)
So, a few things: Typically in a non-Meteor app, I would just load the topoJSON file like so:
$.getJSON('./data/us-congress-113.json', function (data) {
var congress_geojson = topojson.feature(data, data.objects.districts);
congress_layer.addData(congress_geojson);
});
Now of course this won't work in Meteor because its not asynchronous.
One of the things that was recommended here on SO was to not worry about reading the file, and to instead change the json file to .js, and then set the contents (which are of course just an object) equal to a variable.
Here's what I did:
First, I changed the .json file to a .js file in the server directory, and added the "congress =" to the beginning of the file. It's a huge file so forgive me for omitting the whole object.
congress = {"type":"Topology",
"objects":
{"districts":
{"type":"GeometryCollection","geometries":[{"type":"Polygon"
Now here's where everything starts to give me issues:
In the server.coffee, I've created a variable like so to reference the congress object:
#congress_geojson = topojson.feature(congress, congress.objects.districts)
Notice how I'm putting the # symbol there? I've been told this allows a variable in Coffeescript to be globally scoped? I tried to also use a Meteor feature called "share" where I declare the variable as "share.congress_geojson". That led to the same issues which I will describe below.
Now in the client.coffee file, I'm trying to call this variable to load into a Leaflet map.
congress_layer = L.geoJson(null,
style:
color: "#DE0404"
weight: 2
opacity: 0.4
fillOpacity: 0.1
)
congress_layer.addData(#congress_geojson)
This isn't working, and specifically (despite attempts to find other ways, the errors I'm getting in the console are:
Exception from Deps afterFlush function: TypeError: Cannot read property 'features' of undefined
at o.GeoJSON.o.FeatureGroup.extend.addData (http://localhost:3000/packages/leaflet.js?ad7b569067d1f68c7403ea1c89a172b4cfd68d85:39:16471)
at Object.Template.map.rendered (http://localhost:3000/client/client.coffee.js?37b1cdc5945f3407f2726a5719e1459f44d1db2d:213:18)
I have no doubt that I'm missing something stupidly obvious here. Any suggestions or tips for what I'm doing completely wrong would be appreciated. Is it a case where an object globally declared in a .js file isn't available to code in a .coffee file? Maybe I'm doing something wrong on the Meteor side?
Thanks!
Edit:
So I was able to get things working by putting the .js file containing the congress object in a root /lib folder, causing the object to load first, and then calling the congress object from the client. However, I'm still wanting to know how I could simply share this object from the server? What is the "Meteor way" here?
If you are looking for the Meteor way to order the loading of files, use the Meteor.startup function and put the initialization code there. That function is the $.ready of the Meteor world, i.e., it will execute only after all your files have been successfully loaded on the client.
So in your case:
Meter.startup ->
congress_layer.addData(#congress_geojson)

Perl Net::Appliance::Session waitfor?

I have a problem with Net::Appliance::Session. I created a session, executed my command. After execution it prompts me some question (yes/no). I want to answer it but didn't find a way how to do it. Below you can see my trials:
$session->cmd($command);
$session->waitfor(Match=>'/.*yes*/');
$session->print("no");
$session->waitfor(Match=>'');
$session->print("y");
I don't know where is the problem. Accoding to CPAN documentations Net::Telnet have the method waitfor. But Session documentation tells that we can use waitfor(). Another thing said there is that the method "cmd" have a member Match which includes all the features of waitfor(). So I changed my code like below:
$session->cmd($command, Match=>'/.*yes*/');
$session->print("no");
Executing this reports below error:
Odd number of elements in hash assignment at
/usr/lib/perl5/vendor_perl/5.8.8/Net/Appliance/Session.pm line 245.
Is there any idea how can I do that? And why am I getting this error message?
Thanks in advance..
From the Net::Appliance::Session page at meta::cpan
To handle more complicated interactions, for example commands which prompt for confirmation or optional parameters, you should use a Macro. These are set up in the phrasebook and issued via the $s->macro($name) method call. See the Phrasebook and Cookbook manual pages for further details.
So you set up a macro (scripted call and response) in a phrasebook, and then tell your session to use that macro.

Padrino + MongoMapper/Joint Troubles

I'm using MongoMapper with Joint on Padrino, and trying to get the
upload working. However, I keep getting thrown a NoMethodError
"undefined method 'path' for #<Hash:0xa6fbdf0>". It seems like it
can't see the path, but the parameters are okay. What is the problem
here?
Gist with the code: https://gist.github.com/1323998
I was able to get it to not error, but when I go to find the file with mongofiles, I can't find. The same goes for rack/grid-fs. Where is Joint saving to, and is it saving at all?
See my comments above:
Here's my thought on what you need to do, I think you need to modify the params so that params[:background][:file] is the tempfile object, like so:
params[:background][:file] = params[:background][:file][:tempfile]
background = Background.create(params[:background])
I'm not 100% sure on this, but if this doen't work I could setup a quick Padrino app and test.